优先级队列模板推送功能

时间:2018-05-16 04:45:28

标签: c++

我的优先级队列的Push函数出现问题。我编写了一个模板化的优先级队列,其类如下:

template <typename V>
class MyPriorityQueue
{
  V* value;
  int siz;
  int cap;
public:
  MyPriorityQueue(int = 2);
  MyPriorityQueue(const MyPriorityQueue<V>&); // copy constructor
  ~MyPriorityQueue();
  void push(const V&);
  V top() const {return value[siz-1];}
  V pop();
  bool empty() const;
  void clear();
  int size() const {return siz;}
};

这是我的构造函数

template <class V>
MyPriorityQueue<V>::MyPriorityQueue(int n)
{
    cap = n;
    value = new V[cap];
    siz = 0;
}

在我的构造函数中,我将cap初始化为2,将siz初始化为0并初始化值。

这是我的推送功能:

template <class V>
void MyPriorityQueue<V>::push(const V &newValue)
{
    int index = siz;
    if (cap == siz)
    {
        V *temp = new V[2 * cap];
        for (int i = 0; i < cap; i++)
            temp[i] = value[i];
        delete[] value;
        value = temp;
        cap = 2 * cap;
    }
    value[index] = newValue;

    while (true)
    {
        if (index <= 0)
            break;
        int parentIndex = (index - 1) / 2;
        cout << parentIndex << endl;
        if (value[index] < value[parentIndex])
            break;
        swap(value[parentIndex], value[index]);
        index = parentIndex;
    }
    siz++;
}

我创建了一个优先级队列

MyPriorityQueue<int> pq;

然后按如下所示推送其中的一些值

pq.push(14);
pq.push(6);
pq.push(3);
pq.push(19);
pq.push(9);

我使用以下代码进行检查

while(!pq.empty())
{
    cout << pq.top() << endl;
    pq.pop();
}

因为它是一个优先级队列,我的回答应该是19,14,9,6,3,因为它是喜欢的 相反,它给了我9,6,3,3,3。 我很困惑,我认为问题在于父索引的计算,但我无法弄明白。如果你需要完整的代码,我也可以发布,但它太长了。

我也在添加我的pop功能。

template <class V>
V MyPriorityQueue<V>::pop()
{
    V val = value[0];
    // create an int index, initialized to zero -- for traversing the tree
    int index = 0;
    //start loop -- to refill the position at the top of the tree -- the one that's getting popped
    while (true)
    {
        //compute the left and right child indexes of index
        int left_childIndex = 2 * index + 1;
        int right_childIndex = 2 * index + 2;
        //if there is no left child, break from the loop -- we reached the bottom of the heap
        if (left_childIndex >= siz)
            break;
        //  if there's no right child, copy the VALUE at the left child index to the VALUE at index and set index to the left child index -- left child "wins" by default because there's no right child
        if (right_childIndex >= siz)
        {
            value[index] = value[left_childIndex];
            index = left_childIndex;
        }
        //else if the VALUE at the right child index < the VALUE at the left child index, copy the value at the left child index to the value at index and set index to the left child index -- left child "wins"
        else if (value[right_childIndex] < value[left_childIndex])
        {
            value[index] = value[left_childIndex];
            index = left_childIndex;
        }
        //else copy the VALUE at the right child index to the VALUE at index and set index to the right child index -- right child "wins"
        else
        {
            value[index] = value[right_childIndex];
            index = right_childIndex;
        }
    }
    //decrement siz -- now, if index just happens to equal siz, we're done, but no harm in continuing anyway
    siz--;
    // copy the VALUE at siz to the VALUE at index -- it's the right-most value in the bottom-most row -- move it to "fill the hole"
    value[index] = value[siz];
    //start loop -- to promote the newly-repositioned value at index
    while (true)
    {
        //if index <= 0, it's reached the top -- break from the loop -- a value got promoted all the way to the top
        if (index <= 0)
            break;
        //compute the parent index of index
        int parentIndex = (index - 1) / 2; // using integer division with truncation
        //if the VALUE at index < the VALUE at parent index, break from the loop (no more promotions)
        if (value[index] < value[parentIndex])
            break;
        //swap the values at the parent index and index -- promote and demote
        swap(value[parentIndex], value[index]);
        // set index equal to the parent index -- traversing "up" the tree
        index = parentIndex;
    }
    return val;
}

0 个答案:

没有答案