from Tree import BinarySearchTree from P4_3 import miniHeightBST def createLevelLinkedList(root, lists, level): if root == None: return None if len(lists) == level: linkedlist = [] lists.append(linkedlist) else: linkedlist = lists[level] linkedlist.append(root.key) createLevelLinkedList(root.leftChild, lists, level + 1) createLevelLinkedList(root.rightChild, lists, level + 1) return lists T4 = BinarySearchTree() i = [1, 2, 3, 4, 5, 6] T4.root = miniHeightBST(i, 0, len(i) - 1) T4.root.printSubtree(5) lists = [] result = createLevelLinkedList(T4.root, lists, 0) for i in result: print i
if T1 == None or T2 == None: return False if T1.key != T2.key: return False return Check(T1.leftChild, T2.leftChild) and Check(T1.rightChild, T2.rightChild) # Testing part... T8_0 = BinarySearchTree() T8_1 = BinarySearchTree() T8_2 = BinarySearchTree() print "Tree 0:" list0 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] T8_0.root = miniHeightBST(list0, 0, len(list0) - 1) T8_0.root.printSubtree(5) list1 = [2, 1, 3, 4] for i in list1: T8_1.insertion(i) print "Tree 1: " T8_1.root.printSubtree(5) print "Tree 2:" list2 = [8, 6, 7] for j in list2: T8_2.insertion(j) T8_2.root.printSubtree(5) print "----------Checking-----------"
printPath(path,i,level) findSum(node.leftChild,sum,path,level + 1) findSum(node.rightChild,sum,path,level + 1) path[level] = 0 def printPath(path, start, end): for i in range(start,end+1): print(str(path[i]) + " "), print " " def depth(node): if(node == None): return 0 else: return 1 + max(depth(node.rightChild),depth(node.leftChild)) def findPaths(node, sum): Depth = depth(node) path = [0] * Depth findSum(node,sum, path,0) # Testing Part... list = [-1,-4,2,-1,2,3,-4,5,-6,2,8,9] T9 = miniHeightBST(list,0,len(list) - 1) T9.printSubtree(5) findPaths(T9,5)
node = root # stack.push(node) while(stack.isEmpty() is False or node != None): if(node != None): stack.push(node) node = node.leftChild else: node = stack.pop() list.append(node.key) node = node.rightChild print list return checkOrder(list) def checkOrder(list): while(len(list) != 1): buffer = list.pop() if(buffer < max(list)): return False return True T5 = BinarySearchTree(); list = [1,2,3,4,5] T5.root = miniHeightBST(list,0,len(list) - 1) T5.root.printSubtree(5) print (checkBST(T5.root) and "True") or "False"