簡體   English   中英

使用堆的優先級隊列

[英]Priority Queue using Heap

在從算法導論學習之后,我編寫了這段代碼。 我無法找出代碼有什么問題。 我已經為heapsort編寫了代碼並且運行良好。 heap_extract_max()將返回最大值。 heap_increase_key()增加元素的優先級。 在這里,我使用單鏈表編寫了優先級隊列程序,運行良好。

#include <iostream>
#include <vector>
#include <algorithm>

template<typename T>
void max_heapify(std::vector<T>& array, size_t index)
{
  size_t largest;
  size_t left  = (2*index) + 1;
  size_t right = left + 1;

  if(left < array.size() && array[left] > array[index])
    largest = left;
  else
    largest = index;

  if(right < array.size() && array[right] > array[largest])
    largest = right;

  if(largest != index)
  {
    int tmp = array[index];
    array[index] = array[largest];
    array[largest] = tmp;
    max_heapify(array, largest);
  }
}

template<typename T>
void build_max_heap(std::vector<T>& array)
{
  for(size_t i = (array.size()/2) - 1; i >= 0; i--)
  max_heapify(array, i);
}

template<typename T>
T heap_maximum(std::vector<T>& array)
{
  return array[0];
}

template<typename T>
T heap_extract_max(std::vector<T>& array)
{
  if(array.size() < 0)
  {
    std::cerr << "Heap Underflow \n";
  }
  else
  {
    T max = array[0];
    array[0] = array[array.size() - 1];
    //array.size() = array.size() - 1;
    max_heapify(array, 1);
    return max;
  }
}

template<typename T>
void heap_increase_key(std::vector<T>& array, size_t index, T value)
{
  if(value < array[index])
  {
    std::cerr <<"New value is smaller than the current value\n";
    return;
  }
  else
  {
    array[index] = value;
    while(index > 0 && array[(index/2) - 1] < array[index])
    {
      std::swap(array[index], array[(index/2) - 1]);
      index = (index/2) - 1;
    }
  }
}

template<typename T>
void max_heap_insert(std::vector<T>& array, T value)
{
  array[array.size()] = -1;
  heap_increase_key(array, array.size(), value);
}

int main()
{
std::vector<int> v({1, 2, 6, 3, 7});
build_max_heap(v);
std::cout << heap_extract_max(v)<<"\n";
for(size_t i = 0; i < v.size(); i++)
{
  std::cout << v[i] << " ";
}
std::cout << "\n";
}

它沒有顯示任何輸出。 我在寫命令

$ g++ -std=c++11 priorityqueue.cpp -o priorityqueue
$ ./priorityqueue

如果因為這種情況,問題在於以下函數將返回什么

控制可能達到無效功能的結束

template<typename T>
T heap_extract_max(std::vector<T>& array)
{
    if(array.size() < 0)
    {
        std::cerr << "Heap Underflow \n";
    }
    else
    {
        T max = array[0];
        array[0] = array[array.size() - 1];
        //array.size() = array.size() - 1;
        max_heapify(array, 1);
        return max;
    }
}

為if case添加return -1或其他任何內容

if(array.size() < 0)
{
    std::cerr << "Heap Underflow \n";
    return -1;
}

第二個問題

for(size_t i = (array.size()/2) - 1; i >= 0; i--)

無符號表達式的比較> = 0始終為真

嘗試改為:(按照Serge Rogatch的建議)

for(int64_t i = (int64_t(array.size())/2) - 1; i >= 0; i--)

提取它之前有問題在改變結果之前

7
2 3 6 1 2 
Program ended with exit code: 0

由於錯誤處理刪除.Handled在下面的代碼中正確刪除

template<typename T>
T heap_extract_max(std::vector<T>& array)
{
    if(array.size() < 0)
    {
        std::cerr << "Heap Underflow \n";
        return -1;
    }
    else
    {
        T max = array[0];
        array[0] = array[array.size() - 1];
        array.erase(std::remove(array.begin(), array.end(), array[0]),
                   array.end());
        array.shrink_to_fit();
        max_heapify(array, 0);
        return max;
    }
}

改變之后

7
6 3 1 2 
Program ended with exit code: 0

看來你的程序進入無限循環或崩潰。 我在這里看到一個問題:

for(size_t i = (array.size()/2) - 1; i >= 0; i--)

因為i是無符號的,所以總是i >= 0 另外,對於array.size() <= 1 ,你用一些大的正整數初始化它,因為i試圖去負,但size_t永遠不是負數,所以數字包裝。

嘗試更改為:

for(int64_t i = (int64_t(array.size())/2) - 1; i >= 0; i--)

你似乎也混淆了基於01的數組索引,你應該用array.pop_back()來代替//array.size() = array.size() - 1; 此外,似乎你想在這里使用array[1]而不是array[0]

T max = array[0];
array[0] = array[array.size() - 1];

如果您堅持使用基於1的索引,則應在數組中的索引0處放置並保留一個虛擬元素:

std::vector<int> v({/*dummy*/-1, 1, 2, 6, 3, 7});

數組大小永遠不會為負數,因此您不需要if(array.size() < 0)以及then子句中的內容。

雖然基於0的索引在C ++中更自然,但堆也可以用它來實現。 為此,您需要修改所有索引算法,如:

size_t left  = (2*index) + 1;
size_t right = left + 1;

array[(index/2) - 1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM