def test(): btree = BinaryTreeNode(5) btree.setLeft(3) btree.setRight(9) btree.left.setLeft(1) btree.left.setRight(4) btree.right.setLeft(7) btree.right.setRight(10) print isBST1(btree) # True print isBST2(btree) # True
def main(): givenSum = 7 root = BinaryTreeNode(1) root.setLeft(2) root.setRight(3) root.left.setLeft(4) root.left.setRight(5) root.right.setLeft(3) root.right.setRight(4) root.left.right.setLeft(7) findSum(root, givenSum)
def main(): T1 = BinaryTreeNode(1) T1.setLeft(2) T1.setRight(3) T1.left.setLeft(4) T1.left.setRight(2) T1.right.setRight(2) T1.left.right.setLeft(8) T2 = BinaryTreeNode(2) T2.setLeft(4) T2.setRight(2) T2.right.setLeft(8) print containTree(T1, None) # True print containTree(None, T2) # False print containTree(T1, T2) # True print containTree(T1, BinaryTreeNode(4)) # True
def test(): n5 = BinaryTreeNode(5) n5.setLeft(3) n3 = n5.left n5.setRight(7) n7 = n5.right n3.setLeft(1) n1 = n3.left n3.setRight(4) n4 = n3.right n7.setRight(8) n8 = n7.right n1.setLeft(0) n0 = n1.left for node in [n5, n3, n7, n1, n4, n8, n0]: next = findNextNode(node) print node.value, "next:", next.value if next is not None else next
def test(): # create a tree bstree = BinaryTreeNode(5) bstree.setLeft(3) bstree.left.setLeft(1) bstree.left.left.setRight(2) bstree.left.setRight(4) bstree.setRight(6) bstree.right.setRight(8) # create a list of linkedlists containing tree nodes in each level result1 = createLevelLinkedlist1(bstree) result2 = createLevelLinkedlist2(bstree) print "Test for method 1:" printTnodeLinkedlistArray(result1) print "Test for method 2:" printTnodeLinkedlistArray(result2)
def test(): root = BinaryTreeNode(10) root.setLeft(5) root.setRight(6) root.left.setLeft(1) root.left.setRight(2) root.right.setLeft(3) root.right.setRight(4) root.left.right.setLeft(7) # test case 1: node 7 and 6 --> Common ancestor: 10 # test case 2: node 2 and 5 --> Common ancestor: 5 # test case 3: node 6 and a node not in the tree --> Common ancestor: None tests = [(root.left.right.left, root.right), (root.left.right, root.left), (root.right, BinaryTreeNode(9))] methods = [commonAncestor_1, commonAncestor_2] for test in tests: for method in methods: if method == commonAncestor_1: ancestor = method(test[0], test[1]) else: ancestor = method(root, test[0], test[1]) print ancestor.value if ancestor is not None else None