def addNodeBst(arr, start, end): if end < start: return None mid = (start + end) // 2 temp = N.Node(arr[mid]) temp.left = addNodeBst(arr, start, mid - 1) temp.right = addNodeBst(arr, mid + 1, end) return temp
return True else: return wrapper(temp1.left,temp2) or wrapper(temp1.right,temp2) def subTree(temp1,temp2): if temp2 is None: return True return wrapper(temp1,temp2) T1 = Basics.Tree() T1.root = Basics.Node(1) T1.addNode(2) T1.addNode(3) T1.addNode(4) T1.addNode(5) T1.addNode(6) T1.addNode(7) T1.addNode(8) T1.addNode(9) T1.addNode(10) T1.addNode(11) T1.addNode(12) T1.addNode(13) T1.addNode(14) T1.addNode(15) T1.addNode(16)
return None temp = {} def utility(node,height,hd): if node is None: return None if hd not in temp: temp[hd] = [node.value,height] else: x = temp[hd][1] if x>height: temp[hd] = [node.value,height] utility(node.left,height+1,hd-1) utility(node.right,height+1,hd+1) utility(root,0,0) for i in sorted(temp.keys()): print(temp[i][0]) T=Basics.Tree() T.root = Basics.Node(1) T.addNode(2) T.addNode(3) T.addNode(4) T.addNode(5) T.addNode(6) T.addNode(7) T.root.left.left.right= Basics.Node(10) T.root.left.left.right.right = Basics.Node(11) T.root.left.left.right.right.right = Basics.Node(12) Solve(T.root)
root = root.right else: print(root.value, end = ' ') res.append(root.value) root = None T = Basics.Tree() T.root = Basics.Node(1) T.addNode(2) T.addNode(3) T.addNode(4) T.addNode(5) T.addNode(6) T.addNode(7) T.root.left.right.left = Basics.Node(8) # T.postorder(T.root) print("-"*50) iterPostorder2(T.root) print("-"*50)
return if st[depth]: st[depth] = add(st[depth], root.value) else: st[depth] = LinkedNode(root.value) if root.left: createLL(root.left, st, depth + 1) if root.right: createLL(root.right, st, depth + 1) return st T = Basics.Tree() T.root = Basics.Node(1) T.addNode(2) T.addNode(3) T.addNode(4) T.addNode(5) T.addNode(6) T.addNode(7) T.addNode(8) T.addNode(9) height = T.height(T.root) stack = [None] * height res = createLL(T.root, stack, 0) for temp in res: # temp = i while temp:
dl = printkdistancenode(root.left, target, k) if dl != -1: if dl + 1 == k: print(root.value) else: printkdistancedown(root.right, k - dl - 2) return 1 + dl dr = printkdistancenode(root.right, target, k) if dr != -1: if dr + 1 == k: print(root.value) else: printkdistancedown(root.left, k - dr - 2) return 1 + dr return -1 T = Basics.Tree() T.root = Basics.Node(20) T.addNode(8) T.addNode(22) T.addNode(4) T.addNode(12) T.root.left.right.left = Basics.Node(10) T.root.left.right.right = Basics.Node(14) T.inorder(T.root) print("-" * 40) printkdistancenode(T.root, T.root.right, 3)
def __init__(self): self.temp1 = None self.temp2 = None self.prev = Basics.Node(float('-inf'))