def create_bst(lst, start, end): """ Create a BST with minimal height Args: lst(List): A sorted array with unique integer elements start(int): Starting index end(int): Last index Returns: Integer/None: Value of root of each subtree """ if end < start: return None mid = (start + end) // 2 root = BinaryTree(lst[mid]) root.left_child = create_bst(lst, start, mid - 1) root.right_child = create_bst(lst, mid + 1, end) # post-order traversal print(root.get_root_val()) return root
Checks if a binary tree is a BST Args: root(BinaryTree): The root of the tree min(int/None) max(int/None) Returns: Boolean: Indicates whether a BST or not """ if root is None: return True # base case if ((min is not None and root.get_root_val() <= min) or (max is not None and root.get_root_val() > max)): return False if (not check_bst(root.left_child, min, root.get_root_val()) or not check_bst(root.right_child, root.get_root_val(), max)): return False return True r = BinaryTree(5) r.insert_left(3) r.insert_right(7) r.get_left_child().insert_left(1) r.get_left_child().insert_right(4) print(check(r))
lst = [] # if list element with index = depth does not exist if len(lists) == level: lists.append(lst) # if list element with index = depth does exist else: lst = lists[level] lst.append(root.get_root_val()) create_level_linked_list(root.left_child, lists, level + 1) create_level_linked_list(root.right_child, lists, level + 1) def create_levels(root): lists = [] create_level_linked_list(root, lists, 0) return lists r = BinaryTree('a') r.insert_left('b') r.insert_right('c') r.get_left_child().insert_left('d') r.get_left_child().insert_right('e') r.get_right_child().insert_left('f') r.get_right_child().insert_right('g') new_lists = create_levels(r) print(new_lists)
return -1 else: return max(left_height, right_height) + 1 def is_balanced(root): """ Check if tree is balanced root(BinaryTree): The root of the tree Returns: Boolean: Indicates if tree is balanced """ if check_height(root) == -1: return False else: return True r = BinaryTree('a') print(r.get_root_val()) print(r.get_left_child()) r.insert_left('b') print(r.get_left_child()) print(r.get_left_child().get_root_val()) r.insert_right('c') print(r.get_right_child()) print(r.get_right_child().get_root_val()) r.get_right_child().set_root_val('hello') print(r.get_right_child().get_root_val()) print(is_balanced(r))
return Result(root, is_ancestor) else: return Result(rx.node if rx.node is not None else ry.node, False) def common_ancestor(root, p, q): """ Main function to run Args: root(BinaryTree): root of the tree p(BinaryTree): the first node q(BinaryTree): the second node Returns: BinaryTree/None: A binary tree node if common ancestor exists else None """ r = common_ancestor_helper(root, p, q) if r.isAncestor: return r.node return None r = BinaryTree(10) r.insert_left(7) r.insert_right(15) r.left_child.insert_left(6) r.left_child.insert_right(8) r.left_child.right_child.insert_right(9) r.right_child.insert_left(13) r.right_child.insert_right(17) print(common_ancestor(r, r.left_child, r.left_child.left_child).get_root_val())
""" # If both are empty if n1 is None and n2 is None: return True # If one but not both are empty if n1 is None or n2 is None: return False # If values don't match if n1.get_root_val() != n2.get_root_val(): return False return match_tree(n1.left_child, n2.left_child) and\ match_tree(n1.right_child, n2.right_child) # T1 tree1 = BinaryTree(10) tree1.insert_left(7) tree1.insert_right(15) tree1.left_child.insert_left(6) tree1.left_child.insert_right(8) tree1.left_child.right_child.insert_right(9) tree1.right_child.insert_left(13) tree1.right_child.insert_right(17) # T2 tree2 = BinaryTree(7) tree2.insert_left(6) tree2.insert_right(8) tree2.right_child.insert_right(9) print(contains_tree(tree1, tree2))
if temp == _sum: print_path(path, i, level) find_sum(node.left_child, _sum, path, level+1) find_sum(node.right_child, _sum, path, level+1) path[level] = -sys.maxsize - 1 def main(node, _sum): """ Main function to call Args: node(BinaryTree): The node to begin calculating _sum(Integer): The given sum """ d = depth(node) path = [0]*d find_sum(node, _sum, path, 0) tree1 = BinaryTree(10) tree1.insert_left(7) tree1.insert_right(15) tree1.left_child.insert_left(6) tree1.left_child.insert_right(8) tree1.left_child.right_child.insert_right(9) tree1.right_child.insert_left(13) tree1.right_child.insert_right(17) main(tree1, 25)