Heap basic operations implement maximum heaps
- 2020-04-02 02:11:54
- OfStack
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
const int M = 10003;
//Define data node
class dNode{
public:
string name;
int age;
double score;
dNode():name("no name"), age(0), score(0.0){}
dNode(string name, int age, double score):name(name), age(age), score(score){}
bool operator < (const dNode & d){
return score < d.score;
}
bool operator > (const dNode &d){
return score > d.score;
}
bool operator = (const dNode &d){
name = d.name;age=d.age;score=d.score;
}
bool operator == (const dNode &d){
return name == d.name && age == d.age && score == d.score;
}
void swap(dNode & a, dNode & b){
dNode tmp = a;
a = b;
b = tmp;
}
void show(){
cout << "***************" << endl;
cout << "name: " << name << endl;
cout << "age: " << age << endl;
cout << "score: " << score << endl;
}
};
//Define the heap
template<class T>
class Heap{
public:
dNode h[M];
void heapify(int cur);
int n;
//The array index starts at 0
int L(int i){return (i << 1) + 1;}
int R(int i){return (i << 1) + 2;}
int P(int i){return (i - 1) >> 1;}
public:
Heap():n(0){}
Heap(T data[], int len){
memcpy(h, data, sizeof(T) * len);
n = len;
}
//Set up a heap of array h
void build();
//Insert an element
void insert(T data);
//Pops up the heap top element
void pop(){
h[0] = h[--n];
heapify(0);
};
//Roof element
T top(){return h[0];}
//Print all the elements in the array
void show(){
for(int i = 0; i < n; i++){
cout << "***************" << endl;
cout << "cur: " << i << endl;
cout << "name: " << h[i].name << endl;
cout << "age: " << h[i].age << endl;
cout << "score: " << h[i].score << endl;
}
}
};
template<class T>
void Heap<T>::build(){
for(int i = (n / 2) - 1; i >= 0; i--){
heapify(i);
}
}
template<class T>
void Heap<T>::insert(T data){
h[n++] = data;
T tmp = data; //Save the new node
int cur = n - 1; //The current node, because n++ passed before, so minus one
//The loop finds the right place for the TMP and moves the element back to make room for the TMP to be placed
while(cur > 0 && h[P(cur)] < tmp){ //When TMP was older than cur's father
h[cur] = h[P(cur)];
cur = P(cur);
}
//Now the cur location is suitable for TMP
h[cur] = tmp;
}
template<class T>
void Heap<T>::heapify(int cur){
T mmax = h[L(cur)] > h[R(cur)] ? h[L(cur)] : h[R(cur)];
if(mmax < h[cur])
return;
//cout << "##########" << endl;
//mmax.show();
if(h[L(cur)] == mmax){
h[0].swap(h[cur], h[L(cur)]);
heapify(L(cur));
}else{
h[0].swap(h[cur], h[R(cur)]);
heapify(R(cur));
}
//cout << "##########" << endl;
}
int main(){
int num = 7;
dNode d[M];
for(int i = 0; i < num; i++){
d[i] = dNode("Luo", rand() % 50, rand() % 11);
}
d[0].score = 10;
d[1].score = 33;
d[2].score = 22;
d[3].score = 43;
d[4].score = 7;
d[5].score = 66;
d[6].score = 1;
Heap<dNode> *h = new Heap<dNode>(d, num);
h->build();
h->insert(d[1]);
h->insert(d[3]);
h->show();
cout << "########### test top and pop ####" << endl;
h->top().show();
h->pop();
h->top().show();
return 0;
}