我已经创建了一个堆类,我正在尝试创建一个PriorityQueue类,因此它们可以一起工作。但是,我需要有关代码的Priority Queue部分的帮助。我已经做了一个工作堆。我尝试在互联网上查找帮助,但我不断使用“queue”或“heapq”python实现获得答案。任何人都可以帮助我如何使一个有效的优先级队列课程?我写下了基本的功能名称,但我不知道从那里去哪里。请我坚持一段时间,真的需要一些帮助。这是我的工作堆密码。
class Heap(object):
def __init__(self, items=None):
'''Post: A heap is created with specified items.'''
self.heap = [None]
if items is None:
self.heap_size = 0
else:
self.heap += items
self.heap_size = len(items)
self._build_heap()
def size(self):
'''Post: Returns the number of items in the heap.'''
return self.heap_size
def _heapify(self, position):
'''Pre: Items from 0 to position - 1 satisfy the Heap property.
Post: Heap Property is satisfied for the entire heap.'''
item = self.heap[position]
while position * 2 <= self.heap_size:
child = position * 2
# If the right child, determine the maximum of two children.
if (child != self.heap_size and self.heap[child+1] > self.heap[child]):
child += 1
if self.heap[child] > item:
self.heap[position] = self.heap[child]
position = child
else:
break
self.heap[position] = item
def delete_max(self):
'''Pre: Heap property is satisfied
Post: Maximum element in heap is removed and returned. '''
if self.heap_size > 0:
max_item = self.heap[1]
self.heap[1] = self.heap[self.heap_size]
self.heap_size -= 1
self.heap.pop()
if self.heap_size > 0:
self._heapify(1)
return max_item
def insert(self, item):
'''Pre: Heap Property is Satisfied.
Post: Item is inserted in proper location in heap.'''
self.heap_size += 1
# extend the length of the list.
self.heap.append(None)
position = self.heap_size
parent = position // 2
while parent > 0 and self.heap[parent] < item:
# Move the item down.
self.heap[position] = self.heap[parent]
position = parent
parent = position // 2
# Puts the new item in the correct spot.
self.heap[position] = item
def _build_heap(self):
''' Pre: Self.heap has values in 1 to self.heap_size
Post: Heap property is satisfied for entire heap. '''
# 1 through self.heap_size.
for i in range(self.heap_size // 2, 0, -1): # Stops at 1.
self._heapify(i)
def heapsort(self):
'''Pre: Heap Property is satisfied.
Post: Items are sorted in self.heap[1:self.sorted_size].'''
sorted_size = self.heap_size
for i in range(0, sorted_size -1):
# Since delete_max calls pop to remove an item, we need to append a dummy value to avoid an illegal index.
self.heap.append(None)
item = self.delete_max()
self.heap[sorted_size - i] = item
所以这是有效的,但就像我之前所述,我在如何制作优先级队列方面遇到了麻烦?我知道要求代码是错误的,但我很绝望有人可以帮助我吗?我对我想要的优先级代码做了基本的概述。
#PriorityQueue.py
from MyHeap import Heap
class PriorityQueue(object):
def enqueue(self, item, priority):
'''Post: Item is inserted with specified priority in the PQ.'''
def first(self):
'''Post: Returns but does not remove the highest priority item from the PQ.'''
def dequeue(self):
'''Post: Removes and returns the highest priority item from the PQ.'''
def size(self):
'''Post: Returns the number of items in the PQ.'''
return Heap.heap_size
答案 0 :(得分:1)
我认为您实现PriorityQueue
类时缺少的关键想法是每个PriorityQueue
实例都应该有一个Heap
实例作为属性。在__init__
:
class PriorityQueue(object):
def __init__(self)
self.heap = Heap()
当用户调用PriorityQueue
方法时,该方法通常只调用self.heap
的方法,只需要一些额外的工作来修改参数或返回值。 insert
到堆中的项应该是(priority, value)
个元组,因为它们会相应地进行比较(更高优先级的项比较高)。
请注意,如果将为heapq
编写的代码与Heap
进行比较,则需要修改索引和优先级的逻辑,因为heapq
实现了零 - indexed min-heap,你的代码实现了一个索引的最大堆。