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_push_back(self): q = LinkedDeque() q.push_back("A") assert q.length() == 1 q.push_back("B") assert q.length() == 2 assert q.front() == "A" assert q.back() == "B"
def test_pop_back(self): dq = LinkedDeque(['A', 'B', 'C']) assert dq.length() == 3 assert dq.front() == 'A' dq.pop_back() assert dq.length() == 2 assert dq.front() == 'A' dq.pop_back() assert dq.length() == 1 assert dq.front() == 'A' assert dq.is_empty() == False dq.pop_back() assert dq.length() == 0 assert dq.front() == None assert dq.is_empty() == True
def test_init(self): dq = LinkedDeque() assert dq.front() is None assert dq.length() == 0 assert dq.is_empty() is True
def test_push_back(self): dq = LinkedDeque() dq.push_back('A') assert dq.length() == 1 assert dq.front() == 'A' dq.push_back('B') assert dq.length() == 2 assert dq.front() == 'A' dq.push_back('C') assert dq.length() == 3 assert dq.front() == 'A' assert dq.is_empty() == False
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 test_init_with_list(self): dq = LinkedDeque(['A', 'B', 'C']) assert dq.front() == 'A' assert dq.length() == 3 assert dq.is_empty() is False
def setUp(self): self.deque = LinkedDeque()
class TestLinkedDeque(unittest.TestCase): def setUp(self): self.deque = LinkedDeque() def test_push(self): self.deque.push(1) self.deque.push(2) self.deque.push(3) self.assertEqual(self.deque.size, 3) def test_push_left(self): self.deque.push_left(1) self.deque.push_left(2) self.deque.push_left(3) self.assertEqual(self.deque.size, 3) def test_pop(self): self.deque.push(1) self.deque.push(2) self.deque.push(3) self.assertEqual(self.deque.pop(), 3) self.assertEqual(self.deque.size, 2) def test_pop_left(self): self.deque.push(1) self.deque.push(2) self.deque.push(3) self.assertEqual(self.deque.pop_left(), 1) self.assertEqual(self.deque.size, 2) def test_remove(self): self.deque.push(1) self.deque.push(2) self.deque.push(3) self.deque.remove(1) self.assertEqual(self.deque.size, 2) def test_clear(self): self.deque.push(1) self.deque.push(2) self.deque.push(3) self.assertEqual(self.deque.size, 3) self.deque.clear() self.assertEqual(self.deque.size, 0) def test_is_empty(self): self.assertEqual(self.deque.is_empty(), True)
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
def test_back(self): q = LinkedDeque() assert q.back() is None
def test_front(self): q = LinkedDeque() assert q.front() is None