Example #1
0
    def test_queue(self):
        queue = Queue()
        self.assertEqual(queue.is_empty(), True)
        self.assertEqual(queue.peek(), None)

        queue.queue("first_in_line")
        self.assertEqual(queue.peek(), "first_in_line")
        self.assertEqual(queue.is_empty(), False)

        queue.queue("second_in_line")
        self.assertEqual(queue.de_queue(), "first_in_line")
        self.assertEqual(queue.peek(), "second_in_line")

        self.assertEqual(queue.de_queue(), "second_in_line")
        self.assertEqual(queue.is_empty(), True)
        self.assertEqual(queue.peek(), None)
 def traversal_level_order(self):
     # o(n) for time
     # o(n) for space
     if self.root is None:
         return
     node_queue = Queue()
     node_queue.queue(self.root)
     value = []
     while not node_queue.is_empty():
         current_node = node_queue.de_queue()
         value.append(current_node.value)
         if current_node.left is not None:
             node_queue.queue(current_node.left)
         if current_node.right is not None:
             node_queue.queue(current_node.right)
     return value