-1

カスタムメイドのヒープ クラス関数を Priority Queue クラスで使用するのに本当に問題があります。PriorityQueue の "enqueue"、"dequeue"、"front"、および "size" 関数にヒープ クラスのどの関数を使用するかについて問題があります。「エンキュー」には挿入機能を使用する必要があることはわかっていますが、優先順位があるため、これを行う方法がわかりません。正しく機能するために、PriorityQueue クラスが Heap クラスの関数を使用するために必要なことを教えてもらえますか? 私はしばらくこれに固執しており、queue や heapq などの組み込みの Python 関数の使用を含む答えを見つけ続けています。

クラス ヒープ (オブジェクト):

    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 __init__(self):
        self.heap = None

    def enqueue(self, item, priority):
        '''Post: Item is inserted with specified priority in the PQ.'''
        self.heap.insert((priority, item))

    def first(self):
    '''Post: Returns but does not remove the highest priority item from the PQ.'''
        return self.heap[0]

    def dequeue(self):
    '''Post: Removes and returns the highest priority item from the PQ.'''
    if self.heap is None:
        raise ValueError("This queue is empty.")
    self.heap.delete_max()

    def size(self):
    '''Post: Returns the number of items in the PQ.'''
        return self.size

これは私がこれまでに得たものですが、完全に正しいかどうかはわかりません。誰か助けてくれませんか?

コードを編集して最新バージョンにしました。

4

1 に答える 1