C++作为二叉堆的优先级队列
一直在取得进展,但仍然无法弄清楚我的无限循环在哪里...
头文件:
#include <string>
class priority_queue_overflow{}; //if insert tries to exceed the size of A then throw priority_queue_overflow()
class priority_queue_underflow{}; //if extract_min tries is called on an empty heap then throw priority_queue_overflow()
class priority_queue {
private:
class pair {
public:
std::string object;
double key;
};
pair* A; //the array used to store the heap
int heapsize; //the current heap size
int size; //the current size of the array: does not change
void heapify(int k); //as described in Cormen et al
public:
priority_queue(int n); //don't forget to allocate the array of size n+1 as we don't use slot zero
~priority_queue(); //delete the array
bool empty(); //true/false depending upon whether the heap is empty
void insert(std::string s, double priority); //add s to the heap with the given priority as its key
std::string extract_min(); //remove the string of lowest key and return that string
operator std::string();
};
cpp 文件:
/**
* implementing the priority queue as a binary heap
*
*/
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
#include <map>
#include <algorithm>
#include "binary_heap.hpp"
/***********************
*** inline functions
***********************/
inline int left(int i) { return 2*1; } // ( i << 1 )
inline int right(int i) { return 2*i+1; } // (i << 1 | 1)
inline int parent(int i) { return i/2; } // ( i >> 1 )
/*********************
*** constructor
*********************/
priority_queue::priority_queue(int n) //don't forget to allocate the array of size n+1 as we don't use slot zero
:heapsize(0), size(n)
{
A = new pair[n+1];
}
/*********************
*** destructor
*********************/
priority_queue::~priority_queue() //delete the array
{
delete[] A; // iterrate through delete each elem
}
/*******************************************
*** heapify * finds max of three things
*******************************************/
void priority_queue::heapify(int k)
{
std::cout<<"HERE HEAP"<<'\n';
pair smallest = A[k];
int pos = k;
//only treat child as object if it's inside heap
if (left(k) <= heapsize and A[left(k)].key < A[pos].key) {
// update variables
smallest = A[left(k)];
pos = left(k);
}
// identical for right
if (right(k) <= heapsize and A[right(k)].key < A[pos].key) {
// update variables
smallest = A[right(k)];
pos = right(k);
}
// after both if's exectued: smallest and pos contain smallest key
// only need to do something if pos is !=i
std::cout<< pos <<" == "<< k<<'\n';
if (pos != k) {
// swap items
std::swap(A[k], A[pos]);
// go recursive
heapify(pos);
}
}
/****************
*** empty
****************/
bool priority_queue::empty()
{
return (heapsize == 0);
}
/****************
*** insert
****************/
void priority_queue::insert(std::string s, double priority) //add s to the heap with the given priority as its key
{
if (heapsize == size) {
throw priority_queue_overflow();
}
++heapsize;
A[heapsize].object = s;
A[heapsize].key = priority;
int i(heapsize);
while (i > 1 and A[parent(i)].key > A[i].key) {
std::swap(A[parent(i)], A[i]);
i = parent(i);
}
}
/*******************
*** extract_min
*******************/
std::string priority_queue::extract_min() //remove the string of lowest key and return that string
{
if (heapsize == 0) {
throw priority_queue_underflow();
}
std::string ans = A[1].object;
A[1] = A[heapsize];
--heapsize;
heapify(1);
return ans;
}
/**********************************
*** function operator overload
**********************************/
priority_queue::operator std::string()
{
std::stringstream text;
int i(1);
while (i <= size) {
text << A[i].object << std::endl;
++i;
}
return text.str();
}
have been making progress, but still can't figure out where my infinite loop is...
header file:
#include <string>
class priority_queue_overflow{}; //if insert tries to exceed the size of A then throw priority_queue_overflow()
class priority_queue_underflow{}; //if extract_min tries is called on an empty heap then throw priority_queue_overflow()
class priority_queue {
private:
class pair {
public:
std::string object;
double key;
};
pair* A; //the array used to store the heap
int heapsize; //the current heap size
int size; //the current size of the array: does not change
void heapify(int k); //as described in Cormen et al
public:
priority_queue(int n); //don't forget to allocate the array of size n+1 as we don't use slot zero
~priority_queue(); //delete the array
bool empty(); //true/false depending upon whether the heap is empty
void insert(std::string s, double priority); //add s to the heap with the given priority as its key
std::string extract_min(); //remove the string of lowest key and return that string
operator std::string();
};
cpp file:
/**
* implementing the priority queue as a binary heap
*
*/
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
#include <map>
#include <algorithm>
#include "binary_heap.hpp"
/***********************
*** inline functions
***********************/
inline int left(int i) { return 2*1; } // ( i << 1 )
inline int right(int i) { return 2*i+1; } // (i << 1 | 1)
inline int parent(int i) { return i/2; } // ( i >> 1 )
/*********************
*** constructor
*********************/
priority_queue::priority_queue(int n) //don't forget to allocate the array of size n+1 as we don't use slot zero
:heapsize(0), size(n)
{
A = new pair[n+1];
}
/*********************
*** destructor
*********************/
priority_queue::~priority_queue() //delete the array
{
delete[] A; // iterrate through delete each elem
}
/*******************************************
*** heapify * finds max of three things
*******************************************/
void priority_queue::heapify(int k)
{
std::cout<<"HERE HEAP"<<'\n';
pair smallest = A[k];
int pos = k;
//only treat child as object if it's inside heap
if (left(k) <= heapsize and A[left(k)].key < A[pos].key) {
// update variables
smallest = A[left(k)];
pos = left(k);
}
// identical for right
if (right(k) <= heapsize and A[right(k)].key < A[pos].key) {
// update variables
smallest = A[right(k)];
pos = right(k);
}
// after both if's exectued: smallest and pos contain smallest key
// only need to do something if pos is !=i
std::cout<< pos <<" == "<< k<<'\n';
if (pos != k) {
// swap items
std::swap(A[k], A[pos]);
// go recursive
heapify(pos);
}
}
/****************
*** empty
****************/
bool priority_queue::empty()
{
return (heapsize == 0);
}
/****************
*** insert
****************/
void priority_queue::insert(std::string s, double priority) //add s to the heap with the given priority as its key
{
if (heapsize == size) {
throw priority_queue_overflow();
}
++heapsize;
A[heapsize].object = s;
A[heapsize].key = priority;
int i(heapsize);
while (i > 1 and A[parent(i)].key > A[i].key) {
std::swap(A[parent(i)], A[i]);
i = parent(i);
}
}
/*******************
*** extract_min
*******************/
std::string priority_queue::extract_min() //remove the string of lowest key and return that string
{
if (heapsize == 0) {
throw priority_queue_underflow();
}
std::string ans = A[1].object;
A[1] = A[heapsize];
--heapsize;
heapify(1);
return ans;
}
/**********************************
*** function operator overload
**********************************/
priority_queue::operator std::string()
{
std::stringstream text;
int i(1);
while (i <= size) {
text << A[i].object << std::endl;
++i;
}
return text.str();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该尝试找出遇到麻烦的最小问题。如果您能准确缩小问题范围,会更容易提供帮助。
如果您无法理解如何在数组上下文中使用示例中的
pair
类,那么下面的小(独立)示例可能会有所帮助:You should try to extract the smallest problem you're having trouble with. It would be easier to help if you could narrow down exactly what your question is.
If you're having trouble understanding how to use the
pair
class from your example in an array context maybe the following small (self-contained) example will help:好的,终于明白了...谢谢 errbody :-)
ok, finally got it... thanks errbody :-)