2019-07-31 19:06:06 +08:00
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2019-08-01 02:10:49 +08:00
|
|
|
|
|
|
|
|
|
2019-07-31 19:06:06 +08:00
|
|
|
template <class T>
|
|
|
|
|
class Heap
|
|
|
|
|
{
|
|
|
|
|
public:
|
2019-08-01 02:10:49 +08:00
|
|
|
typedef int (*Compare)(T , T);
|
2019-07-31 19:06:06 +08:00
|
|
|
|
2019-08-01 02:10:49 +08:00
|
|
|
private:
|
2019-07-31 19:06:06 +08:00
|
|
|
std::vector<T> data;
|
|
|
|
|
Compare compare;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Heap()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Heap(Compare _compare)
|
|
|
|
|
{
|
|
|
|
|
this->compare = _compare;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~Heap()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void push(T value)
|
|
|
|
|
{
|
|
|
|
|
unsigned long cur_idx = this->data.size();
|
|
|
|
|
this->data.push_back(value);
|
|
|
|
|
|
|
|
|
|
for (;cur_idx != 0;)
|
|
|
|
|
{
|
|
|
|
|
unsigned long push_idx = (cur_idx - 1) >> 1;
|
|
|
|
|
T &push_value = this->data[push_idx];
|
|
|
|
|
if (this->compare(value, push_value) > 0)
|
|
|
|
|
{
|
|
|
|
|
this->data[cur_idx] = push_value;
|
|
|
|
|
cur_idx = push_idx;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->data[cur_idx] = value;
|
|
|
|
|
}
|
2019-08-01 02:10:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
T pop() {
|
|
|
|
|
|
|
|
|
|
}
|
2019-07-31 19:06:06 +08:00
|
|
|
};
|