Exemplo n.º 1
0
            node = que.get()
            if node.left :
                que.put(node.left)
            if node.right :
                que.put(node.right)

            node.left, node.right = node.right, node.left

def _create_mirror(node):
    """
    Recursive implementation
    :param node:
    :return:
    """
    if not node :
        return None
    else :
        _create_mirror(node.left)
        _create_mirror(node.right)
        node.left,node.right = node.right, node.left



if __name__=='__main__':
    tree = BinaryTree()
    for i in range(1,10):
        tree.add_node(i)
    create_mirror(tree.root)
    tree.print_levelorder()
    _create_mirror(tree.root)
    tree.print_levelorder()
            while node and node.data != item :
                stac.append(node)
                node = node.left

            if node and node.data == item :
                break

            if stac[-1].right == None :
                node = stac.pop()

                while len(stac) > 0 and stac[-1].right == node :
                    node = stac.pop()

            node = None if len(stac) == 0 else stac[-1].right


        while len(stac) > 0 :
            node = stac.pop()
            print(node.data)




if __name__=='__main__':
    bt = BinaryTree()
    for i in range(1,10):
        bt.add_node(i)
    print_ancestors(bt.root,8)

    ancestors(bt.root,8)
        return True
    return False


def check_foldable(node):
    if not node:
        return True
    else:
        create_mirror(node.left)
        result = check_identical_structure(node.left, node.right)
        create_mirror(node.left)
        return result


if __name__ == '__main__':
    ll = BinaryTree()
    ll.add_node(10)
    ll.add_node(7)
    ll.add_node(15)
    ll.add_node(None)
    ll.add_node(9)
    ll.add_node(11)
    ll.add_node(None)
    print(check_foldable(ll.root))

    ll2 = BinaryTree()
    ll2.add_node(10)
    ll2.add_node(7)
    ll2.add_node(15)
    ll2.add_node(9)
    ll2.add_node(None)
from GeeksForGeeks.Tree.BinaryTree import BinaryTree, Node
"""
Double Tree
Write a program that converts a given tree to its Double tree. To create Double tree of the given tree,
create a new duplicate for each node, and insert the duplicate as the left child of the original node.


"""


def double_tree(node):
    if not node:
        return
    else:
        double_tree(node.left)
        double_tree(node.right)

        new_node = Node(node.data)
        left = node.left
        node.left = new_node
        new_node.left = left


if __name__ == '__main__':
    bt = BinaryTree()
    for i in range(1, 4):
        bt.add_node(i)

    double_tree(bt.root)
    bt.print_levelorder()

def print_level(node, level, itr):
    if not node:
        return
    if level == 1:
        print(node.data)
    else:
        if not itr:
            print_level(node.left, level - 1, itr)
            print_level(node.right, level - 1, itr)
        else:
            print_level(node.right, level - 1, itr)
            print_level(node.left, level - 1, itr)


def level_order(root, ht, spiral):
    flag = False if spiral else True
    for i in range(1, ht + 1):
        print_level(root, i, flag)
        flag = not flag if spiral else flag


if __name__ == '__main__':
    bt = BinaryTree()
    for i in range(1, 20):
        bt.add_node(i)
    ht = bt._height(bt.root)
    level_order(bt.root, ht, True)
    #print_level(bt.root,4)
Exemplo n.º 6
0
from GeeksForGeeks.Tree.BinaryTree import BinaryTree
from GeeksForGeeks.Tree.CheckIndentical import check_identical

def check_subtree(node1,node2):
    if not node2 and not node1 :
        return True
    elif not node1 and node2  :
        return False
    elif not node2 and node1  :
        return False
    else :

        if node1.data != node2.data :
            return check_subtree(node1,node2.left) and check_subtree(node1,node2.right)
        else :
            return check_identical(node1,node2)

if __name__=='__main__':
    bt = BinaryTree()
    
    else:
        if not node.left and node.right:
            return check_sumtree(node.right)
        elif not node.right and node.left:
            return check_sumtree(node.left)
        elif not node.left and not node.right:
            return node.data
        else:
            a = check_sumtree(node.left)
            b = check_sumtree(node.right)
            if a > 0 and b > 0 and node.data == a + b:
                return 2 * node.data
            else:
                return -1


def check(node):
    return True if check_sumtree(node) > 0 else False


if __name__ == '__main__':
    bt = BinaryTree()
    bt.add_node(32)
    bt.add_node(10)
    bt.add_node(6)
    bt.add_node(4)
    bt.add_node(6)
    bt.add_node(8)
    bt.add_node(3)
    print(check(bt.root))