Esempio n. 1
0
def deserialize(arr, i):
    """Deserialize the binary tree"""
    if arr[i[0]] is None:
        i[0] = i[0] + 1
        return None
    node = Node(arr[i[0]])
    i[0] = i[0] + 1
    node.left = deserialize(arr, i)
    node.right = deserialize(arr, i)
    return node
def bst(start, end, arr):
    """ Convert given sorted array to balanced BST"""

    if end < start:
        return

    mid = (start + end) / 2
    mid = int(mid)

    newnode = Node(arr[mid])
    newnode.left = bst(start, mid - 1, arr)
    newnode.right = bst(mid + 1, end, arr)

    return newnode
from trees.utils import Node, pre_order_traversal


def sum_tree(node):
    """ Convert the given binary tree into sum tree"""
    if node is None:
        return 0

    left_sum = sum_tree(node.left)
    right_sum = sum_tree(node.right)
    previous = node.data
    node.data = left_sum + right_sum

    return node.data + previous


if __name__ == '__main__':
    root = Node(10)
    root.left = Node(-2)
    root.right = Node(6)
    root.left.left = Node(8)
    root.left.right = Node(-4)
    root.right.left = Node(7)
    root.right.right = Node(5)
    sum_tree(root)
    pre_order_traversal(root)
Esempio n. 4
0
    """ Check whether tree is a sub-tree of given binary subtree """
    if tree is None:
        return 0
    if tree.data == subtree.data:
        if identical_tree(tree, subtree) == 1:
            print('subtree is present in given binary tree')
            return 1
    return bt_subtree_approach1(tree.left, subtree) or bt_subtree_approach1(
        tree.right, subtree)


def bt_subtree_approach(tree, subtree):
    pass


if __name__ == '__main__':
    T = Node(26)
    T.right = Node(3)
    T.right.right = Node(3)
    T.left = Node(10)
    T.left.left = Node(4)
    T.left.left.right = Node(30)
    T.left.right = Node(6)

    S = Node(10)
    S.right = Node(6)
    S.left = Node(4)
    S.left.right = Node(30)

    bt_subtree_approach1(T, S)
from trees.utils import Node
from collections import defaultdict


def diagonal_traversal(node, level, diagonal):
    """ Diagonal traversal of binary tree """
    if node is None:
        return None
    diagonal[level].append(node.data)

    diagonal_traversal(node.left, level + 1, diagonal)
    diagonal_traversal(node.right, level, diagonal)

    return diagonal


if __name__ == '__main__':
    root = Node(8)
    root.left = Node(3)
    root.right = Node(10)
    root.left.left = Node(1)
    root.left.right = Node(6)
    root.right.right = Node(14)
    root.right.right.left = Node(13)
    root.left.right.left = Node(4)
    root.left.right.right = Node(7)
    diagonal = defaultdict(list)
    diagonal_traversal(root, 0, diagonal)
    print(diagonal)
Esempio n. 6
0
    elif (key > root.data):
        root.right = delete_node(root.right, key)

    else:
        if root.left is None:
            temp = root.right
            return temp

        elif root.right is None:
            temp = root.left
            return temp

        temp = min_value_node(root.right)
        root.data = temp.data
        root.right = delete_node(root.right, temp.data)

    return root


if __name__ == '__main__':
    root = Node(50)
    root.left = Node(30)
    root.right = Node(70)
    root.left.left = Node(20)
    root.left.right = Node(40)
    root.right.left = Node(60)
    root.right.right = Node(80)
    #insert_node(root, 90)
    #search(root, 80)
    delete_node(root, 20)
    pre_order_traversal(root)
            if queue[0].right is not None:
                queue.append(queue[0].right)
            pop_node = queue.pop(0)
            if not flag and i == 0:
                print(pop_node.data)
            elif flag and i == count_node - 1:
                print(pop_node.data)
            i = i + 1
        flag = not flag


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.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.right.left = Node(14)
    root.right.right.right = Node(15)

    root.left.left.left.left = Node(16)
    root.left.left.left.right = Node(17)
    root.right.right.right.right = Node(31)
Esempio n. 8
0
    while len(stack2) > 0:
        node = stack2.pop(0)
        sum += node.data
        if node.left is not None:
            stack1.append(node.left)
        if node.right is not None:
            stack1.append(node.right)

    if sum != 0:
        product = product * sum

    if len(stack2) == 0 and len(stack1) == 0:
        print(product)

    if len(stack1) > 0 or len(stack2) > 0:
        return product_sum(stack1, stack2, node, product)


if __name__ == '__main__':
    stack1 = []
    stack2 = []
    product = 1

    root = Node(2)
    stack1.append(root)
    root.left = Node(7)
    root.right = Node(5)
    root.left.right = Node(6)
    root.left.left = Node(8)

    product_sum(stack1, stack2, root, 1)
def children_sum(node):
    """ children sum property """
    if node.left is None and node.right is None:
        return
    else:
        children_sum(node.left)
        children_sum(node.right)
        if node.left is not None:
            left_data = node.left.data
        if node.right is not None:
            right_data = node.right.data
        diff = left_data + right_data - node.data
        if diff > 0:
            node.data = node.data + diff
        else:
            increment_child(node, -diff)

    return node


if __name__ == '__main__':
    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)
    node = children_sum(root)
    pre_order_traversal(node)
Esempio n. 10
0
    if node is None:
        return 0
    if node.left is None and node.right is None:
        return node.data

    left_exp = evaluate_expression_tree(node.left)
    right_exp = evaluate_expression_tree(node.right)

    if node.data == '+':
        return left_exp + right_exp

    if node.data == '-':
        return left_exp - right_exp

    if node.data == '*':
        return left_exp * right_exp

    if node.data == '/':
        return left_exp / right_exp


if __name__ == '__main__':
    root = Node('+')
    root.left = Node('*')
    root.left.left = Node(5)
    root.left.right = Node(4)
    root.right = Node('-')
    root.right.left = Node(100)
    root.right.right = Node(20)
    print(evaluate_expression_tree(root))