prepares implementation of flat print method

This commit is contained in:
2013-07-12 21:09:23 +02:00
parent ac878019fd
commit 6c14c68d93
2 changed files with 17 additions and 41 deletions

View File

@@ -78,33 +78,12 @@ class BinarySearchTree():
# we have the leaf
return node.value
# def __str__(self):
# """
# Prints a nice version of the binary search node
# """
# #TODO
# return "aaa"
@staticmethod
def is_search_tree_old(a_tree_root):
def __str__(self):
"""
Returns true of the input binary tree is a valid search binary tree.
Prints a nice version of the binary search node
"""
#TODO
# no check for now, let s imagine we have a correct binary tree.
root_node = a_tree_root
print root_node.value
if (root_node.has_left_child()):
if (root_node.value < root_node.left_child.value): # breaks rules
return False
else:
return BinarySearchTree.is_search_tree(root_node.left_child)
if (root_node.has_right_child()):
if (root_node.value < root_node.right_child.value): # breaks rules
return False
else:
return BinarySearchTree.is_search_tree(root_node.right_child)
return "aaa"
@staticmethod
def is_search_tree(a_tree_root):

View File

@@ -107,23 +107,6 @@ class test_binary_search_tree(unittest.TestCase):
self.assertEqual(bst.min(), node_val2)
def test_print(self):
# No assertion here, I just want a beautifully designed tree.
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)
#print(bst)
def test_is_search_node(self):
bst = BinarySearchTree()
@@ -203,7 +186,21 @@ 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):
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)
self.assertEqual(bst.__str__(), "aaa")
if __name__ == '__main__':
unittest.main()