class PriorityQueueElement: def __init__(self, key, value): self.key = key self.values = Queue() self.values.push(value) def pop(self): return self.values.pop() def peek(self): return self.values.peek() def __len__(self): return len(self.values) def __eq__(self, other): if hasattr(other, "key"): return self.key == other.key else: return self.key == other def __lt__(self, other): if hasattr(other, "key"): return self.key < other.key else: return self.key < other
def test_pop(self): queue = Queue() to_push = [-5, 0.0, "five", True, [], [1]] for item in to_push: queue.push(item) for item in to_push: self.assertEqual(queue.pop(), item) self.assertEqual(len(queue), 0)
def traverse_level_order(self): "Traverse tree in level order. Return contents in list" out = [] q = Queue() node = self._head while node is not None: out.append(node._data) for c in node._children: q.push(c) node = q.pop() return out