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 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) 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) root2 = BinaryTreeNode(100) root2.left = root print(Tree(root2)) print(get_max_path_leaves(root2))
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 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))
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)) mirror(root) print(Tree(root))
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 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 _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