def test_complete_binary_tree(self): """Complete Binary Tree All levels are completely filled except the last level, which has all keys as left as possible. 18 / \ / \ / \ 15 20 / \ / \ 40 50 16 25 / \ / 8 7 1 """ self.root = BinaryNode(18) self.root.left = BinaryNode(15) self.root.right = BinaryNode(20) self.root.left.left = BinaryNode(40) self.root.left.right = BinaryNode(50) self.root.right.left = BinaryNode(16) self.root.right.right = BinaryNode(25) self.root.left.left.left = BinaryNode(8) self.root.left.left.right = BinaryNode(7) self.root.left.right.left = BinaryNode(1) self.assertEqual(pre_order_traversal(self.root), [18, 15, 40, 8, 7, 50, 1, 20, 16, 25])
def setUp(self): """Valid binary search tree.""" self.root = BinaryNode(18) self.root.left = BinaryNode(15) self.root.right = BinaryNode(20) self.root.left.left = BinaryNode(9) self.root.left.right = BinaryNode(17)
def create_tree(): data = raw_input("please input a char(# for blank): ") if data == "#": return None node = BinaryNode(data) node.lchild = BinaryTree.create_tree() node.rchild = BinaryTree.create_tree() return node
def test_small_valid_bst(self): """Binary Tree 2 / \ 1 5 """ self.root = BinaryNode(2) self.root.left = BinaryNode(1) self.root.right = BinaryNode(5) self.assertTrue(validate_bst(self.root))
def test_invalid_bst(self): """Binary Tree 5 / \ / \ / \ 1 4 / \ 3 6 """ self.root = BinaryNode(5) self.root.left = BinaryNode(1) self.root.right = BinaryNode(4) self.root.right.left = BinaryNode(3) self.root.right.right = BinaryNode(6) self.assertFalse(validate_bst(self.root))
def test_strict_binary_tree(self): """Strict Binary Tree Each node has exactly 0 or 2 children. 18 / \ / \ / \ 15 20 / \ 40 50 """ self.root = BinaryNode(18) self.root.left = BinaryNode(15) self.root.right = BinaryNode(20) self.root.left.left = BinaryNode(40) self.root.left.right = BinaryNode(50) self.assertEqual(pre_order_traversal(self.root), [18, 15, 40, 50, 20])
def setUp(self): """Binary Tree 30 / \ / \ / \ 11 29 / \ / \ 8 12 25 14 """ self.root = BinaryNode(30) self.root.left = BinaryNode(11) self.root.right = BinaryNode(29) self.root.left.left = BinaryNode(8) self.root.left.right = BinaryNode(12) self.root.right.left = BinaryNode(25) self.root.right.right = BinaryNode(14)
def test_large_valid_bst(self): """Binary Tree 8 / \ / \ / \ 6 10 / \ / \ 3 7 9 12 """ self.root = BinaryNode(8) self.root.left = BinaryNode(6) self.root.right = BinaryNode(10) self.root.left.left = BinaryNode(3) self.root.left.right = BinaryNode(7) self.root.right.left = BinaryNode(9) self.root.right.right = BinaryNode(12) self.assertTrue(validate_bst(self.root))
def test_full_binary_tree(self): """Full (Perfect) Binary Tree Each node has exactly 0 or 2 children and all leaf nodes are on the same level. 18 / \ / \ / \ 15 20 / \ / \ 40 50 16 25 """ self.root = BinaryNode(18) self.root.left = BinaryNode(15) self.root.right = BinaryNode(20) self.root.left.left = BinaryNode(40) self.root.left.right = BinaryNode(50) self.root.right.left = BinaryNode(16) self.root.right.right = BinaryNode(25) self.assertEqual(pre_order_traversal(self.root), [18, 15, 40, 50, 20, 16, 25])