def tree(index: int) -> Optional[Node]: if index >= l: return None node = Node(level_order[index]) node.left = tree(2 * index + 1) node.right = tree(2 * index + 2) return node
def construct(min_val: int, max_val: int) -> Optional[Node]: nonlocal index, length if index >= length or preorder[index] < min_val or preorder[ index] > max_val: return None root = Node(preorder[index]) index += 1 root.left = construct(min_val, root.data) root.right = construct(root.data, max_val) return root
def construct_bin_tree(string: str) -> Optional[Node]: length = len(string) if length == 0: return None root = Node(string[0]) if length > 1: left_end_index = get_subtree_string(string, 1, length - 1) root.left = construct_bin_tree(string[2:left_end_index]) if left_end_index < length - 2: root.right = construct_bin_tree(string[left_end_index + 2:length - 1]) return root
def construct_bst(min_value: int = 0, max_value: int = 0) -> Sequence[Optional[Node]]: if min_value > max_value: return [None] root_list: list = [] for i in range(min_value, max_value + 1): left_subtrees = construct_bst(min_value, i - 1) right_subtrees = construct_bst(i + 1, max_value) for left in left_subtrees: for r in right_subtrees: right = r root = Node(i) root.left = left root.right = right root_list.append(root) return root_list
def inorder_dll(tree: Optional[Node]) -> Optional[Node]: if tree: node = Node(tree.data) if tree.left: left = inorder_dll(tree.left) if left: while left.right: left = left.right node.left = left left.right = node if tree.right: right = inorder_dll(tree.right) if right: while right.left: right = right.left node.right = right right.left = node return node return None
def leaves(root: Node): """ Same as inorder traversal """ if root: leaves(root.left) if not (root.left or root.right): print(root.data) leaves(root.right) def boundary_traverse(root: Node): print(root.data) left_boundary(root.left) leaves(root) right_boundary(root.right) if __name__ == "__main__": root_node = Node(1) root_node.left = Node(2) root_node.right = Node(3) root_node.left.left = Node(4) root_node.left.right = Node(5) root_node.right.left = Node(6) root_node.right.right = Node(7) boundary_traverse(root_node)
if right: while right.left: right = right.left node.right = right right.left = node return node return None def print_dll(dll: Node): while dll: print(dll.data) dll = dll.right if __name__ == "__main__": root = Node(10) root.left = Node(12) root.right = Node(15) root.left.left = Node(25) root.left.right = Node(30) root.right.left = Node(36) root = inorder_dll(root) while root.left: root = root.left print_dll(root)
if node.left is not None or node.right is not None: left_sum = children_sum(node.left) right_sum = children_sum(node.right) diff = left_sum + right_sum - node.data if diff > 0: node.data += diff elif diff < 0: increment_children_data(node, -diff) return node.data if __name__ == "__main__": """ 50 7 2 3 5 1 30 """ root = Node(50) root.left = Node(7) root.right = Node(2) root.left.left = Node(3) root.left.right = Node(5) root.right.left = Node(1) root.right.right = Node(30) children_sum(root) inorder(root) print()
if root.left is None and root.right is None: return node_level == level if root.left is None or root.right is None: return False return is_full_and_complete(root.left, node_level + 1) and is_full_and_complete( root.right, node_level + 1 ) temp = root level = 0 while temp and temp.left: temp = temp.left level += 1 return is_full_and_complete(root, 0) if __name__ == "__main__": root = Node(10) root.left = Node(20) root.right = Node(30) root.left.left = Node(40) root.left.right = Node(50) root.right.left = Node(60) root.right.right = Node(70) assert is_perfect(root) == True
""" def construct(node: Optional[Node]) -> None: nonlocal node_sum if node is not None: construct(node.right) node_sum = node_sum + node.data node.data = node_sum - node.data construct(node.left) node_sum: int = 0 construct(root_node) if __name__ == "__main__": root = Node(11) root.left = Node(2) root.right = Node(29) root.left.left = Node(1) root.left.right = Node(7) root.right.left = Node(15) root.right.right = Node(40) root.right.right.left = Node(35) greater_sum(root) inorder(root) print()
direction = not direction head = stack.pop() while stack: node = stack.pop() head.left = node node.right = head head = node return head if __name__ == "__main__": root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) root.left.left.left = Node(8) root.left.left.right = Node(9) root.left.right.left = Node(10) root.left.right.right = Node(11) root.right.left.left = Node(12) root.right.left.right = Node(13) root.right.right.right = Node(14) head = convert_to_dll(root) while head and head.right:
def density(root_node: Node) -> float: def in_order(node: Optional[Node], height: int = 1): nonlocal nodes nonlocal max_height if node: in_order(node.left, height + 1) max_height = max(height, max_height) nodes += 1 in_order(node.right, height + 1) nodes: int = 0 max_height: int = 0 in_order(root_node) print("nodes:", nodes, "; max_height:", max_height) return nodes / max_height if max_height else 0 if __name__ == "__main__": root = Node(1) root.left = Node(2) root.right = Node(3) root.left.right = Node(4) root.right.right = Node(5) print(density(root))