예제 #1
0
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)
예제 #2
0
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)
예제 #3
0
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)
예제 #4
0
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)
    level_spiral_traversal = get_level_order_spiral_traversal(root)
    level_spiral_traversal = [node.data for node in level_spiral_traversal]
    print(level_spiral_traversal)
예제 #6
0
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)
예제 #7
0
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)
예제 #8
0
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)
예제 #9
0
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
예제 #10
0
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 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
예제 #12
0
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))
예제 #13
0
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    print(Tree(root))
예제 #14
0
def main():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    print(Tree(root))
    print((get_lca(root, 20, 10)))