mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish bst validate challenge and solution (#83)
Update constraints, test cases, tests, algorithm discussion, and code.
This commit is contained in:
parent
7b573ceaa3
commit
589ff06b15
|
@ -36,7 +36,11 @@
|
|||
"\n",
|
||||
"* Can the tree have duplicates?\n",
|
||||
" * Yes\n",
|
||||
"* If this is called on a None input, should we raise an exception?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume we already have a Node class?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume this fits in memory?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
|
@ -128,10 +132,15 @@
|
|||
"source": [
|
||||
"# %load test_bst_validate.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBstValidate(object):\n",
|
||||
"\n",
|
||||
" @raises(Exception)\n",
|
||||
" def test_bst_validate_empty(self):\n",
|
||||
" validate_bst(None)\n",
|
||||
"\n",
|
||||
" def test_bst_validate(self):\n",
|
||||
" node = Node(5)\n",
|
||||
" insert(node, 8)\n",
|
||||
|
@ -149,11 +158,13 @@
|
|||
" root.right = right\n",
|
||||
" root.left.right = invalid\n",
|
||||
" assert_equal(validate_bst(root), False)\n",
|
||||
"\n",
|
||||
" print('Success: test_bst_validate')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestBstValidate()\n",
|
||||
" test.test_bst_validate_empty()\n",
|
||||
" test.test_bst_validate()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -173,21 +184,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -35,7 +35,11 @@
|
|||
"\n",
|
||||
"* Can the tree have duplicates?\n",
|
||||
" * Yes\n",
|
||||
"* If this is called on a None input, should we raise an exception?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume we already have a Node class?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume this fits in memory?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
|
@ -45,6 +49,8 @@
|
|||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"None -> exception\n",
|
||||
"\n",
|
||||
"<pre>\n",
|
||||
"Valid:\n",
|
||||
" 5\n",
|
||||
|
@ -70,7 +76,7 @@
|
|||
"\n",
|
||||
"We'll use a recursive solution that valides left <= current < right, passing down the min and max values as we do a depth-first traversal.\n",
|
||||
"\n",
|
||||
"* If the node is None, return False\n",
|
||||
"* If the node is None, return True\n",
|
||||
"* If min is set and the node's value <= min, return False\n",
|
||||
"* if max is set and the node's value > max, return False\n",
|
||||
"* Recursively call the validate function on node.left, updating max\n",
|
||||
|
@ -108,19 +114,21 @@
|
|||
"outputs": [],
|
||||
"source": [
|
||||
"def validate_bst(node):\n",
|
||||
" return __validate_bst__(node, None, None)\n",
|
||||
" if node is None:\n",
|
||||
" raise Exception('No root node')\n",
|
||||
" return _validate_bst(node, None, None)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def __validate_bst__(node, mininum, maximum):\n",
|
||||
"def _validate_bst(node, mininum, maximum):\n",
|
||||
" if node is None:\n",
|
||||
" return True\n",
|
||||
" if mininum is not None and node.data <= mininum:\n",
|
||||
" return False\n",
|
||||
" if maximum is not None and node.data > maximum:\n",
|
||||
" return False\n",
|
||||
" if not __validate_bst__(node.left, mininum, node.data):\n",
|
||||
" if not _validate_bst(node.left, mininum, node.data):\n",
|
||||
" return False\n",
|
||||
" if not __validate_bst__(node.right, node.data, maximum):\n",
|
||||
" if not _validate_bst(node.right, node.data, maximum):\n",
|
||||
" return False\n",
|
||||
" return True"
|
||||
]
|
||||
|
@ -150,10 +158,15 @@
|
|||
"source": [
|
||||
"%%writefile test_bst_validate.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBstValidate(object):\n",
|
||||
"\n",
|
||||
" @raises(Exception)\n",
|
||||
" def test_bst_validate_empty(self):\n",
|
||||
" validate_bst(None)\n",
|
||||
"\n",
|
||||
" def test_bst_validate(self):\n",
|
||||
" node = Node(5)\n",
|
||||
" insert(node, 8)\n",
|
||||
|
@ -177,6 +190,7 @@
|
|||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestBstValidate()\n",
|
||||
" test.test_bst_validate_empty()\n",
|
||||
" test.test_bst_validate()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -206,21 +220,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
from nose.tools import assert_equal
|
||||
from nose.tools import raises
|
||||
|
||||
|
||||
class TestBstValidate(object):
|
||||
|
||||
@raises(Exception)
|
||||
def test_bst_validate_empty(self):
|
||||
validate_bst(None)
|
||||
|
||||
def test_bst_validate(self):
|
||||
node = Node(5)
|
||||
insert(node, 8)
|
||||
|
@ -26,6 +31,7 @@ class TestBstValidate(object):
|
|||
|
||||
def main():
|
||||
test = TestBstValidate()
|
||||
test.test_bst_validate_empty()
|
||||
test.test_bst_validate()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user