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)
        while rtree.left:
            rtree = rtree.left
        rtree.left = root
        root.right = rtree

    return root


def convert_to_bt_dll_approach2(root):
    pass


def convert_to_bt_dll_approach3(root):
    pass


if __name__ == '__main__':
    root = Node(1)
    root.left = Node(2)
    root.left.left = Node(3)
    root.left.right = Node(4)
    convert_to_bt_dll_approach1(root)








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
    """ 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)
Esempio n. 7
0
    """ Convert given bst to array"""
    if node is None:
        return
    bst_array(node.left, arr)
    arr.append(node)
    bst_array(node.right, arr)
    return arr


def array_balanced_bst(arr, start, end):
    """ Convert given array of nodes to balanced BST """
    if start > end:
        return
    mid = int((start + end) / 2)
    node = arr[mid]

    node.left = array_balanced_bst(arr, start, mid - 1)
    node.right = array_balanced_bst(arr, mid + 1, end)
    return node


if __name__ == '__main__':
    root = Node(10)
    root.left = Node(8)
    root.left.left = Node(7)
    root.left.left.left = Node(6)
    root.left.left.left.left = Node(5)

    arr = bst_array(root, [])
    node = array_balanced_bst(arr, 0, len(arr) - 1)
    in_order_traversal(node)
from trees.utils import Node


def count_no_of_nodes(node, size):
    """Checks removing an edge from binary tree can divide it into two halfs"""
    if node is None:
        return False
    count = count_no_of_nodes(node.left, size) + 1 + count_no_of_nodes(node.right, size)

    if count == size - count:
        print('1')

    return count


if __name__ == '__main__':
    root = Node(5)
    root.left = Node(1)
    root.right = Node(6)
    root.left.left = Node(3)
    root.right.left = Node(7)
    root.right.right = Node(4)
    count_no_of_nodes(root, 6)
Esempio n. 9
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)
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))