def generate() -> BinaryTree: tree = BinaryTree() tree.root = Node(randint(1, 1000)) generate_helper(tree.root, 0.7, 0.7) # suggestion: don't use higher values for probability, it will lead to recursion # error return tree
def create_trees(n: int) -> List[BinaryTree]: tree_arr = [] if n == 0: return tree_arr tree = BinaryTree() tree.root = Node(0) tree_arr.append(tree) create_trees_helper(tree_arr, n - 1) return tree_arr
def deserialize(string: str) -> BinaryTree: # the string needs to have the same format as the binary tree serialization # eg: data is padded with single quotes (') and comma (,) is used as a delimiter data = string.split(",") queue = Queue() for node in data: queue.enqueue(node) tree = BinaryTree() tree.root = Node(queue.dequeue().strip("'")) deserialize_helper(tree.root, queue) return tree
def merge_trees(tree1: BinaryTree, tree2: BinaryTree) -> BinaryTree: tree = BinaryTree() if not tree1.root and not tree2.root: return tree # root generation r1, r2 = 0, 0 if tree1.root: r1 = tree1.root.val if tree2.root: r2 = tree2.root.val tree.root = Node(r1 + r2) # generating rest of the tree merge_trees_helper(tree.root, tree1.root, tree2.root) return tree
def generate_tree(preorder: List[int], inorder: List[int]) -> BinaryTree: length = len(preorder) if length != len(inorder): raise RuntimeError if length == 0: return BinaryTree() # generating the root root = preorder[0] tree = BinaryTree() tree.root = Node(root) # generating the rest of the tree if length > 1: i = inorder.index(root) # partitioning the nodes as per the branch inorder_left, preorder_left = (inorder[:i], preorder[1 : i + 1]) inorder_right, preorder_right = (inorder[i + 1 :], preorder[i + 1 :]) # creating a tree for each branch tree_left = generate_tree(preorder_left, inorder_left) tree_right = generate_tree(preorder_right, inorder_right) # attaching the sub-tree to their respective branch tree.root.left = tree_left.root tree.root.right = tree_right.root return tree
get_bottom_view_helper(node.left, depth + 1, hd - 1, accumulator) if node.right: get_bottom_view_helper(node.right, depth + 1, hd + 1, accumulator) return accumulator def get_bottom_view(tree: BinaryTree) -> List[int]: data = get_bottom_view_helper(tree.root, 0, 0, {}) res_arr = [(hd, data[hd][1]) for hd in data] res_arr.sort(key=lambda elem: elem[0]) return [elem for _, elem in res_arr] if __name__ == "__main__": tree = BinaryTree() tree.root = Node(5) tree.root.left = Node(3) tree.root.right = Node(7) tree.root.left.left = Node(1) tree.root.left.right = Node(4) tree.root.right.left = Node(6) tree.root.right.right = Node(9) tree.root.left.left.left = Node(0) tree.root.right.right.left = Node(8) print(tree)
l_max_sum + node.val, r_max_sum + node.val, l_max_sum + node.val + r_max_sum, ) # overall max sum is updated as per requirement overall_max_sum = max(current_max_sum, overall_max_sum) return overall_max_sum def get_max_path_sum(tree: BinaryTree) -> int: return get_max_path_sum_helper(tree.root) if __name__ == "__main__": tree = BinaryTree() tree.root = Node(1) print(tree) print(get_max_path_sum(tree)) tree.root.left = Node(2) print(tree) print(get_max_path_sum(tree)) tree.root.right = Node(3) print(tree) print(get_max_path_sum(tree)) tree.root.val = -1 print(tree) print(get_max_path_sum(tree))
tree1 = BinarySearchTree() tree1.add(5) tree1.add(9) tree1.add(1) tree1.add(4) tree1.add(10) tree1.add(3) tree1.add(2) tree1.add(10) tree1.add(7) print(is_binary_search_tree(tree1)) tree2 = BinaryTree() tree2.root = Node(5) tree2.root.left = Node(4) tree2.root.right = Node(6) print(is_binary_search_tree(tree2)) tree3 = BinaryTree() tree3.root = Node(5) tree3.root.left = Node(6) tree3.root.right = Node(4) print(is_binary_search_tree(tree3)) """ SPECS:
self.right.calculate_helper()) return self.val def calculate_expression_tree(tree: BinaryTree) -> Union[int, float]: root = tree.root if root: root.transform_helper() return root.calculate_helper() return None if __name__ == "__main__": tree = BinaryTree() tree.root = ExpressionTreeNode("*") tree.root.left = ExpressionTreeNode("+") tree.root.right = ExpressionTreeNode("+") tree.root.left.left = ExpressionTreeNode(3) tree.root.left.right = ExpressionTreeNode(2) tree.root.right.left = ExpressionTreeNode(4) tree.root.right.right = ExpressionTreeNode(5) print(calculate_expression_tree(tree)) """ SPECS: TIME COMPLEXITY: O(n) SPACE COMPLEXITY: O(log(n))
return tree # root generation r1, r2 = 0, 0 if tree1.root: r1 = tree1.root.val if tree2.root: r2 = tree2.root.val tree.root = Node(r1 + r2) # generating rest of the tree merge_trees_helper(tree.root, tree1.root, tree2.root) return tree if __name__ == "__main__": tree1 = BinaryTree() tree1.root = Node(1) tree1.root.left = Node(2) tree1.root.right = Node(3) tree1.root.left.right = Node(4) print(tree1) tree2 = BinaryTree() tree2.root = Node(2) tree2.root.right = Node(-3) tree2.root.right.right = Node(10) print(tree2) print(merge_trees(tree1, tree2)) """ SPECS:
return True if self.left: left = self.left._is_any_descendant_unlocked() else: left = False if self.right: right = self.right._is_any_descendant_unlocked() else: right = False return left or right if __name__ == "__main__": tree = BinaryTree() tree.root = NodeWithLock(5) tree.root.left = NodeWithLock(3) tree.root.left.parent = tree.root tree.root.right = NodeWithLock(18) tree.root.right.parent = tree.root tree.root.left.left = NodeWithLock(0) tree.root.left.left.parent = tree.root.left print(tree) print() print(tree.root.left.left.lock()) print(tree.root.left.lock()) print(tree.root.lock())
node.left = node.right.left node.right = node.right.right create_full_bin_tree_helper(node) elif node.left is not None and node.right is not None: create_full_bin_tree_helper(node.left) create_full_bin_tree_helper(node.right) def create_full_bin_tree(tree: BinaryTree) -> None: if tree.root: create_full_bin_tree_helper(tree.root) if __name__ == "__main__": tree = BinaryTree() tree.root = Node("a") tree.root.left = Node("b") tree.root.right = Node("c") tree.root.left.left = Node("d") tree.root.left.left.right = Node("f") tree.root.right.right = Node("e") tree.root.right.right.left = Node("g") tree.root.right.right.right = Node("h") print(tree) create_full_bin_tree(tree)
# the string needs to have the same format as the binary tree serialization # eg: data is padded with single quotes (') and comma (,) is used as a delimiter data = string.split(",") queue = Queue() for node in data: queue.enqueue(node) tree = BinaryTree() tree.root = Node(queue.dequeue().strip("'")) deserialize_helper(tree.root, queue) return tree if __name__ == "__main__": tree = BinaryTree() tree.root = Node("root") tree.root.left = Node("left") tree.root.right = Node("right") tree.root.left.left = Node("left.left") print(serialize(tree)) generated_tree = deserialize( "'root','left','left.left','None','None','None','right','None','None'" ) print(serialize(generated_tree))
if sub_tree1.left and find_helper(sub_tree1.left, sub_tree2): return True if sub_tree1.right and find_helper(sub_tree1.right, sub_tree2): return True return False def get_match(s: BinaryTree, t: BinaryTree) -> bool: if s.root and t.root: return find_helper(s.root, t.root) return False if __name__ == "__main__": tree1 = BinaryTree() tree1.root = Node(0) tree1.root.left = Node(1) tree1.root.right = Node(2) tree1.root.right.left = Node(3) tree1.root.right.right = Node(4) tree2 = BinaryTree() tree2.root = Node(2) tree2.root.left = Node(3) tree2.root.right = Node(4) tree3 = BinaryTree() tree3.root = Node(2) tree3.root.left = Node(3) tree3.root.right = Node(5)
g = Node(700) h = Node(800) a.left = b a.right = c b.left = d b.right = e c.left = f c.right = g d.right = h tree = BinaryTree() tree.root = a print(tree) print(get_level_min_sum(tree)) a.val = 1000 print(tree) print(get_level_min_sum(tree)) b.val = 1500 print(tree) print(get_level_min_sum(tree)) h.val = 2000 print(tree) print(get_level_min_sum(tree)) """ SPECS:
current_balance = -1 <= (right_height - left_height) <= 1 height = max(left_height, right_height) + 1 return height, balance and current_balance def check_balance(tree: BinaryTree) -> bool: if tree.root is None: return True _, balance = height_helper(tree.root) return balance if __name__ == "__main__": tree = BinaryTree() tree.root = Node(0) tree.root.left = Node(1) tree.root.right = Node(2) tree.root.left.left = Node(3) tree.root.left.right = Node(4) print(check_balance(tree)) tree.root.left.right.left = Node(5) print(check_balance(tree)) """ SPECS:
def generate_cartesian_tree(sequence: List[int]) -> BinaryTree: tree = BinaryTree() tree.root = generate_cartesian_tree_helper(sequence) return tree