Пример #1
0
def run():
    bst = BuildLinkedBinarySearchTree(auto_populate=True).get_tree()

    print('Before replacement: ')
    Traversal(bst).print_inorder_traversal()

    get_tree_with_replaced_nodes(bst)

    print('After replacement: ')
    Traversal(bst).print_inorder_traversal()
Пример #2
0
def run():
    bst = BuildLinkedBinaryTree().get_tree()
    bst._root = bst.get_new_node(1)
    bst._root._left = bst.get_new_node(3)
    bst._root._left._right = bst.get_new_node(2)

    print('Initial', end=' ')
    Traversal(bst).print_inorder_traversal()
    RecoverBST(bst).fix_bst()
    print('After swapping incorrect nodes', end=' ')
    Traversal(bst).print_inorder_traversal()
Пример #3
0
def run():
    # Make Tree
    binary_tree = BuildLinkedBinaryTree(list_of_nodes=[1, 0, 0, 1, 0]).get_tree()

    root_right = binary_tree.right(binary_tree.root())
    binary_tree.add_right_child(root_right, 1)
    binary_tree.add_right_child(binary_tree.right(root_right), 1)
    root_left = binary_tree.left(binary_tree.root())
    binary_tree.add_left_child(binary_tree.left(root_left), 1)
    root_left_right = binary_tree.right(root_left)
    binary_tree.add_right_child(root_left_right, 0)
    binary_tree.add_left_child(root_left_right, 1)
    binary_tree.add_left_child(binary_tree.left(root_left_right), 1)
    binary_tree.add_left_child(binary_tree.right(root_left_right), 0)
    binary_tree.add_left_child(binary_tree.left(binary_tree.right(root_left_right)), 1)

    # Traversal(binary_tree).print_level_order_traversal()
    #############################################

    equal_01_subtrees = get_equal_0_1_subtrees(binary_tree)
    if equal_01_subtrees:
        print(f'Yes, {len(equal_01_subtrees)} subtrees having equal 0s and 1s nodes are present')
        for subtree in equal_01_subtrees:
            Traversal(binary_tree).print_level_order_traversal(subtree)
    else:
        print('No subtree having equal 0s 1s is present')
Пример #4
0
def run():
    # binary_tree = BuildLinkedBinaryTree(auto_populate=True).get_tree()
    binary_tree = BuildLinkedBinarySearchTree(auto_populate=True).get_tree()
    print('Given tree ', end='')

    Traversal(binary_tree).print_inorder_traversal()
    print('Given binary-tree is {}bst'.format(
        '' if is_bt_bst(binary_tree) else 'not '))
Пример #5
0
def run():
    bst = BuildLinkedBinarySearchTree(auto_populate=True).get_tree()
    node_data = 4
    # node_data = 7
    inorder_successor_node = get_bst_inorder_successor(bst, node_data=node_data)

    Traversal(bst).print_inorder_traversal()
    if inorder_successor_node:
        print(f'In-order successor of the node({node_data}): ', inorder_successor_node.data)
    else:
        print(f'In order successor of given node({node_data}) doesn\'t exists')
Пример #6
0
def run():
    binary_tree = BuildLinkedBinaryTree(auto_populate=True,
                                        list_of_nodes=[60, 70, 80, 90,
                                                       100]).get_tree()
    node_data = 60
    inorder_successor_node = get_bt_inorder_successor(binary_tree,
                                                      node_data=node_data)

    Traversal(binary_tree).print_inorder_traversal()
    if inorder_successor_node:
        print(f'In-order successor of the node({node_data}): ',
              inorder_successor_node.data)
    else:
        print(f'In order successor of given node({node_data}) doesn\'t exists')
Пример #7
0
def run():
    from trees.utils import Traversal
    binary_tree = BuildLinkedBinaryTree(list_of_nodes=[
        60,
        70,
        80,
        90,
        100,
    ],
                                        auto_populate=True).get_tree()
    Traversal(binary_tree).print_level_order_traversal()

    print('ZigZag Traversal of above tree is: ')
    append_right_first = False
    for node, level_bool in zigzag_traversal(
            binary_tree=binary_tree, append_right_first=append_right_first):
        if level_bool != append_right_first:
            print('\n', '=' * 15, sep='')
            append_right_first = level_bool
        print(node.data, end=' ')
Пример #8
0
def run():
    node_1 = 40
    node_2 = 60
    binary_tree = BuildLinkedBinaryTree(list_of_nodes=[
        60,
        70,
        80,
        90,
        100,
    ],
                                        auto_populate=True).get_tree()

    Traversal(binary_tree).print_inorder_traversal()
    distance = get_dist_bw_nodes(binary_tree, node_1, node_2)

    if distance:
        print(f'Distance b/w nodes {node_2} and {node_1} is: {distance}')
    else:
        print(
            f'One of given or both nodes {node_1} and {node_2}, do not exists in given tree'
        )