Polish bst validate challenge and solution (#83)

Update constraints, test cases, tests, algorithm discussion, and code.
This commit is contained in:
Donne Martin 2016-06-25 22:08:54 -04:00 committed by GitHub
parent 7b573ceaa3
commit 589ff06b15
3 changed files with 46 additions and 15 deletions

View File

@ -36,7 +36,11 @@
"\n", "\n",
"* Can the tree have duplicates?\n", "* Can the tree have duplicates?\n",
" * Yes\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", "* Can we assume we already have a Node class?\n",
" * Yes\n",
"* Can we assume this fits in memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -128,10 +132,15 @@
"source": [ "source": [
"# %load test_bst_validate.py\n", "# %load test_bst_validate.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
"from nose.tools import raises\n",
"\n", "\n",
"\n", "\n",
"class TestBstValidate(object):\n", "class TestBstValidate(object):\n",
"\n", "\n",
" @raises(Exception)\n",
" def test_bst_validate_empty(self):\n",
" validate_bst(None)\n",
"\n",
" def test_bst_validate(self):\n", " def test_bst_validate(self):\n",
" node = Node(5)\n", " node = Node(5)\n",
" insert(node, 8)\n", " insert(node, 8)\n",
@ -149,11 +158,13 @@
" root.right = right\n", " root.right = right\n",
" root.left.right = invalid\n", " root.left.right = invalid\n",
" assert_equal(validate_bst(root), False)\n", " assert_equal(validate_bst(root), False)\n",
"\n",
" print('Success: test_bst_validate')\n", " print('Success: test_bst_validate')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestBstValidate()\n", " test = TestBstValidate()\n",
" test.test_bst_validate_empty()\n",
" test.test_bst_validate()\n", " test.test_bst_validate()\n",
"\n", "\n",
"\n", "\n",
@ -173,21 +184,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -35,7 +35,11 @@
"\n", "\n",
"* Can the tree have duplicates?\n", "* Can the tree have duplicates?\n",
" * Yes\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", "* Can we assume we already have a Node class?\n",
" * Yes\n",
"* Can we assume this fits in memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -45,6 +49,8 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"None -> exception\n",
"\n",
"<pre>\n", "<pre>\n",
"Valid:\n", "Valid:\n",
" 5\n", " 5\n",
@ -70,7 +76,7 @@
"\n", "\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", "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", "\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 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", "* if max is set and the node's value > max, return False\n",
"* Recursively call the validate function on node.left, updating max\n", "* Recursively call the validate function on node.left, updating max\n",
@ -108,19 +114,21 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def validate_bst(node):\n", "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",
"\n", "\n",
"def __validate_bst__(node, mininum, maximum):\n", "def _validate_bst(node, mininum, maximum):\n",
" if node is None:\n", " if node is None:\n",
" return True\n", " return True\n",
" if mininum is not None and node.data <= mininum:\n", " if mininum is not None and node.data <= mininum:\n",
" return False\n", " return False\n",
" if maximum is not None and node.data > maximum:\n", " if maximum is not None and node.data > maximum:\n",
" return False\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", " 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 False\n",
" return True" " return True"
] ]
@ -150,10 +158,15 @@
"source": [ "source": [
"%%writefile test_bst_validate.py\n", "%%writefile test_bst_validate.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
"from nose.tools import raises\n",
"\n", "\n",
"\n", "\n",
"class TestBstValidate(object):\n", "class TestBstValidate(object):\n",
"\n", "\n",
" @raises(Exception)\n",
" def test_bst_validate_empty(self):\n",
" validate_bst(None)\n",
"\n",
" def test_bst_validate(self):\n", " def test_bst_validate(self):\n",
" node = Node(5)\n", " node = Node(5)\n",
" insert(node, 8)\n", " insert(node, 8)\n",
@ -177,6 +190,7 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestBstValidate()\n", " test = TestBstValidate()\n",
" test.test_bst_validate_empty()\n",
" test.test_bst_validate()\n", " test.test_bst_validate()\n",
"\n", "\n",
"\n", "\n",
@ -206,21 +220,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -1,8 +1,13 @@
from nose.tools import assert_equal from nose.tools import assert_equal
from nose.tools import raises
class TestBstValidate(object): class TestBstValidate(object):
@raises(Exception)
def test_bst_validate_empty(self):
validate_bst(None)
def test_bst_validate(self): def test_bst_validate(self):
node = Node(5) node = Node(5)
insert(node, 8) insert(node, 8)
@ -26,6 +31,7 @@ class TestBstValidate(object):
def main(): def main():
test = TestBstValidate() test = TestBstValidate()
test.test_bst_validate_empty()
test.test_bst_validate() test.test_bst_validate()