def test_tree3(self): a = Node(1, None, None) b = Node(2, None, None) c = Node(3, None, None) d = Node(4, None, None) a.left = b b.left = c c.left = d tree3 = Tree(a) assert tree3.printTree(tree3.get_height(a)) == self.answer3
def test_tree(self): first = Node(1, None, None) second = Node(2, None, None) third = Node(3, None, None) fourth = Node(4, None, None) fifth = Node(5, None, None) sixth = Node(6, None, None) seventh = Node(7, None, None) eighth = Node(8, None, None) ninth = Node(9, None, None) tenth = Node(10, None, None) eleventh = Node(11, None, None) twelve = Node(12, None, None) thirteenth = Node(13, None, None) fourteenth = Node(14, None, None) fifteenth = Node(15, None, None) first.left = second first.right = third second.left = fourth second.right = fifth third.left = sixth third.right = seventh fourth.left = eighth fourth.right = ninth fifth.left = tenth fifth.right = eleventh sixth.left = twelve sixth.right = thirteenth seventh.left = fourteenth seventh.right = fifteenth tree = Tree(first) result = np.array([[ '|', '|', '|', '|', '|', '|', '|', '1', '|', '|', '|', '|', '|', '|', '|' ], [ '|', '|', '|', '2', '|', '|', '|', '|', '|', '|', '|', '3', '|', '|', '|' ], [ '|', '4', '|', '|', '|', '5', '|', '|', '|', '6', '|', '|', '|', '7', '|' ], [ '8', '|', '9', '|', '10', '|', '11', '|', '12', '|', '13', '|', '14', '|', '15' ]]) assert np.mean(tree.print_tree(first) == result) > 0
def test3(self): # all branches on the left node1 = Node(2) node2 = Node(4) node3 = Node(6) node4 = Node(1) node1.left = node2 node2.left = node3 node3.left = node4 self.input = Tree.print_tree(node1) self.answer = [[ '|', '|', '|', '|', '|', '|', '|', '2', '|', '|', '|', '|', '|', '|', '|' ], [ '|', '|', '|', '4', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|' ], [ '|', '6', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|' ], [ '1', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|' ]] assert self.input == self.answer
def test_tree4(self): a = Node(1, None, None) b = Node(2, None, None) c = Node(3, None, None) d = Node(4, None, None) e = Node(5, None, None) f = Node(6, None, None) a.left = b a.right = c b.right = d c.left = e e.right = f tree4 = Tree(a) assert tree4.printTree(tree4.get_height(a)) == self.answer4
def test_tree2(self): a = Node(1, None, None) b = Node(2, None, None) c = Node(3, None, None) d = Node(4, None, None) e = Node(5, None, None) f = Node(6, None, None) g = Node(7, None, None) a.left = b a.right = c b.left = d b.right = e c.left = f c.right = g tree2 = Tree(a) assert tree2.printTree(tree2.get_height(a)) == self.answer2
def test2(self): # full balanced tree node1 = Node(2) node2 = Node(4) node3 = Node(6) node4 = Node(1) node5 = Node(3) node6 = Node(5) node7 = Node(7) node1.left = node2 node1.right = node3 node2.left = node4 node2.right = node5 node3.left = node6 node3.right = node7 self.input = Tree.print_tree(node1) self.answer = [['|', '|', '|', '2', '|', '|', '|'], ['|', '4', '|', '|', '|', '6', '|'], ['1', '|', '3', '|', '5', '|', '7']] assert self.input == self.answer
def test4(self): # irregular tree root = Node(1) r11 = Node(2) r12 = Node(3) r21 = Node(4) r22 = Node(5) r23 = Node(6) r31 = Node(7) r32 = Node(8) r33 = Node(9) root.left = r11 root.right = r12 r11.left = r21 r11.right = r22 r12.left = r23 r23.right = r33 r21.right = r31 r22.left = r32 self.input = Tree.print_tree(root) self.answer = [[ '|', '|', '|', '|', '|', '|', '|', '1', '|', '|', '|', '|', '|', '|', '|' ], [ '|', '|', '|', '2', '|', '|', '|', '|', '|', '|', '|', '3', '|', '|', '|' ], [ '|', '4', '|', '|', '|', '5', '|', '|', '|', '6', '|', '|', '|', '|', '|' ], [ '|', '|', '7', '|', '8', '|', '|', '|', '|', '|', '9', '|', '|', '|', '|' ]] assert self.input == self.answer
def test1(self): # single node as tree node1 = Node(0) self.answer = [['0']] self.input = Tree.print_tree(node1) assert self.input == self.answer
def test_tree1(self): a = Node(1, None, None) tree1 = Tree(a) assert tree1.printTree(tree1.get_height(a)) == self.answer1