Esempio n. 1
0
    def construct(length: int) -> Optional[Node]:
        nonlocal head

        if head is None or length == 0:
            return None

        left = construct(length // 2)
        root = Node(head.data)
        head = head.next
        root.left = left
        root.right = construct(length - length // 2 - 1)
        return root
Esempio n. 2
0
    def tree() -> Optional[Node]:
        nonlocal index

        if index >= l:
            return None

        node = Node(preorder[index])
        index += 1

        if pre_ln[index - 1] == "N":
            node.left = tree()
            node.right = tree()

        return node
Esempio n. 3
0
def merge_sum_tree(augend: Optional[Node],
                   addend: Optional[Node]) -> Optional[Node]:
    if augend is None and addend is None:
        return None

    if augend is None:
        return addend
    if addend is None:
        return augend

    root = Node(augend.data + addend.data)
    root.left = merge_sum_tree(augend.left, addend.left)
    root.right = merge_sum_tree(augend.right, addend.right)

    return root
    def tree(start: int, end: int) -> Optional[Node]:
        nonlocal pre_index

        if pre_index >= l or start > end:
            return None

        value = preorder[pre_index]
        node = Node(value)
        in_index = d.get(value) or -1
        pre_index += 1

        if start < end:
            node.left = tree(start, in_index - 1)
            node.right = tree(in_index + 1, end)

        return node
Esempio n. 5
0
    def construct():
        nonlocal index, length

        if index >= length:
            return None

        root = Node(expression[index])
        index += 1

        if index < length - 1:
            if expression[index] == "?":
                index += 1
                root.left = construct()
                index += 1
                root.right = construct()

        return root
Esempio n. 6
0
    def tree(start: int, end: int) -> Optional[Node]:
        nonlocal pre_index

        if start > end or pre_index >= l:
            return None

        node = Node(preorder[pre_index])
        pre_index += 1

        if start == end or pre_index >= l:
            return node

        mirror_index = d[preorder[pre_index]]

        if mirror_index <= end:
            node.left = tree(mirror_index, end)
            node.right = tree(start + 1, mirror_index - 1)

        return node
Esempio n. 7
0
    def tree(start: int, end: int) -> Optional[Node]:
        nonlocal pre_index

        if pre_index >= l or start > end:
            return None

        value = preorder[pre_index]
        node = Node(value)
        pre_index += 1

        if start == end or pre_index >= l:
            return node

        post_index: int = d[preorder[pre_index]]

        if post_index <= end:
            node.left = tree(start, post_index)
            node.right = tree(post_index + 1, end)

        return node
Esempio n. 8
0
 def add_val_r(self, val):
     n = Node(val)
     self.add_node_r(self.root, n)
Esempio n. 9
0
 def add_val_l(self, val):
     '''put a new node on the left and fill it up with a val'''
     n = Node(val)  # 这里只是新建了一个 node,还需要把它和原来的树连起来
     self.add_node_l(self.root, n)
Esempio n. 10
0

def diagonal_traversal(root: Node):
    queue = deque()
    queue.append([root, 0])

    while queue:
        node, level = queue.popleft()

        if node.left:
            queue.append([node.left, level + 1])
        if node.right:
            queue.appendleft([node.right, level])
        if queue and queue[0][1] > level:
            print(node.data)
        else:
            print(node.data, end=" ")


if __name__ == "__main__":
    root = Node(8)
    root.left = Node(3)
    root.right = Node(10)
    root.left.left = Node(1)
    root.left.right = Node(6)
    root.right.left = Node(7)
    root.right.right = Node(14)
    root.left.right.left = Node(4)
    root.right.right.left = Node(13)
    diagonal_traversal(root)
        if node_1.left:
            first_queue.append(node_1.left)
            second_queue.append(node_2.right)

        if node_1.right:
            first_queue.append(node_1.right)
            second_queue.append(node_2.left)

    if first_queue or second_queue:
        return False

    return True


if __name__ == "__main__":
    root = Node(1)
    root.left = Node(2)
    root.right = Node(2)
    root.left.left = Node(4)
    root.left.right = Node(3)
    root.right.left = Node(3)
    root.right.right = Node(4)
    assert is_mirror_recursive(root) is True
    assert is_mirror_iterative(root) is True

    root = Node(1)
    root.left = Node(2)
    root.right = Node(2)
    root.left.right = Node(3)
    root.right.right = Node(3)
    assert is_mirror_recursive(root) is False
Esempio n. 12
0
    if root is None:
        return 0

    return_value = root.data
    if root.left is None and root.right is None:
        root.data = 0
        return return_value

    left_sum = sum_tree(root.left)
    right_sum = sum_tree(root.right)
    root.data = left_sum + right_sum
    return return_value + root.data


if __name__ == "__main__":
    root = Node(50)
    root.left = Node(7)
    root.right = Node(2)
    root.left.left = Node(3)
    root.left.right = Node(5)
    root.right.left = Node(1)
    root.right.right = Node(30)
    inorder(root)
    print()
    sum_tree(root)
    inorder(root)
    print()

    root = Node(10)
    root.left = Node(-2)
    root.right = Node(6)
    print(root.data)

    while lque or rque:
        if lque:
            elem = lque.popleft()
            print(elem.data)
            insert(elem, 1)
        if rque:
            elem = rque.popleft()
            print(elem.data)
            insert(elem, 2)


if __name__ == "__main__":
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    root.right.left = Node(6)
    root.right.right = Node(7)
    root.left.left.left = Node(8)
    root.left.left.right = Node(9)
    root.left.right.left = Node(10)
    root.left.right.right = Node(11)
    root.right.left.left = Node(12)
    root.right.left.right = Node(13)
    root.right.right.left = Node(14)
    root.right.right.right = Node(15)
Esempio n. 14
0
 def __init__(self, val):
     self.root = Node(val)
Esempio n. 15
0
def main():
    root = Node('A')
    b = Node('B')
    c = Node('C')
    d = Node('D')
    e = Node('E')
    f = Node('F')

    root.left_child = b
    root.right_child = c

    b.left_child = d
    b.right_child = e

    c.left_child = f
    """
            A          ---- root
        B       C
      D   E   F
    """
    leaf_depths = []
    print("Balanced: %r" % Node.is_balanced_tree(root, 0, leaf_depths))  # True
    print("Height: %r" % Node.get_height(root))  # 3
    print("Balanced(improved 1st): %r" % Node.is_balanced(root))  # True
    print("Balanced(improved 2nd): %r" %
          Node.is_balanced_improved(root))  # True

    root = Node('A')
    b = Node('B')
    c = Node('C')
    d = Node('D')
    e = Node('E')
    f = Node('F')

    root.left_child = b
    root.right_child = c

    b.left_child = d
    b.right_child = e

    d.left_child = f
    """
            A          ---- root
        B       C
      D   E
    F
    """
    leaf_depths = []
    print("Balanced: %r" %
          Node.is_balanced_tree(root, 0, leaf_depths))  # False
    print("Height: %r" % Node.get_height(root))  # 4
    print("Balanced(improved 1st): %r" % Node.is_balanced(root))  # False
    print("Balanced(improved 2nd): %r" %
          Node.is_balanced_improved(root))  # False
            if in_iter == 0:
                node.data = arr[1]
            elif in_iter == l - 1:
                node.data = arr[l - 2]
            else:
                node.data = arr[in_iter - 1] + arr[in_iter + 1]
            in_iter += 1

        traverse_inorder(node.right, fill_array)

    arr: list = []
    traverse_inorder(root, fill_array=True)
    in_iter = 0
    l = len(arr)
    traverse_inorder(root, fill_array=False)
    return root


if __name__ == "__main__":
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    root.right.left = Node(6)
    root.right.right = Node(7)

    print(inorder(root))
    root = replace_data(root)
    print(inorder(root))
Esempio n. 17
0
                   addend: Optional[Node]) -> Optional[Node]:
    if augend is None and addend is None:
        return None

    if augend is None:
        return addend
    if addend is None:
        return augend

    root = Node(augend.data + addend.data)
    root.left = merge_sum_tree(augend.left, addend.left)
    root.right = merge_sum_tree(augend.right, addend.right)

    return root


if __name__ == "__main__":
    augend = Node(2)
    augend.left = Node(1)
    augend.right = Node(4)
    augend.left.left = Node(5)

    addend = Node(3)
    addend.left = Node(6)
    addend.right = Node(1)
    addend.left.right = Node(2)
    addend.right.right = Node(7)

    root = merge_sum_tree(augend, addend)
    inorder(root)