Ejemplo n.º 1
0
def get_std_tree():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)

    return root
Ejemplo n.º 2
0
def get_child_sum_tree():
    root = Node(10)
    root.left = Node(8)
    root.right = Node(2)
    root.left.left = Node(3)
    root.left.right = Node(5)
    root.right.left = Node(2)

    return root
Ejemplo n.º 3
0
def get_std_tree():
    r = Node(1)
    r.left = Node(2)
    r.right = Node(3)
    r.left.left = Node(4)
    r.left.right = Node(5)
    r.right.left = Node(6)
    r.right.right = Node(7)

    return r
def deserialize(string, m):
    global IDX
    if IDX > m:
        return None

    if string[IDX] == '$':
        IDX += 1
        return None

    root = Node(string[IDX])
    IDX += 1

    root.left = deserialize(string, m)
    root.right = deserialize(string, m)

    return root
Ejemplo n.º 5
0
def build_tree_from_po_and_io(po, io, start, end):
    global PO_INDEX
    if start > end:
        return None

    key = po[PO_INDEX]
    node = Node(key)
    PO_INDEX -= 1

    if start == end:
        return node

    in_idx = search(io, start, end, key)

    # start from right
    node.right = build_tree_from_po_and_io(po, io, in_idx + 1, end)
    node.left = build_tree_from_po_and_io(po, io, start, in_idx - 1)

    return node
Ejemplo n.º 6
0
    while root is not None:

        if root.left is None:
            print root,
            root = root.right

        else:
            pre = root.left

            while pre.right is not None and pre.right != root:
                pre = pre.right

            if pre.right is None:
                pre.right = root
                root = root.left

            else:
                pre.right = None
                print root,
                root = root.right


if __name__ == '__main__':
    r = Node(1)
    r.left = Node(2)
    r.right = Node(3)
    r.left.left = Node(4)
    r.left.right = Node(5)
    morris_traversal(r)
    is_found = False

    # to consider non leaf nodes
    if len(s) > 3:
        if hm.get(s):
            is_found = True

        hm[s] = True

    return is_found_left or is_found_right or is_found, s


if __name__ == '__main__':
    r = Node('A')
    r.left = Node('B')
    r.right = Node('C')
    r.left.left = Node('D')
    r.left.right = Node('E')
    r.right.right = Node('B')
    r.right.right.left = Node('D')
    r.right.right.right = Node('E')

    print check_duplicate(r, {})

    # false case
    r = Node('A')
    r.left = Node('B')
    r.right = Node('C')
    r.left.left = Node('D')
    r.left.right = Node('E')
    r.right.right = Node('B')
Ejemplo n.º 8
0
    while not s1.is_empty() or not s2.is_empty():
        while not s1.is_empty():
            node = s1.pop()
            print node,

            if node.left:
                s2.push(node.left)
            if node.right:
                s2.push(node.right)

        while not s2.is_empty():
            node = s2.pop()
            print node,

            if node.right:
                s1.push(node.right)
            if node.left:
                s1.push(node.left)


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)

spiral_print(root)
Ejemplo n.º 9
0
    while m >= 0:
        current_sum += path[m]

        if current_sum == k:
            print_path(path, m, n)

        m -= 1

    path.pop()


if __name__ == '__main__':
    r = Node(1)
    r.left = Node(3)
    r.right = Node(-1)
    r.left.left = Node(2)
    r.left.right = Node(1)
    r.right.left = Node(4)
    r.right.right = Node(5)
    r.left.right.left = Node(1)
    r.right.left.left = Node(1)
    r.right.left.right = Node(2)
    r.right.right.right = Node(6)

    paths_sum_to_k(r, [], 5)
"""
consider every node as a potential root
"""

Ejemplo n.º 10
0
    if l_node is not None and l_level % 2 != 0:
        deepest_odd_level = l_level
        _deepest_odd_level_node = l_node

    if r_node is not None and r_level % 2 != 0 and deepest_odd_level < r_level:
        deepest_odd_level = r_level
        _deepest_odd_level_node = r_node

    return _deepest_odd_level_node, deepest_odd_level


if __name__ == '__main__':
    print ''
    r = Node(5)
    r.left = Node(10)
    r.right = Node(2)
    r.left.left = Node(3)
    r.left.right = Node(4)
    r.right.right = Node(15)
    r.left.right.left = Node(44)
    r.left.right.left.right = Node(12)
    r.right.right.left = Node(9)
    r.right.right.left = Node(8)

    print deepest_odd_level_node(r, 1)

    r = Node(10)
    r.left = Node(28)
    r.right = Node(13)
    r.right.left = Node(14)
    r.right.right = Node(15)
Ejemplo n.º 11
0
def convert_to_mirror(root):
    if root is None:
        return

    left = root.left
    root.left = root.right
    root.right = left

    convert_to_mirror(root.left)
    convert_to_mirror(root.right)


if __name__ == '__main__':
    t = Node(1)
    t.left = Node(2)
    t.right = Node(3)
    t.left.left = Node(4)
    t.left.right = Node(5)
    convert_to_mirror(t)
    print t, t.left, t.right, t.right.left, t.right.right


def iterative_mirror(root):
    q = Queue()
    q.put(root)

    while not q.empty():
        node = q.get()

        l = node.left
        r = node.right
Ejemplo n.º 12
0
def eval_expression_tree(root):
    if is_leaf(root):
        return root.data

    left_val = eval_expression_tree(root.left)
    right_val = eval_expression_tree(root.right)
    op = root.data
    funct = EXPS[op]
    return funct(left_val, right_val)


if __name__ == '__main__':
    print ''
    r = Node('+')
    r.left = Node(3)
    r.right = Node('*')
    r.right.left = Node('+')
    r.right.right = Node(2)
    r.right.left.left = Node(5)
    r.right.left.right = Node(9)

    print eval_expression_tree(r)
"""
amzn

http://www.geeksforgeeks.org/given-a-binary-tree-how-do-you-remove-all-the-half-nodes/
"""


def remove_half_nodes(root):
    if root is None:
Ejemplo n.º 13
0
        if root1.data == root2.data and are_trees_identical(root1, root2):
            return True

        is_found_left = is_subtree(root1.left, root2)
        is_found_right = is_subtree(root1.right, root2)

        return is_found_left or is_found_right

    return False


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

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

    print 'is subtree'
    print is_subtree(root1, root2)
"""
http://www.geeksforgeeks.org/reverse-level-order-traversal/
"""