def convert_to_BST_n(self, nu, head): #import ipdb; ipdb.set_trace() if (nu<=0): return None left = self.convert_to_BST_n(nu/2, head) node = Tree(head.contents) head = head.next node.left = left right = self.convert_to_BST_n(nu-nu/2-1, head) node.right = right return node
def insert_in_bst(root, element): if root == None: root = Tree(element) elif root.data > element: #move left if root.left == None: root.left = Tree(element) else: insert_in_bst(root.left, element) elif root.data < element: #move right if root.right == None: root.right = Tree(element) else: insert_in_bst(root.right, element) return root
def insert_in_bst_iterative(root, element): temp = root if root == None: root = Tree(element) return root else: while root != None: if root.data > element: if root.left: root = root.left else: root.left = Tree(element) break elif root.data < element: if root.right: root = root.right else: root.right = Tree(element) break return temp
def convert_to_bst(self, start, end): self.count +=1 #print "count: ", self.count rootNode = None if (start>end): return None else: mid =(start+end)/2 # mid =(start+(end-start))/2 #print "left : " ,start, mid, self.array[mid] leftNode = Tree(self.convert_to_bst(start, mid-1)) #leftNode.print_node() #print "mid: " ,self.array[mid] rootNode = Tree(self.array[mid]) rootNode.left = leftNode #rootNode.print_node() #print "right: ", mid+1, end, self.array[mid] rightNode = Tree(self.convert_to_bst(mid+1, end)) rootNode.right = rightNode #rightNode.print_node()z return rootNode