mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-09 02:48:02 +00:00
Polish bst validate challenge and solution (#83)
Update constraints, test cases, tests, algorithm discussion, and code.
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user