Move bst to a class

This commit is contained in:
Donne Martin 2016-08-14 08:20:06 -04:00
parent be5f29f98e
commit 82c9725000
4 changed files with 119 additions and 81 deletions

View File

@ -10,22 +10,34 @@ class Node(object):
return str(self.data) return str(self.data)
def insert(root, data): class Bst(object):
# Constraint: Assume we are working with valid ints
if root is None: def __init__(self, root=None):
root = Node(data) self.root = root
return root
if data <= root.data: def insert(self, data):
if root.left is None: if data is None:
root.left = insert(root.left, data) raise Exception('Data cannot be None')
root.left.parent = root if self.root is None:
return root.left self.root = Node(data)
return self.root
return self._insert(self.root, data)
def _insert(self, node, data):
# Constraint: Assume we are working with valid ints
if node is None:
return Node(data)
if data <= node.data:
if node.left is None:
node.left = self._insert(node.left, data)
node.left.parent = node
return node.left
else:
return self._insert(node.left, data)
else: else:
return insert(root.left, data) if node.right is None:
else: node.right = self._insert(node.right, data)
if root.right is None: node.right.parent = node
root.right = insert(root.right, data) return node.right
root.right.parent = root else:
return root.right return self._insert(node.right, data)
else:
return insert(root.right, data)

View File

@ -98,9 +98,11 @@
" pass\n", " pass\n",
"\n", "\n",
"\n", "\n",
"def insert(root, data):\n", "class Bst(object):\n",
" # TODO: Implement me\n", "\n",
" pass" " def insert(self, data):\n",
" # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@ -156,22 +158,25 @@
" def __init__(self):\n", " def __init__(self):\n",
" self.results = Results()\n", " self.results = Results()\n",
"\n", "\n",
" def test_tree(self):\n", " def test_tree_one(self):\n",
" node = Node(5)\n", " bst = Bst()\n",
" assert_equal(insert(node, 2).data, 2)\n", " bst.insert(5)\n",
" assert_equal(insert(node, 8).data, 8)\n", " bst.insert(2)\n",
" assert_equal(insert(node, 1).data, 1)\n", " bst.insert(8)\n",
" assert_equal(insert(node, 3).data, 3)\n", " bst.insert(1)\n",
" in_order_traversal(node, self.results.add_result)\n", " bst.insert(3)\n",
" in_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n", " assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
" self.results.clear_results()\n", " self.results.clear_results()\n",
"\n", "\n",
" node = insert(None, 1)\n", " def test_tree_two(self):\n",
" assert_equal(insert(node, 2).data, 2)\n", " bst = Bst()\n",
" assert_equal(insert(node, 3).data, 3)\n", " bst.insert(1)\n",
" assert_equal(insert(node, 4).data, 4)\n", " bst.insert(2)\n",
" insert(node, 5)\n", " bst.insert(3)\n",
" in_order_traversal(node, self.results.add_result)\n", " bst.insert(4)\n",
" bst.insert(5)\n",
" in_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n", " assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
"\n", "\n",
" print('Success: test_tree')\n", " print('Success: test_tree')\n",
@ -179,7 +184,8 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestTree()\n", " test = TestTree()\n",
" test.test_tree()\n", " test.test_tree_one()\n",
" test.test_tree_two()\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@ -127,25 +127,37 @@
" return str(self.data)\n", " return str(self.data)\n",
"\n", "\n",
"\n", "\n",
"def insert(root, data):\n", "class Bst(object):\n",
" # Constraint: Assume we are working with valid ints\n", "\n",
" if root is None:\n", " def __init__(self, root=None):\n",
" root = Node(data)\n", " self.root = root\n",
" return root\n", "\n",
" if data <= root.data:\n", " def insert(self, data):\n",
" if root.left is None:\n", " if data is None:\n",
" root.left = insert(root.left, data)\n", " raise Exception('Data cannot be None')\n",
" root.left.parent = root\n", " if self.root is None:\n",
" return root.left\n", " self.root = Node(data)\n",
" return self.root\n",
" return self._insert(self.root, data)\n",
"\n",
" def _insert(self, node, data):\n",
" # Constraint: Assume we are working with valid ints\n",
" if node is None:\n",
" return Node(data)\n",
" if data <= node.data:\n",
" if node.left is None:\n",
" node.left = self._insert(node.left, data)\n",
" node.left.parent = node\n",
" return node.left\n",
" else:\n",
" return self._insert(node.left, data)\n",
" else:\n", " else:\n",
" return insert(root.left, data)\n", " if node.right is None:\n",
" else:\n", " node.right = self._insert(node.right, data)\n",
" if root.right is None:\n", " node.right.parent = node\n",
" root.right = insert(root.right, data)\n", " return node.right\n",
" root.right.parent = root\n", " else:\n",
" return root.right\n", " return self._insert(node.right, data)"
" else:\n",
" return insert(root.right, data)"
] ]
}, },
{ {
@ -213,22 +225,25 @@
" def __init__(self):\n", " def __init__(self):\n",
" self.results = Results()\n", " self.results = Results()\n",
"\n", "\n",
" def test_tree(self):\n", " def test_tree_one(self):\n",
" node = Node(5)\n", " bst = Bst()\n",
" assert_equal(insert(node, 2).data, 2)\n", " bst.insert(5)\n",
" assert_equal(insert(node, 8).data, 8)\n", " bst.insert(2)\n",
" assert_equal(insert(node, 1).data, 1)\n", " bst.insert(8)\n",
" assert_equal(insert(node, 3).data, 3)\n", " bst.insert(1)\n",
" in_order_traversal(node, self.results.add_result)\n", " bst.insert(3)\n",
" in_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n", " assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
" self.results.clear_results()\n", " self.results.clear_results()\n",
"\n", "\n",
" node = insert(None, 1)\n", " def test_tree_two(self):\n",
" assert_equal(insert(node, 2).data, 2)\n", " bst = Bst()\n",
" assert_equal(insert(node, 3).data, 3)\n", " bst.insert(1)\n",
" assert_equal(insert(node, 4).data, 4)\n", " bst.insert(2)\n",
" insert(node, 5)\n", " bst.insert(3)\n",
" in_order_traversal(node, self.results.add_result)\n", " bst.insert(4)\n",
" bst.insert(5)\n",
" in_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n", " assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
"\n", "\n",
" print('Success: test_tree')\n", " print('Success: test_tree')\n",
@ -236,7 +251,8 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestTree()\n", " test = TestTree()\n",
" test.test_tree()\n", " test.test_tree_one()\n",
" test.test_tree_two()\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@ -6,22 +6,25 @@ class TestTree(object):
def __init__(self): def __init__(self):
self.results = Results() self.results = Results()
def test_tree(self): def test_tree_one(self):
node = Node(5) bst = Bst()
assert_equal(insert(node, 2).data, 2) bst.insert(5)
assert_equal(insert(node, 8).data, 8) bst.insert(2)
assert_equal(insert(node, 1).data, 1) bst.insert(8)
assert_equal(insert(node, 3).data, 3) bst.insert(1)
in_order_traversal(node, self.results.add_result) bst.insert(3)
in_order_traversal(bst.root, self.results.add_result)
assert_equal(str(self.results), '[1, 2, 3, 5, 8]') assert_equal(str(self.results), '[1, 2, 3, 5, 8]')
self.results.clear_results() self.results.clear_results()
node = insert(None, 1) def test_tree_two(self):
assert_equal(insert(node, 2).data, 2) bst = Bst()
assert_equal(insert(node, 3).data, 3) bst.insert(1)
assert_equal(insert(node, 4).data, 4) bst.insert(2)
insert(node, 5) bst.insert(3)
in_order_traversal(node, self.results.add_result) bst.insert(4)
bst.insert(5)
in_order_traversal(bst.root, self.results.add_result)
assert_equal(str(self.results), '[1, 2, 3, 4, 5]') assert_equal(str(self.results), '[1, 2, 3, 4, 5]')
print('Success: test_tree') print('Success: test_tree')
@ -29,7 +32,8 @@ class TestTree(object):
def main(): def main():
test = TestTree() test = TestTree()
test.test_tree() test.test_tree_one()
test.test_tree_two()
if __name__ == '__main__': if __name__ == '__main__':