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_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
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
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)
return node.parent node = node.parent return None def getMin(node): '''returns the mininum element under given node/subtree''' curr = node while curr.left: curr = curr.left return curr if __name__ == '__main__': from minimalTree import lp2, createTree from printTree import printTree N, M = 0, 10 tree1 = createTree(range(N, M), lp2(M - N) - 1, lp2(M - N) / 2) printTree(tree1) node1 = getMin(tree1) while 1: nextNode = getSucc(node1) if nextNode is None: break assert nextNode.data == (node1.data + 1) print "%s -> %s" % (node1, nextNode) node1 = nextNode