def generate() -> BinaryTree:
    tree = BinaryTree()
    tree.root = Node(randint(1, 1000))
    generate_helper(tree.root, 0.7, 0.7)
    # suggestion: don't use higher values for probability, it will lead to recursion
    # error
    return tree
def create_trees(n: int) -> List[BinaryTree]:
    tree_arr = []
    if n == 0:
        return tree_arr
    tree = BinaryTree()
    tree.root = Node(0)
    tree_arr.append(tree)
    create_trees_helper(tree_arr, n - 1)
    return tree_arr
def deserialize(string: str) -> BinaryTree:
    # the string needs to have the same format as the binary tree serialization
    # eg: data is padded with single quotes (') and comma (,) is used as a delimiter
    data = string.split(",")
    queue = Queue()
    for node in data:
        queue.enqueue(node)
    tree = BinaryTree()
    tree.root = Node(queue.dequeue().strip("'"))
    deserialize_helper(tree.root, queue)
    return tree
Пример #4
0
def merge_trees(tree1: BinaryTree, tree2: BinaryTree) -> BinaryTree:
    tree = BinaryTree()
    if not tree1.root and not tree2.root:
        return tree
    # root generation
    r1, r2 = 0, 0
    if tree1.root:
        r1 = tree1.root.val
    if tree2.root:
        r2 = tree2.root.val
    tree.root = Node(r1 + r2)
    # generating rest of the tree
    merge_trees_helper(tree.root, tree1.root, tree2.root)
    return tree
def generate_tree(preorder: List[int], inorder: List[int]) -> BinaryTree:
    length = len(preorder)
    if length != len(inorder):
        raise RuntimeError
    if length == 0:
        return BinaryTree()
    # generating the root
    root = preorder[0]
    tree = BinaryTree()
    tree.root = Node(root)
    # generating the rest of the tree
    if length > 1:
        i = inorder.index(root)
        # partitioning the nodes as per the branch
        inorder_left, preorder_left = (inorder[:i], preorder[1 : i + 1])
        inorder_right, preorder_right = (inorder[i + 1 :], preorder[i + 1 :])
        # creating a tree for each branch
        tree_left = generate_tree(preorder_left, inorder_left)
        tree_right = generate_tree(preorder_right, inorder_right)
        # attaching the sub-tree to their respective branch
        tree.root.left = tree_left.root
        tree.root.right = tree_right.root
    return tree
    balance = balance_left and balance_right
    current_balance = -1 <= (right_height - left_height) <= 1
    height = max(left_height, right_height) + 1
    return height, balance and current_balance


def check_balance(tree: BinaryTree) -> bool:
    if tree.root is None:
        return True
    _, balance = height_helper(tree.root)
    return balance


if __name__ == "__main__":
    tree = BinaryTree()

    tree.root = Node(0)

    tree.root.left = Node(1)
    tree.root.right = Node(2)

    tree.root.left.left = Node(3)
    tree.root.left.right = Node(4)

    print(check_balance(tree))

    tree.root.left.right.left = Node(5)

    print(check_balance(tree))
"""
    f = Node(600)
    g = Node(700)
    h = Node(800)

    a.left = b
    a.right = c

    b.left = d
    b.right = e

    c.left = f
    c.right = g

    d.right = h

    tree = BinaryTree()
    tree.root = a

    print(tree)
    print(get_level_min_sum(tree))
    a.val = 1000
    print(tree)
    print(get_level_min_sum(tree))
    b.val = 1500
    print(tree)
    print(get_level_min_sum(tree))
    h.val = 2000
    print(tree)
    print(get_level_min_sum(tree))
"""
SPECS:
if __name__ == "__main__":
    tree1 = BinarySearchTree()

    tree1.add(5)
    tree1.add(9)
    tree1.add(1)
    tree1.add(4)
    tree1.add(10)
    tree1.add(3)
    tree1.add(2)
    tree1.add(10)
    tree1.add(7)

    print(is_binary_search_tree(tree1))

    tree2 = BinaryTree()
    tree2.root = Node(5)

    tree2.root.left = Node(4)
    tree2.root.right = Node(6)

    print(is_binary_search_tree(tree2))

    tree3 = BinaryTree()
    tree3.root = Node(5)

    tree3.root.left = Node(6)
    tree3.root.right = Node(4)

    print(is_binary_search_tree(tree3))
"""
Пример #9
0
    if not tree1.root and not tree2.root:
        return tree
    # root generation
    r1, r2 = 0, 0
    if tree1.root:
        r1 = tree1.root.val
    if tree2.root:
        r2 = tree2.root.val
    tree.root = Node(r1 + r2)
    # generating rest of the tree
    merge_trees_helper(tree.root, tree1.root, tree2.root)
    return tree


if __name__ == "__main__":
    tree1 = BinaryTree()
    tree1.root = Node(1)
    tree1.root.left = Node(2)
    tree1.root.right = Node(3)
    tree1.root.left.right = Node(4)
    print(tree1)

    tree2 = BinaryTree()
    tree2.root = Node(2)
    tree2.root.right = Node(-3)
    tree2.root.right.right = Node(10)
    print(tree2)

    print(merge_trees(tree1, tree2))
"""
SPECS:
        if not self.is_locked():
            return True

        if self.left:
            left = self.left._is_any_descendant_unlocked()
        else:
            left = False
        if self.right:
            right = self.right._is_any_descendant_unlocked()
        else:
            right = False
        return left or right


if __name__ == "__main__":
    tree = BinaryTree()
    tree.root = NodeWithLock(5)

    tree.root.left = NodeWithLock(3)
    tree.root.left.parent = tree.root
    tree.root.right = NodeWithLock(18)
    tree.root.right.parent = tree.root

    tree.root.left.left = NodeWithLock(0)
    tree.root.left.left.parent = tree.root.left

    print(tree)

    print()
    print(tree.root.left.left.lock())
    print(tree.root.left.lock())
def deserialize(string: str) -> BinaryTree:
    # the string needs to have the same format as the binary tree serialization
    # eg: data is padded with single quotes (') and comma (,) is used as a delimiter
    data = string.split(",")
    queue = Queue()
    for node in data:
        queue.enqueue(node)
    tree = BinaryTree()
    tree.root = Node(queue.dequeue().strip("'"))
    deserialize_helper(tree.root, queue)
    return tree


if __name__ == "__main__":
    tree = BinaryTree()

    tree.root = Node("root")

    tree.root.left = Node("left")
    tree.root.right = Node("right")

    tree.root.left.left = Node("left.left")

    print(serialize(tree))

    generated_tree = deserialize(
        "'root','left','left.left','None','None','None','right','None','None'"
    )

    print(serialize(generated_tree))
    # if the subtree is not same, the children are checked
    if sub_tree1.left and find_helper(sub_tree1.left, sub_tree2):
        return True
    if sub_tree1.right and find_helper(sub_tree1.right, sub_tree2):
        return True
    return False


def get_match(s: BinaryTree, t: BinaryTree) -> bool:
    if s.root and t.root:
        return find_helper(s.root, t.root)
    return False


if __name__ == "__main__":
    tree1 = BinaryTree()
    tree1.root = Node(0)
    tree1.root.left = Node(1)
    tree1.root.right = Node(2)
    tree1.root.right.left = Node(3)
    tree1.root.right.right = Node(4)

    tree2 = BinaryTree()
    tree2.root = Node(2)
    tree2.root.left = Node(3)
    tree2.root.right = Node(4)

    tree3 = BinaryTree()
    tree3.root = Node(2)
    tree3.root.left = Node(3)
    tree3.root.right = Node(5)
Пример #13
0
        if callable(self.val):
            return self.val(self.left.calculate_helper(),
                            self.right.calculate_helper())
        return self.val


def calculate_expression_tree(tree: BinaryTree) -> Union[int, float]:
    root = tree.root
    if root:
        root.transform_helper()
        return root.calculate_helper()
    return None


if __name__ == "__main__":
    tree = BinaryTree()

    tree.root = ExpressionTreeNode("*")
    tree.root.left = ExpressionTreeNode("+")
    tree.root.right = ExpressionTreeNode("+")

    tree.root.left.left = ExpressionTreeNode(3)
    tree.root.left.right = ExpressionTreeNode(2)

    tree.root.right.left = ExpressionTreeNode(4)
    tree.root.right.right = ExpressionTreeNode(5)

    print(calculate_expression_tree(tree))
"""
SPECS:
Пример #14
0
def generate_cartesian_tree(sequence: List[int]) -> BinaryTree:
    tree = BinaryTree()
    tree.root = generate_cartesian_tree_helper(sequence)
    return tree