def BalancedBST(a,start,end): #IMPORTANT: exit condition > (not == or <) if start > end: return None mid = start + (end-start)//2 node = BST.BSTNode(a[mid]) node.left = BalancedBST(a,start,mid-1) node.right = BalancedBST(a,mid+1,end) return node
elif node.data >= anode.data and node.data >= bnode.data: return node.data #case 3: node a and b are on the right subtree elif node.data <= anode.data and node.data <= bnode.data: return node.data #case 4: go left elif node.data >= anode.data and node.data >= bnode.data: node = node.left #case 5: go right else: node = node.right a = [3, 2, 1, 6, 5, 4, 7] #Create BST root = BST.BSTNode(a[0]) for i in range(1, len(a)): node = BST.BSTNode(a[i]) BST.insert(root, node) print("In order traversal >>") BinaryTreeTraversal.inOrder(root) print("") node1 = BST.BSTNode(2) node2 = BST.BSTNode(4) print("LCA for nodes with values %d & %d = %d" % (node1.data, node2.data, findLCA(root, node1, node2))) print("LCA for nodes with values %d & %d = %d" % (node1.data, node2.data, findLCA2(root, node1, node2)))
def insert(self, key, value=None): node = BST.BSTNode(key, value) if self.isEmpty(): self.root = node else: self.root = AVL_TREE.insert(self.root, node)
parent.left = node.right #cleanup(node) deleteNode(node.right,parent,target_min,target_max) elif node.data > target_max: parent.right = node.left #cleanup(node) deleteNode(node.left,parent,target_min,target_max) else: deleteNode(node.left,node,target_min,target_max) deleteNode(node.right,node,target_min,target_max) #def cleanup(node): #remove all references #Initialize BST root = BST.BSTNode(6) input = [-13,14,-8,13,7,15] for x in input: BST.insert(root, BST.BSTNode(x)) print("pre order traversal >>>") BinaryTreeTraversal.preOrder(root) print("") target_min = -10 target_max = 13 deleteNode(root,None,target_min,target_max) print("pre order traversal after removing nodes outside range %d - %d >>>" % (target_min,target_max)) BinaryTreeTraversal.preOrder(root)
#Largest Kth value in BST import sys sys.path.append("./mylib") import BST import BinaryTreeTraversal a = [3, 2, 1, 6, 5, 4, 7] #Create BST root = BST.BSTNode(a[0]) for i in range(1, len(a)): node = BST.BSTNode(a[i]) BST.insert(root, node) #Reverse in order traversal to get values in descending order def largestK(node, K): if node and K > 0: K = largestK(node.right, K) if K > 0: print(node.data) K = K - 1 K = largestK(node.left, K) #Important: return K return K else: #Important: return K return K print("In order traversal >>")
#Question: Delete a node from Binary Search Tree(BST) import sys sys.path.append("./mylib") import BinaryTreeTraversal import BST a = [3, 2, 1, 5, 5, 4, 7] #Create BST root = BST.BSTNode(a[0]) for i in range(1, len(a)): node = BST.BSTNode(a[i]) BST.insert(root, node) print("Tree in-order traversal:") BinaryTreeTraversal.inOrder(root) #Time Complexity: O(logn), since only branch of a tree is processed #Delete node from BST print("\ndeleting node.data=", 3) root = BST.delete(root, BST.BSTNode(3)) print("Tree in-order traversal:") BinaryTreeTraversal.inOrder(root) print("")
def next(self): next_smallest_node = self.nodes.pop() self.size -= 1 #IMPORTANT: check node right sub-tree self.fillNodes(next_smallest_node.right) return next_smallest_node.data #Time complexity:O(1) def hasNext(self): return (self.size > 0) #Create BST root = BST.BSTNode(4) BST.insert(root, BST.BSTNode(3)) BST.insert(root, BST.BSTNode(6)) BST.insert(root, BST.BSTNode(2)) BST.insert(root, BST.BSTNode(5)) BST.insert(root, BST.BSTNode(7)) print("In order traversal >>") BinaryTreeTraversal.inOrder(root) print("") bstI = BSTIterator(root) while bstI.hasNext(): print("next smallest >>>", bstI.next())
#Solution 2 #in-order traversal and list to store smallest K elements #Time complexity: O(n) #Space complexity: O(k) def KSmallest(root,k,mylist): if (len(mylist) < k and root.left is not None): KSmallest(root.left, k, mylist) if (len(mylist) < k): mylist.append(root.data) if (len(mylist) < k and root.right is not None): KSmallest(root.right, k, mylist) #Create BST root = BST.BSTNode(4) input = [3,2,1,5,6,7] for x in input: BST.insertNode(root, BST.BSTNode(x)) #Smallest K # mylist = [] # for x in range(1,len(input)+2): # KSmallest(root,x,mylist) # print("smallest element (k=%d) = %d" % (x,mylist.pop())) # del mylist[:] #Solution 3 #Reference: http://stackoverflow.com/questions/2329171/find-kth-smallest-element-in-a-binary-search-tree-in-optimum-way #Algorithm: #Step 1: Augment BST to store #nodes in its left subtree including current node
def insert(self, key, value=None): newNode = BST.BSTNode(key, value) if self.isEmpty(): self.root = newNode else: BST.insert(self.root, newNode)