mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
implements print tree method
This commit is contained in:
@@ -82,8 +82,27 @@ class BinarySearchTree():
|
||||
"""
|
||||
Prints a nice version of the binary search node
|
||||
"""
|
||||
#TODO
|
||||
return self.root_node.__str__()
|
||||
max_iter = 1000000000 # max number of nodes
|
||||
ite = 0
|
||||
ret = ""
|
||||
node = self.root_node
|
||||
|
||||
top = False # dictates when to climb one level
|
||||
while(ite < max_iter):
|
||||
while (node.has_left_child()):
|
||||
node = node.left_child
|
||||
top = True
|
||||
ret += str(node.value) + " "
|
||||
if top: # we need to climb one level
|
||||
node = node.parent
|
||||
top = False
|
||||
ret += str(node.value) + " "
|
||||
if (not node.has_right_child()):
|
||||
return ret.strip() # removes superflous spaces
|
||||
else:
|
||||
node = node.right_child
|
||||
ite += 1
|
||||
return None # problem
|
||||
|
||||
@staticmethod
|
||||
def is_search_tree(a_tree_root):
|
||||
|
||||
@@ -186,7 +186,7 @@ class test_binary_search_tree(unittest.TestCase):
|
||||
self.assertEqual(bst.root_node.right_child.value, 20)
|
||||
self.assertFalse(BinarySearchTree.is_search_tree(bst.root_node))
|
||||
|
||||
def test_print(self):
|
||||
def test_print_node(self):
|
||||
bst = BinarySearchTree()
|
||||
|
||||
self.assertEqual(bst.min(), None)
|
||||
@@ -206,11 +206,33 @@ class test_binary_search_tree(unittest.TestCase):
|
||||
# \
|
||||
# 16
|
||||
|
||||
self.assertEqual(bst.__str__(), "4 6 10")
|
||||
#self.assertEqual(bst.__str__(), "4 6 10")
|
||||
|
||||
self.assertEqual(bst.root_node.__str__(), "4 6 10")
|
||||
self.assertEqual(bst.root_node.right_child.__str__(), "10 16")
|
||||
self.assertEqual(bst.root_node.right_child.right_child.__str__(), "16")
|
||||
|
||||
def test_print_tree(self):
|
||||
bst = BinarySearchTree()
|
||||
|
||||
self.assertEqual(bst.min(), None)
|
||||
|
||||
node_val1 = 6
|
||||
bst.add(node_val1)
|
||||
node_val2 = 4
|
||||
bst.add(node_val2)
|
||||
node_val3 = 10
|
||||
bst.add(node_val3)
|
||||
node_val4 = 16
|
||||
bst.add(node_val4)
|
||||
|
||||
# 6
|
||||
# / \
|
||||
# 4 10
|
||||
# \
|
||||
# 16
|
||||
|
||||
self.assertEqual(bst.__str__(), "4 6 10 16")
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user