def main(): root = Node(1) root.left = Node(2) root.right = Node(3) right_view = [] get_right_view(root, 0, right_view) print(right_view)
def main(): root = Node(1) root.left = Node(2) root.right = Node(3) xpos_nodes_map = {} top_view = get_top_view(root) print(top_view)
def main(): root = Node(1) root.left = Node(2) root.right = Node(3) vertical_order = get_vertical_order(root) vertical_order = [node.data for node in vertical_order] print(vertical_order)
def main(): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.right.right = Node(5) order = postorder_iterative(root) print(order)
def main(): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.right.right = Node(5) order = [] preorder(root, order) print(order)
def main(): root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.right.right = Node(5) level_spiral_traversal = get_level_order_spiral_traversal(root) level_spiral_traversal = [node.data for node in level_spiral_traversal] print(level_spiral_traversal)
def deserialize_tree(arr, idx=0): ''' idx is the index of the current element to be processed. Returns <root, idx> where idx is the index of the next element to be processed. ''' if idx >= len(arr) or arr[idx] is None: return None, idx + 1 node = Node(arr[idx]) node.left, idx = deserialize_tree(arr, idx + 1) node.right, idx = deserialize_tree(arr, idx) return node, idx
def main(): root = BinaryTreeNode(1) root.left = BinaryTreeNode(2) root.right = BinaryTreeNode(3) root2 = BinaryTreeNode(100) root2.left = root print(Tree(root2)) print(get_max_path_leaves(root2))
def recurse(left, right): global head if left > right: return None mid = (left + right) // 2 left_root = recurse(left, mid - 1) node = BinaryTreeNode(head.value) node.left = left_root # Maintain the invariant head = head.next node.right = recurse(mid + 1, right) return node
def main(): root = Node(20) root.left = Node(8) root.left.left = Node(4) root.left.right = Node(12) root.left.right.left = Node(10) root.left.right.right = Node(14) root.right = Node(22) root.right.right = Node(25) boundary = get_boundary_traversal(root) boundary = [node.data for node in boundary] print(boundary)
def deserialize(in_order, pre_order, in_start, in_end): if deserialize.in_order_index_map is None: build_reverse_map(in_order) if in_start > in_end: return None root = Node(pre_order[deserialize.pre_index]) deserialize.pre_index += 1 if in_start == in_end: return root root_in_index = deserialize.in_order_index_map[root.data] root.left = deserialize(in_order, pre_order, in_start, root_in_index - 1) root.right = deserialize(in_order, pre_order, root_in_index + 1, in_end) return root
def main(): """ Constructed binary tree is 1 / \ 2 3 / \ 4 5 """ root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) ans = morris_inorder(root) print(ans)
def 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.right = Node(101) root.right.left.left = Node(102) print(Tree(root)) root2 = root.left.left ans = is_subtree(root, root2) print(ans)
def 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.right = Node(101) root.right.left.left = Node(102) print(Tree(root)) arr = [] serialize_tree(root, arr) print(arr) root, idx = deserialize_tree(arr) print(Tree(root))
def reverse_inorder(root: Node): nonlocal prev if root is None: return reverse_inorder(root.left) root.right = prev if prev is not None: prev.left = root prev = root reverse_inorder(root.right)
def get_huffman_tree(msg): char_freq = {} for ch in msg: char_freq[ch] = char_freq.get(ch, 0) + 1 heap = [] for ch, freq in char_freq.items(): node = BinaryTreeNode((freq, ch)) heapq.heappush(heap, node) root = None while heap: first = heapq.heappop(heap) if not heap: root = first break second = heapq.heappop(heap) new_freq = first.data[0] + second.data[0] # Non-leaf nodes are not associated with any character! new_node = BinaryTreeNode((new_freq, ""), first, second) heapq.heappush(heap, new_node) return root
def main(): root = BinaryTreeNode(1) root.left = BinaryTreeNode(2) root.right = BinaryTreeNode(3) bottom_view = {} get_bottom_view(root, 0, 0, bottom_view) print(bottom_view)
def main(): root = BinaryTreeNode(1) root.left = BinaryTreeNode(2) root.right = BinaryTreeNode(3) print(Tree(root)) print(get_max_path(root))
def main(): root = BinaryTreeNode(1) root.left = BinaryTreeNode(2) root.right = BinaryTreeNode(3) left_view = [] get_left_view(root, 0, left_view) print(left_view)
def tree_to_dll(root: Node): """ Returns (head, tail) of the dll. First convert left subtree, then right. Then connect head to both of them. """ if root is None: return None, None left_head, left_tail = tree_to_dll(root.left) right_head, right_tail = tree_to_dll(root.right) if left_tail is not None: left_tail.right = root root.left = left_tail if right_head is not None: right_head.left = root root.right = right_head head = left_head if head is None: head = root tail = right_tail if tail is None: tail = root return head, tail
def _build_from_heap_recursive(self, heap_array, root_index): if root_index >= len(heap_array): return None root = BinaryTreeNode(heap_array[root_index]) left_child_index = root_index * 2 + 1 left_child = self._build_from_heap_recursive(heap_array, left_child_index) if left_child: root.set_left_child(left_child) right_child_index = left_child_index + 1 right_child = self._build_from_heap_recursive(heap_array, right_child_index) if right_child: root.set_right_child(right_child) return root
def main(): root = Node(1) root.left = Node(2) root.right = Node(3) print(Tree(root))
def main(): root = BinaryTreeNode(1) root.left = BinaryTreeNode(2) root.right = BinaryTreeNode(3) root.left.left = BinaryTreeNode(4) root.left.right = BinaryTreeNode(5) root.right.left = BinaryTreeNode(6) root.right.right = BinaryTreeNode(7) root.left.left.right = BinaryTreeNode(101) root.right.left.left = BinaryTreeNode(102) print(Tree(root)) print(get_diameter(root))
def mirror(root: BinaryTreeNode): if root is None: return mirror(root.left) mirror(root.right) root.left, root.right = root.right, root.left
def main(): root = BinaryTreeNode(1) root.left = BinaryTreeNode(2) root.right = BinaryTreeNode(3) root.left.left = BinaryTreeNode(4) root.left.right = BinaryTreeNode(5) root.right.left = BinaryTreeNode(6) root.right.right = BinaryTreeNode(7) root.left.left.right = BinaryTreeNode(101) root.right.left.left = BinaryTreeNode(102) print(Tree(root)) mirror(root) print(Tree(root))
def main(): root = Node(1) root.left = Node(2) root.right = Node(3) print(Tree(root)) print((get_lca(root, 20, 10)))
def main(): root = BinaryTreeNode(20) root.left = BinaryTreeNode(10) root.left.left = BinaryTreeNode(5) root.left.left.left = BinaryTreeNode(1) root.left.left.left.right = BinaryTreeNode(50) root = BinaryTreeNode(1) root.left = BinaryTreeNode(3) root.right = BinaryTreeNode(2) print(Tree(root)) print(isBST_direct(root))
def main(): root = BinaryTreeNode(1) root.left = BinaryTreeNode(2) root.right = BinaryTreeNode(3) root.left.left = BinaryTreeNode(4) root.left.right = BinaryTreeNode(5) root.right.left = BinaryTreeNode(6) root.right.right = BinaryTreeNode(7) root.left.left.right = BinaryTreeNode(101) root.right.left.left = BinaryTreeNode(102) print(Tree(root)) print(are_isomorphic(root, root))