Merge pull request #97 from donnemartin/develop

Move tree challenges and solutions to classes
This commit is contained in:
Donne Martin 2016-08-14 08:31:06 -04:00 committed by GitHub
commit 982580d051
20 changed files with 404 additions and 359 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -90,15 +90,10 @@
},
"outputs": [],
"source": [
"def create_min_bst(array):\n",
" # TODO: Implement me\n",
" # Calls _create_min_bst\n",
" pass \n",
"class MinBst(object):\n",
"\n",
"\n",
"def _create_min_bst(array, start, end):\n",
" # TODO: Implement me\n",
" pass"
" def create_min_bst(self, array):\n",
" # TODO: Implement me"
]
},
{
@ -115,17 +110,6 @@
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run ../tree_height/height.py"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -138,15 +122,24 @@
"from nose.tools import assert_equal\n",
"\n",
"\n",
"def height(node):\n",
" if node is None:\n",
" return 0\n",
" return 1 + max(height(node.left),\n",
" height(node.right))\n",
"\n",
"\n",
"class TestBstMin(object):\n",
"\n",
" def test_bst_min(self):\n",
" min_bst = MinBst()\n",
" array = [0, 1, 2, 3, 4, 5, 6]\n",
" root = create_min_bst(array)\n",
" root = min_bst.create_min_bst(array)\n",
" assert_equal(height(root), 3)\n",
"\n",
" min_bst = MinBst()\n",
" array = [0, 1, 2, 3, 4, 5, 6, 7]\n",
" root = create_min_bst(array)\n",
" root = min_bst.create_min_bst(array)\n",
" assert_equal(height(root), 4)\n",
"\n",
" print('Success: test_bst_min')\n",

View File

@ -102,20 +102,21 @@
"from __future__ import division\n",
"\n",
"\n",
"def create_min_bst(array):\n",
" if array is None:\n",
" return\n",
" return _create_min_bst(array, 0, len(array)-1)\n",
"class MinBst(object):\n",
"\n",
" def create_min_bst(self, array):\n",
" if array is None:\n",
" return\n",
" return self._create_min_bst(array, 0, len(array)-1)\n",
"\n",
"def _create_min_bst(array, start, end):\n",
" if end < start:\n",
" return None\n",
" mid = (start + end) // 2\n",
" node = Node(array[mid])\n",
" node.left = _create_min_bst(array, start, mid-1)\n",
" node.right = _create_min_bst(array, mid+1, end)\n",
" return node"
" def _create_min_bst(self, array, start, end):\n",
" if end < start:\n",
" return None\n",
" mid = (start + end) // 2\n",
" node = Node(array[mid])\n",
" node.left = self._create_min_bst(array, start, mid-1)\n",
" node.right = self._create_min_bst(array, mid+1, end)\n",
" return node"
]
},
{
@ -128,17 +129,6 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run ../tree_height/height.py"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
@ -156,15 +146,24 @@
"from nose.tools import assert_equal\n",
"\n",
"\n",
"def height(node):\n",
" if node is None:\n",
" return 0\n",
" return 1 + max(height(node.left),\n",
" height(node.right))\n",
"\n",
"\n",
"class TestBstMin(object):\n",
"\n",
" def test_bst_min(self):\n",
" min_bst = MinBst()\n",
" array = [0, 1, 2, 3, 4, 5, 6]\n",
" root = create_min_bst(array)\n",
" root = min_bst.create_min_bst(array)\n",
" assert_equal(height(root), 3)\n",
"\n",
" min_bst = MinBst()\n",
" array = [0, 1, 2, 3, 4, 5, 6, 7]\n",
" root = create_min_bst(array)\n",
" root = min_bst.create_min_bst(array)\n",
" assert_equal(height(root), 4)\n",
"\n",
" print('Success: test_bst_min')\n",
@ -181,7 +180,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {
"collapsed": false
},

View File

@ -1,15 +1,24 @@
from nose.tools import assert_equal
def height(node):
if node is None:
return 0
return 1 + max(height(node.left),
height(node.right))
class TestBstMin(object):
def test_bst_min(self):
min_bst = MinBst()
array = [0, 1, 2, 3, 4, 5, 6]
root = create_min_bst(array)
root = min_bst.create_min_bst(array)
assert_equal(height(root), 3)
min_bst = MinBst()
array = [0, 1, 2, 3, 4, 5, 6, 7]
root = create_min_bst(array)
root = min_bst.create_min_bst(array)
assert_equal(height(root), 4)
print('Success: test_bst_min')

View File

@ -105,9 +105,11 @@
},
"outputs": [],
"source": [
"def bst_successor(node):\n",
" # TODO: Implement me\n",
" pass"
"class BstSuccessor(object):\n",
"\n",
" def get_next(self, node):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
@ -141,28 +143,31 @@
"\n",
" @raises(Exception)\n",
" def test_bst_successor_empty(self):\n",
" bst_successor(None)\n",
" bst_successor = BstSuccessor()\n",
" bst_successor.get_next(None)\n",
"\n",
" def test_bst_successor(self):\n",
" nodes = {}\n",
" node = Node(5)\n",
" nodes[5] = node\n",
" nodes[3] = insert(node, 3)\n",
" nodes[8] = insert(node, 8)\n",
" nodes[2] = insert(node, 2)\n",
" nodes[4] = insert(node, 4)\n",
" nodes[6] = insert(node, 6)\n",
" nodes[12] = insert(node, 12)\n",
" nodes[1] = insert(node, 1)\n",
" nodes[7] = insert(node, 7)\n",
" nodes[10] = insert(node, 10)\n",
" nodes[15] = insert(node, 15)\n",
" nodes[9] = insert(node, 9)\n",
" bst = Bst(nodes[5])\n",
" nodes[3] = bst.insert(3)\n",
" nodes[8] = bst.insert(8)\n",
" nodes[2] = bst.insert(2)\n",
" nodes[4] = bst.insert(4)\n",
" nodes[6] = bst.insert(6)\n",
" nodes[12] = bst.insert(12)\n",
" nodes[1] = bst.insert(1)\n",
" nodes[7] = bst.insert(7)\n",
" nodes[10] = bst.insert(10)\n",
" nodes[15] = bst.insert(15)\n",
" nodes[9] = bst.insert(9)\n",
"\n",
" assert_equal(bst_successor(nodes[4]), 5)\n",
" assert_equal(bst_successor(nodes[5]), 6)\n",
" assert_equal(bst_successor(nodes[8]), 9)\n",
" assert_equal(bst_successor(nodes[15]), None)\n",
" bst_successor = BstSuccessor()\n",
" assert_equal(bst_successor.get_next(nodes[4]), 5)\n",
" assert_equal(bst_successor.get_next(nodes[5]), 6)\n",
" assert_equal(bst_successor.get_next(nodes[8]), 9)\n",
" assert_equal(bst_successor.get_next(nodes[15]), None)\n",
"\n",
" print('Success: test_bst_successor')\n",
"\n",

View File

@ -111,29 +111,28 @@
},
"outputs": [],
"source": [
"def left_most_node(node):\n",
" if node is None:\n",
" return None\n",
" if node.left is None:\n",
"class BstSuccessor(object):\n",
"\n",
" def get_next(self, node):\n",
" if node is None:\n",
" raise Exception('Invalid input node')\n",
" if node.right is not None:\n",
" return self._left_most(node.right)\n",
" return self._next_ancestor(node)\n",
"\n",
" def _left_most(self, node):\n",
" if node.left is not None:\n",
" return self._left_most(node.left)\n",
" return node.data\n",
" return left_most_node(node.left)\n",
"\n",
"\n",
"def bst_successor(node):\n",
" if node is None:\n",
" raise Exception('Invalid input node')\n",
" if node.right is not None:\n",
" return left_most_node(node.right)\n",
" else:\n",
" parent = node.parent\n",
" while parent is not None and parent.left is not node:\n",
" node = node.parent\n",
" parent = node.parent\n",
" if parent is None:\n",
" # We reached the root, the original input node\n",
" # must be the largest element in the tree.\n",
" return None\n",
" return parent.data"
" def _next_ancestor(self, node):\n",
" if node.parent is not None:\n",
" if node.parent.data > node.data:\n",
" return node.parent.data\n",
" return self._next_ancestor(node.parent)\n",
" # We reached the root, the original input node\n",
" # must be the largest element in the tree.\n",
" return None"
]
},
{
@ -168,28 +167,31 @@
"\n",
" @raises(Exception)\n",
" def test_bst_successor_empty(self):\n",
" bst_successor(None)\n",
" bst_successor = BstSuccessor()\n",
" bst_successor.get_next(None)\n",
"\n",
" def test_bst_successor(self):\n",
" nodes = {}\n",
" node = Node(5)\n",
" nodes[5] = node\n",
" nodes[3] = insert(node, 3)\n",
" nodes[8] = insert(node, 8)\n",
" nodes[2] = insert(node, 2)\n",
" nodes[4] = insert(node, 4)\n",
" nodes[6] = insert(node, 6)\n",
" nodes[12] = insert(node, 12)\n",
" nodes[1] = insert(node, 1)\n",
" nodes[7] = insert(node, 7)\n",
" nodes[10] = insert(node, 10)\n",
" nodes[15] = insert(node, 15)\n",
" nodes[9] = insert(node, 9)\n",
" bst = Bst(nodes[5])\n",
" nodes[3] = bst.insert(3)\n",
" nodes[8] = bst.insert(8)\n",
" nodes[2] = bst.insert(2)\n",
" nodes[4] = bst.insert(4)\n",
" nodes[6] = bst.insert(6)\n",
" nodes[12] = bst.insert(12)\n",
" nodes[1] = bst.insert(1)\n",
" nodes[7] = bst.insert(7)\n",
" nodes[10] = bst.insert(10)\n",
" nodes[15] = bst.insert(15)\n",
" nodes[9] = bst.insert(9)\n",
"\n",
" assert_equal(bst_successor(nodes[4]), 5)\n",
" assert_equal(bst_successor(nodes[5]), 6)\n",
" assert_equal(bst_successor(nodes[8]), 9)\n",
" assert_equal(bst_successor(nodes[15]), None)\n",
" bst_successor = BstSuccessor()\n",
" assert_equal(bst_successor.get_next(nodes[4]), 5)\n",
" assert_equal(bst_successor.get_next(nodes[5]), 6)\n",
" assert_equal(bst_successor.get_next(nodes[8]), 9)\n",
" assert_equal(bst_successor.get_next(nodes[15]), None)\n",
"\n",
" print('Success: test_bst_successor')\n",
"\n",

View File

@ -6,28 +6,31 @@ class TestBstSuccessor(object):
@raises(Exception)
def test_bst_successor_empty(self):
bst_successor(None)
bst_successor = BstSuccessor()
bst_successor.get_next(None)
def test_bst_successor(self):
nodes = {}
node = Node(5)
nodes[5] = node
nodes[3] = insert(node, 3)
nodes[8] = insert(node, 8)
nodes[2] = insert(node, 2)
nodes[4] = insert(node, 4)
nodes[6] = insert(node, 6)
nodes[12] = insert(node, 12)
nodes[1] = insert(node, 1)
nodes[7] = insert(node, 7)
nodes[10] = insert(node, 10)
nodes[15] = insert(node, 15)
nodes[9] = insert(node, 9)
bst = Bst(nodes[5])
nodes[3] = bst.insert(3)
nodes[8] = bst.insert(8)
nodes[2] = bst.insert(2)
nodes[4] = bst.insert(4)
nodes[6] = bst.insert(6)
nodes[12] = bst.insert(12)
nodes[1] = bst.insert(1)
nodes[7] = bst.insert(7)
nodes[10] = bst.insert(10)
nodes[15] = bst.insert(15)
nodes[9] = bst.insert(9)
assert_equal(bst_successor(nodes[4]), 5)
assert_equal(bst_successor(nodes[5]), 6)
assert_equal(bst_successor(nodes[8]), 9)
assert_equal(bst_successor(nodes[15]), None)
bst_successor = BstSuccessor()
assert_equal(bst_successor.get_next(nodes[4]), 5)
assert_equal(bst_successor.get_next(nodes[5]), 6)
assert_equal(bst_successor.get_next(nodes[8]), 9)
assert_equal(bst_successor.get_next(nodes[15]), None)
print('Success: test_bst_successor')

View File

@ -87,9 +87,11 @@
},
"outputs": [],
"source": [
"def bfs(self, visit_func):\n",
" # TODO: Implement me\n",
" pass"
"class BstBfs(Bst):\n",
"\n",
" def bfs(self, visit_func):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
@ -128,12 +130,12 @@
" self.results = Results()\n",
"\n",
" def test_bfs(self):\n",
" node = Node(5)\n",
" insert(node, 2)\n",
" insert(node, 8)\n",
" insert(node, 1)\n",
" insert(node, 3)\n",
" bfs(node, self.results.add_result)\n",
" bst = BstBfs(Node(5))\n",
" bst.insert(2)\n",
" bst.insert(8)\n",
" bst.insert(1)\n",
" bst.insert(3)\n",
" bst.bfs(self.results.add_result)\n",
" assert_equal(str(self.results), '[5, 2, 8, 1, 3]')\n",
"\n",
" print('Success: test_bfs')\n",

View File

@ -97,16 +97,20 @@
"from collections import deque\n",
"\n",
"\n",
"def bfs(root, visit_func):\n",
" queue = deque()\n",
" queue.append(root)\n",
" while queue:\n",
" node = queue.popleft()\n",
" visit_func(node)\n",
" if node.left is not None:\n",
" queue.append(node.left)\n",
" if node.right is not None:\n",
" queue.append(node.right)"
"class BstBfs(Bst):\n",
"\n",
" def bfs(self, visit_func):\n",
" if self.root is None:\n",
" raise Exception('root is None')\n",
" queue = deque()\n",
" queue.append(self.root)\n",
" while queue:\n",
" node = queue.popleft()\n",
" visit_func(node)\n",
" if node.left is not None:\n",
" queue.append(node.left)\n",
" if node.right is not None:\n",
" queue.append(node.right)"
]
},
{
@ -153,12 +157,12 @@
" self.results = Results()\n",
"\n",
" def test_bfs(self):\n",
" node = Node(5)\n",
" insert(node, 2)\n",
" insert(node, 8)\n",
" insert(node, 1)\n",
" insert(node, 3)\n",
" bfs(node, self.results.add_result)\n",
" bst = BstBfs(Node(5))\n",
" bst.insert(2)\n",
" bst.insert(8)\n",
" bst.insert(1)\n",
" bst.insert(3)\n",
" bst.bfs(self.results.add_result)\n",
" assert_equal(str(self.results), '[5, 2, 8, 1, 3]')\n",
"\n",
" print('Success: test_bfs')\n",

View File

@ -7,12 +7,12 @@ class TestBfs(object):
self.results = Results()
def test_bfs(self):
node = Node(5)
insert(node, 2)
insert(node, 8)
insert(node, 1)
insert(node, 3)
bfs(node, self.results.add_result)
bst = BstBfs(Node(5))
bst.insert(2)
bst.insert(8)
bst.insert(1)
bst.insert(3)
bst.bfs(self.results.add_result)
assert_equal(str(self.results), '[5, 2, 8, 1, 3]')
print('Success: test_bfs')

View File

@ -99,17 +99,19 @@
},
"outputs": [],
"source": [
"def in_order_traversal(node, visit_func):\n",
"class BstDfs(Bst):\n",
"\n",
" def in_order_traversal(self, node, visit_func):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
"def pre_order_traversal(node, visit_func):\n",
" # TODO: Implement me\n",
" pass\n",
" def pre_order_traversal(self, node, visit_func):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
"def post_order_traversal(node, visit_func):\n",
" # TODO: Implement me\n",
" pass"
" def post_order_traversal(self,node, visit_func):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
@ -148,39 +150,39 @@
" self.results = Results()\n",
"\n",
" def test_dfs(self):\n",
" node = Node(5)\n",
" insert(node, 2)\n",
" insert(node, 8)\n",
" insert(node, 1)\n",
" insert(node, 3)\n",
" bst = BstDfs(Node(5))\n",
" bst.insert(2)\n",
" bst.insert(8)\n",
" bst.insert(1)\n",
" bst.insert(3)\n",
"\n",
" in_order_traversal(node, self.results.add_result)\n",
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 2, 3, 5, 8]\")\n",
" self.results.clear_results()\n",
"\n",
" pre_order_traversal(node, self.results.add_result)\n",
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
" self.results.clear_results()\n",
"\n",
" post_order_traversal(node, self.results.add_result)\n",
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" node = Node(1)\n",
" insert(node, 2)\n",
" insert(node, 3)\n",
" insert(node, 4)\n",
" insert(node, 5)\n",
" bst = BstDfs(Node(1))\n",
" bst.insert(2)\n",
" bst.insert(3)\n",
" bst.insert(4)\n",
" bst.insert(5)\n",
"\n",
" in_order_traversal(node, self.results.add_result)\n",
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" pre_order_traversal(node, self.results.add_result)\n",
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" post_order_traversal(node, self.results.add_result)\n",
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
"\n",
" print('Success: test_dfs')\n",

View File

@ -132,23 +132,25 @@
},
"outputs": [],
"source": [
"def in_order_traversal(node, visit_func):\n",
" if node is not None:\n",
" in_order_traversal(node.left, visit_func)\n",
" visit_func(node)\n",
" in_order_traversal(node.right, visit_func)\n",
"class BstDfs(Bst):\n",
"\n",
"def pre_order_traversal(node, visit_func):\n",
" if node is not None:\n",
" visit_func(node)\n",
" pre_order_traversal(node.left, visit_func)\n",
" pre_order_traversal(node.right, visit_func)\n",
" def in_order_traversal(self, node, visit_func):\n",
" if node is not None:\n",
" self.in_order_traversal(node.left, visit_func)\n",
" visit_func(node)\n",
" self.in_order_traversal(node.right, visit_func)\n",
"\n",
"def post_order_traversal(node, visit_func):\n",
" if node is not None:\n",
" post_order_traversal(node.left, visit_func)\n",
" post_order_traversal(node.right, visit_func)\n",
" visit_func(node)"
" def pre_order_traversal(self, node, visit_func):\n",
" if node is not None:\n",
" visit_func(node)\n",
" self.pre_order_traversal(node.left, visit_func)\n",
" self.pre_order_traversal(node.right, visit_func)\n",
"\n",
" def post_order_traversal(self, node, visit_func):\n",
" if node is not None:\n",
" self.post_order_traversal(node.left, visit_func)\n",
" self.post_order_traversal(node.right, visit_func)\n",
" visit_func(node)"
]
},
{
@ -195,39 +197,39 @@
" self.results = Results()\n",
"\n",
" def test_dfs(self):\n",
" node = Node(5)\n",
" insert(node, 2)\n",
" insert(node, 8)\n",
" insert(node, 1)\n",
" insert(node, 3)\n",
" bst = BstDfs(Node(5))\n",
" bst.insert(2)\n",
" bst.insert(8)\n",
" bst.insert(1)\n",
" bst.insert(3)\n",
"\n",
" in_order_traversal(node, self.results.add_result)\n",
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 2, 3, 5, 8]\")\n",
" self.results.clear_results()\n",
"\n",
" pre_order_traversal(node, self.results.add_result)\n",
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
" self.results.clear_results()\n",
"\n",
" post_order_traversal(node, self.results.add_result)\n",
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" node = Node(1)\n",
" insert(node, 2)\n",
" insert(node, 3)\n",
" insert(node, 4)\n",
" insert(node, 5)\n",
" bst = BstDfs(Node(1))\n",
" bst.insert(2)\n",
" bst.insert(3)\n",
" bst.insert(4)\n",
" bst.insert(5)\n",
"\n",
" in_order_traversal(node, self.results.add_result)\n",
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" pre_order_traversal(node, self.results.add_result)\n",
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
" self.results.clear_results()\n",
"\n",
" post_order_traversal(node, self.results.add_result)\n",
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
" assert_equal(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
"\n",
" print('Success: test_dfs')\n",

View File

@ -7,39 +7,39 @@ class TestDfs(object):
self.results = Results()
def test_dfs(self):
node = Node(5)
insert(node, 2)
insert(node, 8)
insert(node, 1)
insert(node, 3)
bst = BstDfs(Node(5))
bst.insert(2)
bst.insert(8)
bst.insert(1)
bst.insert(3)
in_order_traversal(node, self.results.add_result)
bst.in_order_traversal(bst.root, self.results.add_result)
assert_equal(str(self.results), "[1, 2, 3, 5, 8]")
self.results.clear_results()
pre_order_traversal(node, self.results.add_result)
bst.pre_order_traversal(bst.root, self.results.add_result)
assert_equal(str(self.results), "[5, 2, 1, 3, 8]")
self.results.clear_results()
post_order_traversal(node, self.results.add_result)
bst.post_order_traversal(bst.root, self.results.add_result)
assert_equal(str(self.results), "[1, 3, 2, 8, 5]")
self.results.clear_results()
node = Node(1)
insert(node, 2)
insert(node, 3)
insert(node, 4)
insert(node, 5)
bst = BstDfs(Node(1))
bst.insert(2)
bst.insert(3)
bst.insert(4)
bst.insert(5)
in_order_traversal(node, self.results.add_result)
bst.in_order_traversal(bst.root, self.results.add_result)
assert_equal(str(self.results), "[1, 2, 3, 4, 5]")
self.results.clear_results()
pre_order_traversal(node, self.results.add_result)
bst.pre_order_traversal(bst.root, self.results.add_result)
assert_equal(str(self.results), "[1, 2, 3, 4, 5]")
self.results.clear_results()
post_order_traversal(node, self.results.add_result)
bst.post_order_traversal(bst.root, self.results.add_result)
assert_equal(str(self.results), "[5, 4, 3, 2, 1]")
print('Success: test_dfs')

View File

@ -1,5 +1,7 @@
def height(node):
if node is None:
return 0
return 1 + max(height(node.left),
height(node.right))
class BstHeight(Bst):
def height(self, node):
if node is None:
return 0
return 1 + max(self.height(node.left),
self.height(node.right))

View File

@ -88,9 +88,11 @@
},
"outputs": [],
"source": [
"def height(node):\n",
" # TODO: Implement me\n",
" pass"
"class BstHeight(Bst):\n",
"\n",
" def height(self, node):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
@ -122,13 +124,13 @@
"class TestHeight(object):\n",
"\n",
" def test_height(self):\n",
" root = Node(5)\n",
" assert_equal(height(root), 1)\n",
" insert(root, 2)\n",
" insert(root, 8)\n",
" insert(root, 1)\n",
" insert(root, 3)\n",
" assert_equal(height(root), 3)\n",
" bst = BstHeight(Node(5))\n",
" assert_equal(bst.height(bst.root), 1)\n",
" bst.insert(2)\n",
" bst.insert(8)\n",
" bst.insert(1)\n",
" bst.insert(3)\n",
" assert_equal(bst.height(bst.root), 3)\n",
"\n",
" print('Success: test_height')\n",
"\n",

View File

@ -91,33 +91,15 @@
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting height.py\n"
]
}
],
"source": [
"%%writefile height.py\n",
"def height(node):\n",
" if node is None:\n",
" return 0\n",
" return 1 + max(height(node.left), \n",
" height(node.right))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run height.py"
"class BstHeight(Bst):\n",
"\n",
" def height(self, node):\n",
" if node is None:\n",
" return 0\n",
" return 1 + max(self.height(node.left),\n",
" self.height(node.right))"
]
},
{
@ -129,7 +111,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {
"collapsed": false
},
@ -150,13 +132,13 @@
"class TestHeight(object):\n",
"\n",
" def test_height(self):\n",
" root = Node(5)\n",
" assert_equal(height(root), 1)\n",
" insert(root, 2)\n",
" insert(root, 8)\n",
" insert(root, 1)\n",
" insert(root, 3)\n",
" assert_equal(height(root), 3)\n",
" bst = BstHeight(Node(5))\n",
" assert_equal(bst.height(bst.root), 1)\n",
" bst.insert(2)\n",
" bst.insert(8)\n",
" bst.insert(1)\n",
" bst.insert(3)\n",
" assert_equal(bst.height(bst.root), 3)\n",
"\n",
" print('Success: test_height')\n",
"\n",
@ -172,7 +154,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {
"collapsed": false
},

View File

@ -4,13 +4,13 @@ from nose.tools import assert_equal
class TestHeight(object):
def test_height(self):
root = Node(5)
assert_equal(height(root), 1)
insert(root, 2)
insert(root, 8)
insert(root, 1)
insert(root, 3)
assert_equal(height(root), 3)
bst = BstHeight(Node(5))
assert_equal(bst.height(bst.root), 1)
bst.insert(2)
bst.insert(8)
bst.insert(1)
bst.insert(3)
assert_equal(bst.height(bst.root), 3)
print('Success: test_height')