def test_dequeue(self):
     q = DeQueue(['A', 'B', 'C'])
     assert q.dequeue_front() == 'A'
     assert q.length() == 2
     assert q.dequeue_back() == 'C'
     assert q.length() == 1
     assert q.dequeue_back() == 'B'
     assert q.length() == 0
     assert q.is_empty() is True
     with self.assertRaises(ValueError):
         q.dequeue_front()
 def test_enqueue(self):
     q = DeQueue()
     q.enqueue_back('B')
     assert q.front() == 'B'
     assert q.length() == 1
     q.enqueue_back('C')
     assert q.front() == 'B'
     assert q.length() == 2
     q.enqueue_front('A')
     assert q.front() == 'A'
     assert q.length() == 3
     assert q.is_empty() is False
Пример #3
0
 def _traverse_level_order_iterative(self, start_node, visit):
     """Traverse this binary tree with iterative level-order traversal (BFS).
     Start at the given node and visit each node with the given function.
      Running time: O(n) Why and under what conditions?
      Memory usage: O(n) Why and under what conditions?"""
     # Create queue to store nodes not yet traversed in level-order
     """Remove and return the item at the back of this queue,"""
     queue = DeQueue()
     queue.enqueue_front(start_node)
     while queue.is_empty() == False:
         node = queue.dequeue_front()
         visit(node.data)
         if node.left != None:
             queue.enqueue_back(node.left)
         if node.right != None:
             queue.enqueue_back(node.right)
 def test_init_with_list(self):
     q = DeQueue(['A', 'B', 'C'])
     assert q.front() == 'A'
     assert q.length() == 3
     assert q.is_empty() is False
 def test_init(self):
     q = DeQueue()
     assert q.front() is None
     assert q.length() == 0
     assert q.is_empty() is True