def test_queue_size_method(): """Test that queue size method returns size.""" from que_ import Queue q = Queue() q.enqueue('one') q.enqueue('two') assert q.size() == 2
def breadth(self): from que_ import Queue return_list = [] next_up = Queue() next_up.enqueue(self._root) while next_up.size() > 0: temp = next_up.dequeue().value if temp.left: next_up.enqueue(temp.left) if temp.right: next_up.enqueue(temp.right) return_list.append(temp.value) for value in return_list: yield value
def breadth_first_traversal(graph, start): """Traverse a graph by breadth.""" if not isinstance(graph, Weighted): raise TypeError('Must provide graph.') if not graph.has_node(start): raise KeyError('Node not in graph.') peeped = [] queue = Queue() queue.enqueue(start) while queue.size() > 0: node = queue.dequeue() if node not in peeped: peeped.append(node) for neighbor in list(graph[node].keys()): if neighbor not in peeped: queue.enqueue(neighbor) return peeped
def breadth_first_traversal(self, val): """Return a list of all nodes connected to given start pointbased on a breadth first algorithm.""" from que_ import Queue seen = [] next_up = Queue() try: while True: if val not in seen: seen.append(val) for i in self.graph_dict[val]: next_up.enqueue(i) if next_up.size() == 0: break val = next_up.dequeue()[0] return seen except KeyError: raise KeyError('Given value does not exist.')
def test_size_for_queue(itr): """Test that size gets the length of a queue.""" from que_ import Queue q = Queue(itr) assert q.size() == len(itr)
def test_queue_constructs_with_iterable(): """.""" from que_ import Queue a = Queue([1, 2]) assert a.size() == len([1, 2])