造个轮子 - 优先队列(priority queue)

优先队列(priority queue)

优先队列中,元素(item)按键值(key value)排序,键值最低的元素位于队首,键值最高的元素位于队尾;反之亦然。

基本操作

  • 插入(insert、enqueue)

  • 删除(remove、dequeue)

实现

class PriorityQuery(object):
    """
    priority query with higher the value, higher the priority
    queue order - high priority (front) -> low priority (rear)
    """
    
    def __init__(self, max_length):
        self._queue = []
        self._max_length = max_length
        
    def __str__(self):
        return " ".join([str(item) for item in self._queue])
        
    def is_empty(self):
        return len(self._queue) == 0
        
    def is_full(self):
        return len(self._queue) == self._max_length
    
    def insert(self, item):
        """
        insert an item into the queue
        """
        # the queue is empty
        if self.is_empty():
            self._queue.append(item)
            return 0, item
        
        # if the queue is not empty, traverse the queue to find the position
        # where the item is inserted
        for idx in range(len(self._queue)):
            # the position new item will be inserted
            if self._queue[idx] <= item:
                self._queue.insert(idx, item)
                # if the queue has been full before insertion
                if len(self._queue) > self._max_length:
                    self._queue.pop()
                return idx, item
                
        # the queue is not full and the item has the lowest priority
        if not self.is_full():
            self._queue.append(item)
        return len(self._queue) - 1, item
    
    def delete(self):
        """
        pop the item with the lowest priority,
        i.e. the last item in the queue
        """
        if not self.is_empty():
            return self._queue.pop()
    

测试:

queue = PriorityQuery(max_length=10)

for item in [1, 1, 2, 3, 4, 5, 12, 23, 34, 12, 32, 1, 35, 43, 4, 19, 100]:
    result = queue.insert(item)
    if result:
        print("insert {0[1]} at position {0[0]}".format(result))
    print("now, the queue is:", queue)
insert 1 at position 0
now, the queue is: 1
insert 1 at position 0
now, the queue is: 1 1
insert 2 at position 0
now, the queue is: 2 1 1
insert 3 at position 0
now, the queue is: 3 2 1 1
insert 4 at position 0
now, the queue is: 4 3 2 1 1
insert 5 at position 0
now, the queue is: 5 4 3 2 1 1
insert 12 at position 0
now, the queue is: 12 5 4 3 2 1 1
insert 23 at position 0
now, the queue is: 23 12 5 4 3 2 1 1
insert 34 at position 0
now, the queue is: 34 23 12 5 4 3 2 1 1
insert 12 at position 2
now, the queue is: 34 23 12 12 5 4 3 2 1 1
insert 32 at position 1
now, the queue is: 34 32 23 12 12 5 4 3 2 1
insert 1 at position 9
now, the queue is: 34 32 23 12 12 5 4 3 2 1
insert 35 at position 0
now, the queue is: 35 34 32 23 12 12 5 4 3 2
insert 43 at position 0
now, the queue is: 43 35 34 32 23 12 12 5 4 3
insert 4 at position 8
now, the queue is: 43 35 34 32 23 12 12 5 4 4
insert 19 at position 5
now, the queue is: 43 35 34 32 23 19 12 12 5 4
insert 100 at position 0
now, the queue is: 100 43 35 34 32 23 19 12 12 5
发布了103 篇原创文章 · 获赞 162 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/zhaoyin214/article/details/103542351