def q_20(): """Making one queue instance with len of 20 per test.""" from que_ import Queue q = Queue() for num in range(20): q.enqueue(num) return q
def breadth_first(self): """ Traversal through bst with breadth first approach. Look at each node at current depth before going on to next level. """ from que_ import Queue q = Queue() nodes = [] q.enqueue(self.root) if self.root is None: raise ValueError("The bst is empty.") def _recur_breadth_first(): """Recursive helper function for breadth first.""" try: current = q.dequeue() nodes.append(current.val) if current.left: q.enqueue(current.left) if current.right: q.enqueue(current.right) _recur_breadth_first() except IndexError: return _recur_breadth_first() for val in nodes: yield val
def test_queue_peek_with_data(): """Test the queue peek method displays the tail.""" from que_ import Queue q = Queue() q.enqueue('one') q.enqueue('two') assert q.peek() == 'one'
def test_dequeue_node(): """Test if able to dequeue a node.""" from que_ import Queue test_q = Queue() test_q.enqueue(9) test_q.enqueue(10) assert test_q.dequeue() == 9
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 test_peek(): """Test if next value to be removed is accurate.""" from que_ import Queue test_q = Queue() test_q.enqueue(99) test_q.enqueue(5) assert test_q.peek() == 99
def test_dequeue_method(): """Remove a node in the proper sequence.""" from que_ import Queue q = Queue() q.enqueue('one') q.enqueue('two') assert q.dequeue() == 'one'
def test_head_tail_of_list_of_len_1(): """Test that in a one-node list, the node is head and tail.""" from que_ import Queue q = Queue() q.enqueue(5) assert q.linked_list.head assert q.linked_list.tail
def test_queue_enqueue_two_nodes(): """Test if able to enqueue two nodes.""" from que_ import Queue test_q = Queue() test_q.enqueue(7) test_q.enqueue(8) assert test_q._dll.tail.data == 7
def test_queue_len_function(): """Test that len function returns queue size.""" from que_ import Queue q = Queue() q.enqueue('one') q.enqueue('two') assert len(q) == 2
def test_dequeue_node_length(): """Test if able to dequeue a node.""" from que_ import Queue test_q = Queue() test_q.enqueue(9) test_q.enqueue(10) test_q.dequeue() assert test_q.size == 1
def test_if_peek_needs_no_inputs(): """Test that an unnecessary parameter will raise a TypeError.""" import pytest from que_ import Queue q = Queue() q.enqueue(5) with pytest.raises(TypeError): q.peek(5)
def test_dequeue_head_is_tail_with_one(): """Test if head == tail if only one node.""" from que_ import Queue test_q = Queue() test_q.enqueue(99) test_q.enqueue(5) test_q.dequeue() assert test_q._dll.tail == test_q._dll.head
def test_7(self): """ Check that length returns the size of queue """ q = Queue() n1 = Node(1) n2 = Node(2) q.enqueue(n1) q.enqueue(n2) self.assertEqual(q.length(), 2)
def test_5(self): """ Check that peek gives the value of the top node """ q = Queue() n1 = Node(1) n2 = Node(2) q.enqueue(n1) q.enqueue(n2) q.peek() self.assertEqual(q.top._value, 1)
def test_3(self): """ check that nodes are appending to the bottom of the queue, not the top """ q = Queue() n1 = Node(1) n2 = Node(2) n3 = Node(3) q.enqueue(n1) q.enqueue(n2) q.enqueue(n3) self.assertEqual(q.top._value, 1)
def test_4(self): """ check that deque turns the second node into the top node """ q = Queue() n1 = Node(1) n2 = Node(2) n3 = Node(3) q.enqueue(n1) q.enqueue(n2) q.enqueue(n3) q.dequeue() self.assertEqual(q.top._value, 2)
def breadth_first_traversal(graph, start): """Traverse a graph by breadth.""" if not isinstance(graph, Graph): raise TypeError('Must provide graph.') if not graph.has_node(start): raise KeyError('Node not in graph.') peeped = [] queue = Queue() queue.enqueue(start) while len(queue) > 0: node = queue.dequeue() if node not in peeped: peeped.append(node) for neighbor in graph._graph[node]: if neighbor not in peeped: queue.enqueue(neighbor) return peeped
def insert(self, val, priority=None): """.""" if not priority: priority = self.lowest if priority < self.lowest: self.lowest = priority for queue in self._priority: if priority == queue.priority: queue.enqueue(val) return q = Queue(priority) q.enqueue(val) for idx, queue in enumerate(self._priority): if priority > self._priority[idx].priority: self._priority.insert(idx, q) return self._priority.append(q)
def make_family(self, node): """Make a list of family members for node.""" family = Queue() family.enqueue(node) if node.parent: family.enqueue(node.parent) if node.parent.parent: parent = node.parent grandparent = node.parent.parent family.enqueue(grandparent) if grandparent.left: if grandparent.left != parent: family.enqueue(grandparent.left) if grandparent.right: if grandparent.right != parent: family.enqueue(grandparent.right) self.recoloring(family)
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 breadth_first_traversal(self, val): """ Perform a breadth first traversal. The breadth first traversal starts at the user input val and explores the neighbor nodes first before moving on to the next depth of neighbors. """ search_order = [] children_queue = Queue() children_queue.enqueue(val) while children_queue.peek(): if self.graph[children_queue.peek()] == {}: child = children_queue.dequeue() if child not in search_order: search_order.append(child) else: for child in self.graph[children_queue.peek()].keys(): if child not in search_order: children_queue.enqueue(child) search_order.append(children_queue.dequeue()) return search_order
def breadth_first(self): """Return values breadth first.""" q = Queue() q.enqueue(self.root) while len(q) > 0: node = q.dequeue() if node.left: q.enqueue(node.left) if node.right: q.enqueue(node.right) self.list.append(node.value) return self.list
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(self): """Return a generator that returns values tree breadth first order.""" if self.size_count == 0: raise IndexError("Cannot traverse empty tree.") breadth_list = Queue() breadth_list.enqueue(self.root) yield breadth_list.peek().val while breadth_list.peek(): if breadth_list.peek().left: breadth_list.enqueue(breadth_list.peek().left) yield breadth_list.peek().left.val if breadth_list.peek().right: breadth_list.enqueue(breadth_list.peek().right) yield breadth_list.peek().right.val breadth_list.dequeue()
def test_2(self): """ check that nodes are added to the queue """ q = Queue() n1 = Node(5) q.enqueue(n1) self.assertEqual(q.top._value, 5)
def test_queue_enqueue_node(): """Test if able to enqueue a node.""" from que_ import Queue test_q = Queue() test_q.enqueue(8) assert test_q._dll.head.data == 8
def test_output_of_peek_exists(): """Test that the output exists.""" from que_ import Queue q = Queue() q.enqueue(5) assert q.peek() is not None