def tree(): ''' 10 0 15 -2 4 18 ''' t = Tree() for n in (10, 15, 0, 4, 18, -2): t.insert(n) return t
def test_height_of_known_height_tree(self): ''' assert that the height of a tree known to be 4 is actually 4 ''' t = Tree() t.add(1) t.add(2) t.add(3) t.add(4) self.assertEqual(t.height(), 4)
def create_three_node_tree(self): ''' create a sample tree which looks like: 23 /\ 15 47 ''' t = Tree() t.add(23) t.add(15) t.add(47) return t
def test_4_node_right_tree(self): ''' another sample tree, this one with only right nodes 13 \ 14 \ 15 ''' t = Tree() t.add(13) t.add(14) t.add(15) self.assertIsNone(t.root.left) self.assertEqual(t.root.value, 13) self.assertEqual(t.root.right.value, 14) self.assertEqual(t.root.right.right.value, 15)
import sys from os import path sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) from binary_search_tree import Tree #from .binary_search_tree import Tree bst = Tree() bst.insert(10) print(bst.insert(15)) bst.preorder() bst.postorder() bst.inorder()
import sys from os import path sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) ) from binary_search_tree import Tree #from .binary_search_tree import Tree bst = Tree() bst.insert(10) print(bst.insert(15)) bst.preorder() bst.postorder() bst.inorder()
def create_sample_tree(self): ''' create a sample tree to use in the traversal implementations ''' t = Tree() t.add(100) t.add(50) t.add(25) t.add(75) t.add(150) t.add(125) t.add(175) t.add(110) return t.root
def test_empty_tree_find_fails(): t = Tree() assert t.find(10) is False
def test_one_node_tree(self): t = Tree() t.add(23) self.assertEqual(t.root.value, 23)
def test_height_of_zero_node_tree(self): ''' assert that the height of a tree with no nodes should be 0 ''' t = Tree() self.assertEqual(t.height(), 0)
if left > root.data: # print("Left greater than root") raise Exception else: left = None if left and right: if right < left: # print("Left greater than right") raise Exception else: return True is_valid(root.right) is_valid(root.left) if __name__ == "__main__": tree = Tree() tree.insert(1) tree.insert(2) tree.insert(10) tree.insert(6) try: is_valid(tree.root) except: print("Tree is invalid") invalid_root = Node(2) right = Node(4) left = Node(5) invalid_root.right = right invalid_root.left = left
#!/usr/bin/env python3 import unittest from binary_search_tree import Tree, InorderIterator, InorderIterator1, TreeNode def insert(tree, lst): for elem in lst: tree.insert(elem) t = unittest.TestCase() # pre_order testcases lst = [100, 50, 200, 25, 125, 350] tree = Tree() insert(tree, lst) t.assertEqual([100, 50, 25, 200, 125, 350], tree.pre_order()) lst = [100] tree = Tree() insert(tree, lst) t.assertEqual([100], tree.pre_order()) lst = [] tree = Tree() insert(tree, lst) t.assertEqual([], tree.pre_order()) lst = [1, 2, 3, 4, 5, 6, 7, 8] tree = Tree()
def test_insert_and_search(self): bst = Tree() bst.insert(5) self.assertEqual(bst.search(5), True) bst.insert(15) self.assertEqual(bst.search(15), True) bst.insert(6) bst.insert(7) self.assertEqual(bst.search(6), True)