From dfeb7f4d806fd95e4c7775f818c4b6d3bbc339fb Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 30 Aug 2016 06:06:58 -0400 Subject: [PATCH] Move bst validate to a class (#98) --- .../bst_validate/bst_validate_challenge.ipynb | 32 +++++----- .../bst_validate/bst_validate_solution.ipynb | 58 ++++++++++--------- .../bst_validate/test_bst_validate.py | 24 ++++---- 3 files changed, 59 insertions(+), 55 deletions(-) diff --git a/graphs_trees/bst_validate/bst_validate_challenge.ipynb b/graphs_trees/bst_validate/bst_validate_challenge.ipynb index c5e579a..5aef392 100644 --- a/graphs_trees/bst_validate/bst_validate_challenge.ipynb +++ b/graphs_trees/bst_validate/bst_validate_challenge.ipynb @@ -103,9 +103,11 @@ }, "outputs": [], "source": [ - "def validate_bst(node):\n", - " # TODO: Implement me\n", - " pass" + "class BstValidate(Bst):\n", + "\n", + " def validate(self):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -142,22 +144,22 @@ " validate_bst(None)\n", "\n", " def test_bst_validate(self):\n", - " node = Node(5)\n", - " insert(node, 8)\n", - " insert(node, 5)\n", - " insert(node, 6)\n", - " insert(node, 4)\n", - " insert(node, 7)\n", - " assert_equal(validate_bst(node), True)\n", + " bst = BstValidate(Node(5))\n", + " bst.insert(8)\n", + " bst.insert(5)\n", + " bst.insert(6)\n", + " bst.insert(4)\n", + " bst.insert(7)\n", + " assert_equal(bst.validate(), True)\n", "\n", - " root = Node(5)\n", + " bst = BstValidate(Node(5))\n", " left = Node(5)\n", " right = Node(8)\n", " invalid = Node(20)\n", - " root.left = left\n", - " root.right = right\n", - " root.left.right = invalid\n", - " assert_equal(validate_bst(root), False)\n", + " bst.root.left = left\n", + " bst.root.right = right\n", + " bst.root.left.right = invalid\n", + " assert_equal(bst.validate(), False)\n", "\n", " print('Success: test_bst_validate')\n", "\n", diff --git a/graphs_trees/bst_validate/bst_validate_solution.ipynb b/graphs_trees/bst_validate/bst_validate_solution.ipynb index 23f50c2..3b3dd7b 100644 --- a/graphs_trees/bst_validate/bst_validate_solution.ipynb +++ b/graphs_trees/bst_validate/bst_validate_solution.ipynb @@ -113,24 +113,26 @@ }, "outputs": [], "source": [ - "def validate_bst(node):\n", - " if node is None:\n", - " raise Exception('No root node')\n", - " return _validate_bst(node, None, None)\n", + "import sys\n", "\n", "\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", - " return False\n", - " if not _validate_bst(node.right, node.data, maximum):\n", - " return False\n", - " return True" + "class BstValidate(Bst):\n", + "\n", + " def validate(self):\n", + " if self.root is None:\n", + " raise Exception('No root node')\n", + " return self._validate(self.root)\n", + "\n", + " def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n", + " if node is None:\n", + " return True\n", + " if node.data <= mininum or node.data > maximum:\n", + " 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", "\n", " def test_bst_validate(self):\n", - " node = Node(5)\n", - " insert(node, 8)\n", - " insert(node, 5)\n", - " insert(node, 6)\n", - " insert(node, 4)\n", - " insert(node, 7)\n", - " assert_equal(validate_bst(node), True)\n", + " bst = BstValidate(Node(5))\n", + " bst.insert(8)\n", + " bst.insert(5)\n", + " bst.insert(6)\n", + " bst.insert(4)\n", + " bst.insert(7)\n", + " assert_equal(bst.validate(), True)\n", "\n", - " root = Node(5)\n", + " bst = BstValidate(Node(5))\n", " left = Node(5)\n", " right = Node(8)\n", " invalid = Node(20)\n", - " root.left = left\n", - " root.right = right\n", - " root.left.right = invalid\n", - " assert_equal(validate_bst(root), False)\n", + " bst.root.left = left\n", + " bst.root.right = right\n", + " bst.root.left.right = invalid\n", + " assert_equal(bst.validate(), False)\n", "\n", " print('Success: test_bst_validate')\n", "\n", diff --git a/graphs_trees/bst_validate/test_bst_validate.py b/graphs_trees/bst_validate/test_bst_validate.py index 815a03e..89a9dcb 100644 --- a/graphs_trees/bst_validate/test_bst_validate.py +++ b/graphs_trees/bst_validate/test_bst_validate.py @@ -9,22 +9,22 @@ class TestBstValidate(object): validate_bst(None) def test_bst_validate(self): - node = Node(5) - insert(node, 8) - insert(node, 5) - insert(node, 6) - insert(node, 4) - insert(node, 7) - assert_equal(validate_bst(node), True) + bst = BstValidate(Node(5)) + bst.insert(8) + bst.insert(5) + bst.insert(6) + bst.insert(4) + bst.insert(7) + assert_equal(bst.validate(), True) - root = Node(5) + bst = BstValidate(Node(5)) left = Node(5) right = Node(8) invalid = Node(20) - root.left = left - root.right = right - root.left.right = invalid - assert_equal(validate_bst(root), False) + bst.root.left = left + bst.root.right = right + bst.root.left.right = invalid + assert_equal(bst.validate(), False) print('Success: test_bst_validate')