def test_pathsWithSums(): from minimalTree import createTree, lp2 from printTree import printTree from random import randint from collections import defaultdict N, M = 0, 400 values = sorted([randint(0, 50) for x in range(N, M)]) tree = createTree(values, lp2(M - N) - 1, lp2(M - N) / 2) #printTree(tree) paths = [] target = 52 pathsWithSums(target, tree, [], [], paths) print len(paths) v1 = len(paths) #for x in paths: #assert sum(x) == target #print x, sum(x) pathCount = [0] target = 52 pathsWithSums2(target, tree, [0], defaultdict(lambda: []), pathCount) print pathCount[0], '\n' v2 = pathCount[0] assert v1 == v2
def test_getRandomNode(): from minimalTree import createTree, lp2 from printTree import printTree N, M = 0, 7 tree = createTree(range(N, M), lp2(M-N) - 1, lp2(M-N)/2) printTree(tree) root = [None] def bfs(nn): from Queue import Queue toVisit = Queue() toVisit.put(nn) while not toVisit.empty(): curr = toVisit.get() if not root[0]: root[0] = Node(curr.data) else: root[0].insert(curr.data) if curr.left: toVisit.put(curr.left) if curr.right: toVisit.put(curr.right) bfs(tree) bt = root[0] print [bt.getRandomNode().data for x in range(20)]
def test_getFCA(): from minimalTree import createTree, lp2 N, M = 0, 41 tree = createTree(range(N, M), lp2(M - N) - 1, lp2(M - N) / 2) assert getFCA(tree.left.right.right, tree.right.left.left.right).data == 31 assert getFCA(tree.left.right.right, tree.left.left.left.right).data == 15
def test_isSubtree(): from minimalTree import createTree, lp2 from printTree import printTree N, M = 0, 41 tree1 = createTree(range(N, M), lp2(M - N) - 1, lp2(M - N) / 2) #printTree(tree1) N, M = 0, 7 tree2 = createTree(range(N, M), lp2(M - N) - 1, lp2(M - N) / 2) #printTree(tree2) assert isSubtree(tree1, tree2)[0] == True N, M = 0, 8 tree2 = createTree(range(N, M), lp2(M - N) - 1, lp2(M - N) / 2) #printTree(tree2) assert isSubtree(tree1, tree2)[0] == False
def test_recurBFS(): from minimalTree import createTree, lp2 from printTree import printTree N, M = 0, 7 tree = createTree(range(N, M), lp2(M - N) - 1, lp2(M - N) / 2) #printTree(tree) #print '\n' inputs = [] recurBFS([], set([tree]), inputs) #from pprint import pprint as pp #pp(inputs) #print '\n%s' % len(inputs) assert len(inputs) == 80
else: toVisit.append(" "*mnl) if node.right: toVisit.append(node.right) else: toVisit.append(" "*mnl) return nLists def getNodeNames(tree): '''returns a set of all node names from the tree''' data = set() def dfs(node, height): '''depth first search for getting node names''' data.add(str(node.data)) if node.left: dfs(node.left, height) if node.right: dfs(node.right, height) dfs(tree, 1) return data if __name__ == '__main__': N, M = 0, 41 from minimalTree import lp2, createTree print 'max power of 2 : %s' % lp2(M-N) tree1 = createTree(range(N, M), lp2(M-N) - 1, lp2(M-N)/2) printTree(tree1)
from llist import sllist class Listsolution: answerList = [] def __init__(self, height): self.answerList = [None] * height def sol(self, node, level): if (node == None): return 0 else: if (self.answerList[level] == None): self.answerList[level] = sllist() self.answerList[level].append(node.data) self.sol(node.left, level + 1) self.sol(node.right, level + 1) # from tree import BST if __name__ == '__main__': obj = createTree() rootNode = obj.solution([1, 2, 3, 4, 5, 6, 7, 8]) # print("Height of tree ",rootNode.getHeightOfTree(rootNode.root)) solutionObj = Listsolution(rootNode.getHeightOfTree(rootNode.root)) solutionObj.sol(rootNode.root, 0) print(solutionObj.answerList)