def reconstruct_tree(preorder, inorder): # TODO - you fill in here. # 1. root is first node in preorder traversal # 2. use root to divide inorder traversal into left and right subtrees (inorder) # 3. use (left,right) inorder traversal set to partition preorder traversal # after root element into (left,right) preorder traversal # print('preorder={},inorder={}'.format(preorder, inorder)) if not preorder: print("returning None") return None # 1. root_value = preorder[0] root = BstNode(root_value) # 2. root_index = inorder.index(root_value) # root_index = element_index[root_value] print('root_index=', root_index) # 3. inorder_left_tree = inorder[:root_index] inorder_right_tree = inorder[root_index + 1:] print('inorder_left_tree={},inorder_right_tree={}'.format( inorder_left_tree, inorder_right_tree)) preorder_left_tree = preorder[1:root_index + 1] preorder_right_tree = preorder[root_index + 1:] print('preorder_left_tree={},preorder_right_tree={}'.format( preorder_left_tree, preorder_right_tree)) # root.left = reconstruct_tree(preorder_left_tree, inorder_left_tree) root.right = reconstruct_tree(preorder_right_tree, inorder_right_tree) print('root={}'.format(root)) return root
def helper(start, end): if end - start < 0: return None mid_idx = (end+start)//2 node = BstNode(A[mid_idx]) node.left = helper(start, mid_idx-1) node.right = helper(mid_idx+1, end) return node
def bst_helper(A, start, end): if end < start: return None middle = (start + end) // 2 node = BstNode(A[middle]) node.left = bst_helper(A, start, middle - 1) node.right = bst_helper(A, middle + 1, end) return node
def build_min_height_bst_from_sorted_array(A: List[int]) -> Optional[BstNode]: root = None if len(A) != 0: middle = len(A) // 2 root = BstNode(A[middle]) left_arr = A[:middle] right_arr = A[middle + 1:] root.left = build_min_height_bst_from_sorted_array(left_arr) root.right = build_min_height_bst_from_sorted_array(right_arr) return root
def rebuild_bst_from_preorder(preorder_sequence): if len(preorder_sequence) == 0: return None root = BstNode(preorder_sequence[0]) i = 1 left = [x for x in preorder_sequence[1:] if x < preorder_sequence[0]] right = [x for x in preorder_sequence[1:] if x > preorder_sequence[0]] root.left = rebuild_bst_from_preorder(left) root.right = rebuild_bst_from_preorder(right) return root
def build_min_height_bst_from_sorted_subarray(start, end): if start > end: return None mid = (start + end) // 2 tree = BstNode(A[mid]) tree.left = build_min_height_bst_from_sorted_subarray(start, mid - 1) tree.right = build_min_height_bst_from_sorted_subarray(mid + 1, end) return tree
def build_tree(A): if not A: return None mid_i = (len(A) - 1) // 2 mid_elem = A[mid_i] node = BstNode(mid_elem) node.left = build_tree(A[:mid_i]) node.right = build_tree(A[mid_i + 1:]) return node
def helper(left_limit, right_limit): nonlocal idx nonlocal arr if idx >= len(arr) or not left_limit < arr[idx] < right_limit: return None node = BstNode(data=arr[idx]) idx += 1 node.left = helper(left_limit, node.data) node.right = helper(node.data, right_limit) return node
def preorder_helper(lower_bound, upper_bound): if root_idx[0] == len(preorder_sequence): return None root = preorder_sequence[root_idx[0]] if not lower_bound <= root <= upper_bound: return None root_idx[0] += 1 node = BstNode(root) ## ordering is critical node.left = preorder_helper(lower_bound, root) node.right = preorder_helper(root, upper_bound) return node
def rebuild_within_range(low=float('-inf'), high=float('inf')): nonlocal i if i >= len(preorder_sequence): return None v = preorder_sequence[i] if low <= v <= high: u = BstNode(v) i += 1 u.left = rebuild_within_range(low=low, high=v) u.right = rebuild_within_range(low=v, high=high) return u return None
def build_min_height_bst_from_sorted_array(A): # TODO - you fill in here. # Alok if not A: return None splitIndex = len(A)//2 root = BstNode(A[splitIndex]) root.left = build_min_height_bst_from_sorted_array(A[:splitIndex]) root.right = build_min_height_bst_from_sorted_array(A[splitIndex+1:]) return root
def construct_tree(root_idx, lower_bound, upper_bound): if root_idx == len(preorder_sequence): return Result(None, root_idx) root_val = preorder_sequence[root_idx] if not lower_bound <= root_val <= upper_bound: return Result(None, root_idx) root_idx += 1 tree = BstNode(root_val) tree.left, root_idx = construct_tree(root_idx, lower_bound, root_val) tree.right, root_idx = construct_tree(root_idx, root_val, upper_bound) return Result(tree, root_idx)
def build_bst(seq, start, end): if start == end: return None node = BstNode(seq[start]) right = start while right < end and seq[right] <= seq[start]: right += 1 node.left = build_bst(seq, start + 1, right) node.right = build_bst(seq, right, end) return node
def build_tree(start, end): if start > end: return None root_val = preorder_sequence[start] right_index = start + 1 while right_index <= end: if preorder_sequence[right_index] > root_val: break right_index += 1 tree = BstNode(root_val) tree.left = build_tree(start + 1, right_index - 1) tree.right = build_tree(right_index, end) return tree
def find_k_largest_in_bst(tree: BstNode, k: int) -> List[int]: #Reverse inorder traversal (sorta) path = [] stack = [tree] while k != 0 and stack: tree = stack.pop() if tree: if tree.left: stack.append(tree.left) if not tree.right: #Current node is the right most value path.append(tree.data) k -= 1 else: #Keeping going right, but save the current node too right = tree.right tree.left, tree.right = None, None #Prune the tree stack.append(tree) stack.append(right) return path
def binary_tree_from_preorder_inorder(preorder, inorder): # for a preorder traversal, the first element is the root, # followed by a set of elements for the left subtree then the right # the set of left subtree elements in the preorder traversal can be determined by # using the root element to divide the inorder traversal elements into # left and right subtree elements, # the left subtree elements in the inorder traversal is the elements that # preceeds the root element in the inorder traversal if not preorder: return None root_val = preorder[0] root = BstNode(root_val) inorder_root_index = inorder.index(root_val) inorder_left_tree = inorder[:inorder_root_index] inorder_right_tree = inorder[inorder_root_index + 1:] preorder_left_tree = preorder[1:(len(inorder_left_tree) + 1)] #skip root preorder_right_tree = preorder[(len(inorder_left_tree) + 1):] root.left = binary_tree_from_preorder_inorder(preorder_left_tree, inorder_left_tree) root.right = binary_tree_from_preorder_inorder(preorder_right_tree, inorder_right_tree) return root