def test_length(self):
     dq = LinkedDeque()
     assert dq.length() == 0
     dq.push_back('A')
     assert dq.length() == 1
     dq.push_back('B')
     assert dq.length() == 2
     dq.pop_front()
     assert dq.length() == 1
     dq.pop_front()
     assert dq.length() == 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) all cases where n is the number of nodes and m is 
     the number of edges
     Memory usage: O(?) where n is the number of nodes; best case where each
     level stores only 1-2 nodes; worst case where all nodes are on the same 
     level except for the root node.
     """
     # Create queue to store nodes not yet traversed in level-order
     deque = LinkedDeque()
     # Push given starting node
     deque.push_back(start_node)
     # Loop until deque is empty
     while deque.length() > 0:
         # Pop node at front of deque
         node = deque.pop_front()
         # Visit this node's data with given function
         visit(node.data)
         # Enqueue this node's left child, if it exists
         if node.left:
             deque.push_back(node.left)
         # Enqueue this node's right child, if it exists
         if node.right:
             deque.push_back(node.right)
 def test_pop_front(self):
     dq = LinkedDeque(['A', 'B', 'C'])
     assert dq.length() == 3
     assert dq.front() == 'A'
     dq.pop_front()
     assert dq.length() == 2
     assert dq.front() == 'B'
     dq.pop_front()
     assert dq.length() == 1
     assert dq.front() == 'C'
     assert dq.is_empty() == False
     dq.pop_front()
     assert dq.length() == 0
     assert dq.front() == None
     assert dq.is_empty() == True
Exemplo n.º 4
0
 def test_pop_front(self):
     q = LinkedDeque()
     with self.assertRaises(ValueError):
         q.pop_front()
     q.push_front("A")
     assert q.length() == 1
     assert q.pop_front() == "A"
     assert q.is_empty() is True
     q.push_front("one")
     q.push_back("two")
     assert q.pop_front() == "one"
     assert q.pop_front() == "two"
     with self.assertRaises(ValueError):
         q.pop_front()
     assert q.is_empty() is True