예제 #1
0
    queue = deque([root])

    while queue:
        count = len(queue)
        for i in range(count):
            next = queue.popleft()
            print(next.value, end=",")
            if next.left:
                queue.append(next.left)
            if next.right:
                queue.append(next.right)
        print()


if __name__ == "__main__":
    root = bst.from_ordered_list([2, 5, 6, 9, 17, 27, 34])
    print('inorder:')
    print_in_order(root)
    print('preorder:')
    print_pre_order(root)
    print('post order:')
    print_post_order(root)
    print('BFS single line:')
    print_levels_single_line(root)
    print('print levels:')
    print_levels(root)
    # print('height=', bst.get_height(root))
    # #print_tree_levels_recursive(root)
    # bst.print_levels(root)
    # print('two queue:')
    # print_tree_levels_twoqueue(root)
예제 #2
0
https://www.geeksforgeeks.org/check-if-two-trees-are-mirror/
"""
import bst


def is_mirror(root1, root2):
    if root1 is None and root2 is None:
        return True

    if root1 is None or root2 is None:
        return False

    if root1.value != root2.value:
        return False

    return is_mirror(root1.left, root2.right) and is_mirror(root1.right, root2.left)


# tests
print(is_mirror(bst.from_ordered_list([]), bst.from_ordered_list([])))
print(is_mirror(bst.from_ordered_list([1]), bst.from_ordered_list([1])))
print(is_mirror(bst.from_ordered_list([1,3,6,9,11]), bst.from_ordered_list([1,3,6,9,11])))

root2 = bst.Node(3)
bst.add_node(root2, 11)
bst.add_node(root2, 1)
bst.add_node(root2, 9)
bst.add_node(root2, 6)

print(is_mirror(bst.from_ordered_list([1,3,6,9,11]), root2))
    # case 5: both exist
    llist_start, llist_end = toDoubleLinkedList(root.left)
    rlist_start, rlist_end = toDoubleLinkedList(root.right)

    # fix up the list on the right
    root.right = rlist_start
    rlist_start.left = root

    # find the last node
    llist_end.right = root
    root.left = llist_end
    return llist_start, rlist_end


if __name__ == '__main__':
    root = bst.from_ordered_list([4, 8, 12, 16, 24, 32, 64])
    start, end = current = toDoubleLinkedList(root)

    while start:
        print(start, end=' ')
        start = start.right
    print()

    while end:
        print(end, end=' ')
        end = end.left

    # print()
    # reverseorder = True
    # while start or current is not ll:
    #     start = False
"""
Write a function to traverse a Binary tree in PreOrder, without using recursion. As you traverse,
please print contents of the nodes.
"""
import bst

def print_pre_order_iterative(root):
    stack = [root]
    while stack:
        # now pop the right element of the stack
        current = stack.pop()
        while current:
            if current.right:
                stack.append(current.right)
            print(current.value, end=',')
            current = current.left

if __name__=="__main__":
    root = bst.from_ordered_list([2,3,4,5,6])
    bst.print_levels(root)
    print()
    bst.print_pre_order(root)
    print()
    print_pre_order_iterative(root)

예제 #5
0
def is_identical(root1, root2):
    if root1 is None and root2 is None:
        return True

    # if either one is Null but not the other return False
    if root1 is None or root2 is None:
        return False

    if root1.value != root2.value:
        return False

    return is_identical(root1.left, root2.left) and is_identical(
        root1.right, root2.right)


# tests
print(is_identical(bst.from_ordered_list([]), bst.from_ordered_list([])))
print(is_identical(bst.from_ordered_list([1]), bst.from_ordered_list([1])))
print(
    is_identical(bst.from_ordered_list([1, 3, 6, 9, 11]),
                 bst.from_ordered_list([1, 3, 6, 9, 11])))

root2 = bst.Node(3)
bst.add_node(root2, 11)
bst.add_node(root2, 1)
bst.add_node(root2, 9)
bst.add_node(root2, 6)

print(is_identical(bst.from_ordered_list([1, 3, 6, 9, 11]), root2))
예제 #6
0
import bst


def buildStackPostOrder(root, stack):
    if root is None:
        return

    buildStackPostOrder(root.right, stack)
    stack.append(root.value)
    buildStackPostOrder(root.left, stack)


class treeIterator:
    def __init__(self, root):
        self.stack = []
        buildStackPostOrder(root, self.stack)

    def hasNext(self):
        return len(self.stack) > 0

    def next(self):
        return self.stack.pop()


if __name__ == "__main__":
    root = bst.from_ordered_list([2, 4, 11, 13, 21, 27, 35, 76, 112])
    i = treeIterator(root)
    while i.hasNext():
        print(i.next())
예제 #7
0
    if left_list is None:
        root.left = right_list_end
        right_list_end.right = root
        return root
    else:
        left_list_end = left_list.left
        left_list_end.right = root
        root.left = left_list_end
        right_list_end.right = left_list
        left_list.left = right_list_end
        return left_list


if __name__ == '__main__':
    root = bst.from_ordered_list([3, 7, 9, 13, 16, 19, 23])
    ll = current = toDoubleLinkedList(root)

    start = True
    while start or current is not ll:
        start = False
        print(current, end=' ')
        current = current.right

    print()
    start = True
    while start or current is not ll:
        start = False
        print(current, end=' ')
        current = current.left
예제 #8
0

def get_vertical_sums_helper(node, sums, col):
    # We have to visited each node in the tree
    # so time complexity is O(N) where N is the number of nodes in the tree
    # Space complexity is O(C) where C is the # of columns
    if node.left is None and node.right is None:
        # leaf node, simply add it to the columns sum
        current_sum = sums.setdefault(col, 0)
        sums[col] = current_sum + node.value
        return

    # keep going left
    if node.left:
        get_vertical_sums_helper(node.left, sums, col - 1)

    # process the current node
    current_sum = sums.setdefault(col, 0)
    sums[col] = current_sum + node.value

    # then go right
    if node.right:
        get_vertical_sums_helper(node.right, sums, col + 1)


print(get_vertical_sums(bst.from_ordered_list([])))
print(get_vertical_sums(bst.from_ordered_list([1])))
print(get_vertical_sums(bst.from_ordered_list([1, 2])))
print(get_vertical_sums(bst.from_ordered_list([1, 2, 3])))
print(get_vertical_sums(bst.from_ordered_list([1, 2, 3, 4, 5])))
print(get_vertical_sums(bst.from_ordered_list([1, 3, 6, 8, 12])))