def bft(root_node):
    if root_node is None:
        return False

    result = []

    queue = Queue()

    queue.enqueue(root_node)

    while not queue.isEmpty():

        current_node = queue.dequeue()

        result.append(str(current_node.data))

        if current_node.left:

            queue.enqueue(current_node.left)

        if current_node.right:

            queue.enqueue(current_node.right)

    result_string = "->".join(result)

    print result_string
def bft(root_node):
    if root_node is None:
        return False

    result = []

    queue = Queue()

    queue.enqueue(root_node)

    while not queue.isEmpty():

        current_node = queue.dequeue()

        result.append(str(current_node.data))

        if current_node.left:

            queue.enqueue(current_node.left)

        if current_node.right:

            queue.enqueue(current_node.right)

    result_string = "->".join(result)

    print result_string
def print_out_tree(root_node):
    if root_node is None:
        return False

    result = []

    level = 0

    last_level = -1

    level_queue = Queue()

    queue = Queue()

    queue.enqueue(root_node)

    level_queue.enqueue(level)

    while not queue.isEmpty():

        current_node = queue.dequeue()

        current_level = level_queue.dequeue()

        # result.append(str(current_node.data))

        result.append(str(current_node.data))

        # print "%s" %(current_node.data)

        level += 1

        if current_node.left:

            queue.enqueue(current_node.left)

            level_queue.enqueue(level)

        if current_node.right:

            queue.enqueue(current_node.right)

            level_queue.enqueue(level)

        print "  ".join(result)

        if current_level is not last_level:

            result = []

            last_level = current_level
Exemplo n.º 4
0
from structure import Queue
from structure import Stack

# Inverse queue
if __name__ == '__main__':
    input_queue = Queue()
    input_queue.add("example")
    input_queue.add("with")
    input_queue.add("Queue")

    stack = Stack(maxsize=3)
    while not stack.is_full():
        stack.push(input_queue.remove())

    output_queue = Queue()
    while not stack.is_empty():
        output_queue.add(stack.pop())

    print(f"{output_queue}")
def print_out_tree(root_node):
    if root_node is None:
        return False

    result = []

    level = 0

    last_level = -1

    level_queue = Queue()

    queue = Queue()

    queue.enqueue(root_node)

    level_queue.enqueue(level)

    while not queue.isEmpty():

        current_node = queue.dequeue()

        current_level = level_queue.dequeue()

        # result.append(str(current_node.data))

        result.append(str(current_node.data))

        # print "%s" %(current_node.data)

        level += 1

        if current_node.left:

            queue.enqueue(current_node.left)

            level_queue.enqueue(level)

        if current_node.right:

            queue.enqueue(current_node.right)

            level_queue.enqueue(level)

        print "  ".join(result)

        if current_level is not last_level:

            result = []

            last_level = current_level