コード例 #1
0
        return True
    else:
        l_val = r_val = 0
        if node.left:
            l_val = node.left.data
        if node.right:
            r_val = node.right.data
        if (l_val + r_val == node.data) and check_children_sum(
                node.left) and check_children_sum(node.right):
            return True
        else:
            return False


if __name__ == '__main__':
    bt = BinaryTree()
    bt.add_node(10)
    bt.add_node(8)
    bt.add_node(2)
    bt.add_node(3)
    bt.add_node(5)
    bt.add_node(2)
    #bt.print_levelorder()

    print(check_children_sum(bt.root))

    bt2 = BinaryTree()
    for i in range(1, 10):
        bt2.add_node(i)
    print(check_children_sum(bt2.root))
コード例 #2
0
def convert_sumtree(node):
    if not node:
        return 0
    else:
        old_val = node.data
        node.data = convert_sumtree(node.left) + convert_sumtree(node.right)
        return node.data + old_val


def print_preorder(node):
    if not node:
        return
    print(node.data)
    print_preorder(node.left)
    print_preorder(node.right)


if __name__ == '__main__':
    bt = BinaryTree()
    bt.add_node(10)
    bt.add_node(-2)
    bt.add_node(6)
    bt.add_node(8)
    bt.add_node(-4)
    bt.add_node(7)
    bt.add_node(5)

    convert_sumtree(bt.root)
    print_preorder(bt.root)
コード例 #3
0

def r2l(node, path, paths):
    if not node:
        return
    else:
        path.append(node.data)

        if not node.right and not node.left:
            paths.append(copy.deepcopy(path))

        if node.left:
            r2l(node.left, path, paths)
            path.pop()

        if node.right:
            r2l(node.right, path, paths)
            path.pop()
        return paths


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

    path = []
    paths = []
    r2l(bt.root, path, paths)
    print(paths)
コード例 #4
0
while traversing we need to compare data and children of the trees.
"""


def check_identical(node1, node2):
    if not node1 and not node2:
        return True
    if not node1 and node2 or not node2 and node1:
        return False
    if node1.data != node2.data:
        return False
    else:
        return (node1.data == node2.data) and check_identical(
            node1.left, node2.left) and check_identical(
                node1.right, node2.right)


if __name__ == '__main__':
    tree1 = BinaryTree()
    for i in range(1, 10):
        tree1.add_node(i)
    tree2 = BinaryTree()
    for i in range(1, 10):
        tree2.add_node(i)
    tree = BinaryTree()
    for i in range(1, 15):
        tree.add_node(i)

    print(check_identical(tree1.root, tree2.root))
    print(check_identical(tree.root, tree1.root))
    print(check_identical(tree2.root, tree.root))
コード例 #5
0
        diff = ldata + rdata - node.data
        if diff > 0 :
            node.data = node.data + diff
        elif diff < 0 :
            increment(node.left,-diff)

def increment(node,diff):
    if not node :
        return
    else :
        node.data = node.data + diff
        if node.left :
            increment(node.left,diff)
        elif node.right :
            increment(node.right,diff)

if __name__=='__main__':
    bt = BinaryTree()
    bt.add_node(50)
    bt.add_node(7)
    bt.add_node(2)
    bt.add_node(3)
    bt.add_node(5)
    bt.add_node(1)
    bt.add_node(30)

    convert(bt.root)
    bt.print_levelorder()

コード例 #6
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()
コード例 #7
0
    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)
    ll2.add_node(None)
コード例 #8
0
    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))