コード例 #1
0
def merge_trees_helper(node: Node, node1: Node, node2: Node) -> None:
    n1_l_val, n1_r_val = 0, 0
    n2_l_val, n2_r_val = 0, 0
    n1_l, n1_r = None, None
    n2_l, n2_r = None, None
    # tree1 node related data generation
    if node1:
        if node1.left:
            n1_l_val = node1.left.val
            n1_l = node1.left
        if node1.right:
            n1_r_val = node1.right.val
            n1_r = node1.right
    # tree2 node related data generation
    if node2:
        if node2.left:
            n2_l_val = node2.left.val
            n2_l = node2.left
        if node2.right:
            n2_r_val = node2.right.val
            n2_r = node2.right
    # left node generation
    if n1_l is not None or n2_l is not None:
        node.left = Node(n1_l_val + n2_l_val)
        merge_trees_helper(node.left, n1_l, n2_l)
    # right node generation
    if n1_r is not None or n2_r is not None:
        node.right = Node(n1_r_val + n2_r_val)
        merge_trees_helper(node.right, n1_r, n2_r)
コード例 #2
0
def deserialize_helper(node: Node, queue: Queue) -> Node:
    # helper function to deserialize a string into a Binary Tree
    # data is a queue containing the data as a prefix notation can be easily decoded
    # using a queue
    left = queue.dequeue().strip("'")
    if left != "None":
        # if the left child exists, its added to the tree
        node.left = Node(left)
        node.left = deserialize_helper(node.left, queue)

    right = queue.dequeue().strip("'")
    if right != "None":
        # if the right child exists, its added to the tree
        node.right = Node(right)
        node.right = deserialize_helper(node.right, queue)
    return node
コード例 #3
0
def create_full_bin_tree_helper(node: Node) -> None:
    # if a node with one missing child is encountered, the value is replaced by its
    # child and the children of the current node overwritten with the child's children
    if node.right is None and node.left is None:
        return
    elif node.left is not None and node.right is None:
        node.val = node.left.val
        node.right = node.left.right
        node.left = node.left.left
        create_full_bin_tree_helper(node)
    elif node.left is None and node.right is not None:
        node.val = node.right.val
        node.left = node.right.left
        node.right = node.right.right
        create_full_bin_tree_helper(node)
    elif node.left is not None and node.right is not None:
        create_full_bin_tree_helper(node.left)
        create_full_bin_tree_helper(node.right)
コード例 #4
0
def prune_helper(node: Node) -> None:
    if node.left:
        prune_helper(node.left)
        if node.left.val == 0:
            if not node.left.left and not node.left.right:
                temp = node.left
                node.left = None
                del temp
    if node.right:
        prune_helper(node.right)
        if node.right.val == 0:
            if not node.right.left and not node.right.right:
                temp = node.right
                node.right = None
                del temp
コード例 #5
0
def generate_cartesian_tree_helper(arr: List[int],
                                   last: Optional[Node] = None,
                                   root: Optional[Node] = None) -> Node:
    if not arr:
        return root
    # Cartesian tree generation
    node = Node(arr[0])
    if not last:
        # root of the tree
        return generate_cartesian_tree_helper(arr[1:], node, node)
    if last.val > node.val:
        # property of Cartesian tree
        node.left = last
        return generate_cartesian_tree_helper(arr[1:], node, node)
    last.right = node
    return generate_cartesian_tree_helper(arr[1:], last, last)
コード例 #6
0
def generate_helper(
    node: Node,
    probability_add_children: float = 0.5,
    probability_add_branch: float = 0.5,
) -> None:
    if random() > probability_add_children:
        return
    # generating the left branch
    if random() < probability_add_branch:
        node.left = Node(randint(1, 1000))
        generate_helper(node.left, probability_add_children,
                        probability_add_branch)
    # generating the right branch
    if random() < probability_add_branch:
        node.right = Node(randint(1, 1000))
        generate_helper(node.right, probability_add_children,
                        probability_add_branch)
コード例 #7
0
                queue.enqueue(None)
                curr_level_sum = 0
    return min_level_sum


if __name__ == "__main__":
    a = Node(100)
    b = Node(200)
    c = Node(300)
    d = Node(400)
    e = Node(500)
    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))
コード例 #8
0
    if not node:
        return
    return inorder_successor_helper(node)


# adding the parent pointer to Node class
setattr(Node, "parent", None)

if __name__ == "__main__":
    a = Node(10)
    b = Node(5)
    c = Node(30)
    d = Node(22)
    e = Node(35)

    a.left = b
    a.right = c
    c.left = d
    c.right = e

    b.parent = a
    c.parent = a
    d.parent = c
    e.parent = c

    tree = BinarySearchTree()
    tree.root = a

    print(tree)
    print(inorder_successor(d))
    print(inorder_successor(a))
コード例 #9
0

def get_largest_bst_size(tree: BinaryTree) -> Tuple[int, int]:
    size, node, _, _, _ = get_largest_bst_size_helper(tree.root)
    return size, node.val


if __name__ == "__main__":
    a = Node(3)
    b = Node(2)
    c = Node(6)
    d = Node(1)
    e = Node(1)
    f = Node(4)

    a.left = b
    a.right = c
    b.left = d
    b.right = e
    c.left = f

    tree = BinaryTree()
    tree.root = a

    print(tree)
    print("Size: {}\tNode Val: {}".format(*get_largest_bst_size(tree)))

    a = Node(3)
    b = Node(2)
    c = Node(6)
    d = Node(1)