Move bst validate to a class (#98)

This commit is contained in:
Donne Martin 2016-08-30 06:06:58 -04:00 committed by GitHub
parent 982580d051
commit dfeb7f4d80
3 changed files with 59 additions and 55 deletions

View File

@ -103,9 +103,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def validate_bst(node):\n", "class BstValidate(Bst):\n",
" # TODO: Implement me\n", "\n",
" pass" " def validate(self):\n",
" # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@ -142,22 +144,22 @@
" validate_bst(None)\n", " validate_bst(None)\n",
"\n", "\n",
" def test_bst_validate(self):\n", " def test_bst_validate(self):\n",
" node = Node(5)\n", " bst = BstValidate(Node(5))\n",
" insert(node, 8)\n", " bst.insert(8)\n",
" insert(node, 5)\n", " bst.insert(5)\n",
" insert(node, 6)\n", " bst.insert(6)\n",
" insert(node, 4)\n", " bst.insert(4)\n",
" insert(node, 7)\n", " bst.insert(7)\n",
" assert_equal(validate_bst(node), True)\n", " assert_equal(bst.validate(), True)\n",
"\n", "\n",
" root = Node(5)\n", " bst = BstValidate(Node(5))\n",
" left = Node(5)\n", " left = Node(5)\n",
" right = Node(8)\n", " right = Node(8)\n",
" invalid = Node(20)\n", " invalid = Node(20)\n",
" root.left = left\n", " bst.root.left = left\n",
" root.right = right\n", " bst.root.right = right\n",
" root.left.right = invalid\n", " bst.root.left.right = invalid\n",
" assert_equal(validate_bst(root), False)\n", " assert_equal(bst.validate(), False)\n",
"\n", "\n",
" print('Success: test_bst_validate')\n", " print('Success: test_bst_validate')\n",
"\n", "\n",

View File

@ -113,24 +113,26 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def validate_bst(node):\n", "import sys\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", "class BstValidate(Bst):\n",
" if node is None:\n", "\n",
" return True\n", " def validate(self):\n",
" if mininum is not None and node.data <= mininum:\n", " if self.root is None:\n",
" return False\n", " raise Exception('No root node')\n",
" if maximum is not None and node.data > maximum:\n", " return self._validate(self.root)\n",
" return False\n", "\n",
" if not _validate_bst(node.left, mininum, node.data):\n", " def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
" return False\n", " if node is None:\n",
" if not _validate_bst(node.right, node.data, maximum):\n", " return True\n",
" return False\n", " if node.data <= mininum or node.data > maximum:\n",
" return True" " return False\n",
" if not self._validate(node.left, mininum, node.data):\n",
" return False\n",
" if not self._validate(node.right, node.data, maximum):\n",
" return False\n",
" return True"
] ]
}, },
{ {
@ -168,22 +170,22 @@
" validate_bst(None)\n", " validate_bst(None)\n",
"\n", "\n",
" def test_bst_validate(self):\n", " def test_bst_validate(self):\n",
" node = Node(5)\n", " bst = BstValidate(Node(5))\n",
" insert(node, 8)\n", " bst.insert(8)\n",
" insert(node, 5)\n", " bst.insert(5)\n",
" insert(node, 6)\n", " bst.insert(6)\n",
" insert(node, 4)\n", " bst.insert(4)\n",
" insert(node, 7)\n", " bst.insert(7)\n",
" assert_equal(validate_bst(node), True)\n", " assert_equal(bst.validate(), True)\n",
"\n", "\n",
" root = Node(5)\n", " bst = BstValidate(Node(5))\n",
" left = Node(5)\n", " left = Node(5)\n",
" right = Node(8)\n", " right = Node(8)\n",
" invalid = Node(20)\n", " invalid = Node(20)\n",
" root.left = left\n", " bst.root.left = left\n",
" root.right = right\n", " bst.root.right = right\n",
" root.left.right = invalid\n", " bst.root.left.right = invalid\n",
" assert_equal(validate_bst(root), False)\n", " assert_equal(bst.validate(), False)\n",
"\n", "\n",
" print('Success: test_bst_validate')\n", " print('Success: test_bst_validate')\n",
"\n", "\n",

View File

@ -9,22 +9,22 @@ class TestBstValidate(object):
validate_bst(None) validate_bst(None)
def test_bst_validate(self): def test_bst_validate(self):
node = Node(5) bst = BstValidate(Node(5))
insert(node, 8) bst.insert(8)
insert(node, 5) bst.insert(5)
insert(node, 6) bst.insert(6)
insert(node, 4) bst.insert(4)
insert(node, 7) bst.insert(7)
assert_equal(validate_bst(node), True) assert_equal(bst.validate(), True)
root = Node(5) bst = BstValidate(Node(5))
left = Node(5) left = Node(5)
right = Node(8) right = Node(8)
invalid = Node(20) invalid = Node(20)
root.left = left bst.root.left = left
root.right = right bst.root.right = right
root.left.right = invalid bst.root.left.right = invalid
assert_equal(validate_bst(root), False) assert_equal(bst.validate(), False)
print('Success: test_bst_validate') print('Success: test_bst_validate')