示例#1
0
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
示例#2
0
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
示例#3
0
 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)
示例#4
0
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
示例#5
0
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'
示例#6
0
    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()
示例#7
0
def test_dequeue_one_item_from_any_length_queue(itr):
    """Test that dequeue item removes value from head of Queue."""
    from que_ import Queue
    q = Queue(itr)
    x = q.dequeue()
    assert x == itr[0]
    assert q.back.val == itr[-1]
    assert q.front.val == itr[1]
    assert q.front.nxt is None
    assert q._values.length == len(itr) - 1
示例#8
0
 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
示例#9
0
def test_dequeue_multiple_items_from_any_length_queue(itr):
    """Test that dequeue items removes head from Queue."""
    from que_ import Queue
    from random import randint
    q = Queue(itr)
    num = randint(2, len(itr) - 1)
    for _ in range(num):
        x = q.dequeue()
    assert x == itr[num - 1]
    assert q.back.val == itr[-1]
    assert q.front.val == itr[num]
    assert q.front.nxt is None
    assert q._values.length == len(itr) - num
示例#10
0
    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
示例#11
0
 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
示例#12
0
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
示例#13
0
    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.')
示例#14
0
def test_type_error_dequeue_if_empty():
    """Raise TypeError is dequeue is called on empty queue."""
    from que_ import Queue
    test_q = Queue()
    with pytest.raises(IndexError):
        test_q.dequeue()
示例#15
0
def test_dequeue_raise_index_error():
    """Raise an indext error when list empty."""
    from que_ import Queue
    q = Queue()
    with pytest.raises(IndexError):
        q.dequeue()
示例#16
0
 def test_6(self):
     """ check that empty queue returns error upon dequeue """
     q = Queue()
     self.assertEqual(q.dequeue(), "No nodes to return")