Ejemplo n.º 1
0
        if node:
            if node.right:
                right_nodes(node.right)
                print(node.data, end=" ")
            elif node.left:
                right_nodes(node.left)
                print(node.data, end=" ")

    print(root.data, end=" ")
    left_nodes(root.left)
    bottom_nodes(root)
    right_nodes(root.right)


if __name__ == "__main__":
    bst = BinarySearchTree()
    tree_nodes = [20, 8, 4, 12, 10, 14, 22, 25]
    """
    Tree representation of above numbers
                20
               /  \
              8    22
             / \     \
            4  12     25
               / \
              10  14
    
    in-order traversal - 4, 8, 10, 12, 14, 20, 22, 25
    """
    for node_data in tree_nodes:
        bst.root = bst.insert(bst.root, node_data)
        """
        if not root:
            return 0
        return root.data + get_sum(root.left) + get_sum(root.right)

    if not root:
        return True
    l_sum = get_sum(root.left)
    r_sum = get_sum(root.right)
    if (root.left or root.right) and root.data != (l_sum + r_sum):
        return False
    return True


if __name__ == "__main__":
    bst = BinarySearchTree()
    bst.root = Node(26)
    bst.root.left = Node(10)
    bst.root.right = Node(3)
    bst.root.left.left = Node(4)
    bst.root.left.right = Node(6)
    bst.root.right.right = Node(3)
    """
    Tree representation of above numbers
                26
               /  \
              10    3
             / \     \
            4   6     3
    
    in-order traversal - 4, 8, 10, 12, 14, 20, 22, 25