堆基本操作實現(xiàn)最大堆
/**
* 實現(xiàn)最大堆
*
*/
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
const int M = 10003;
//定義數(shù)據(jù)節(jié)點
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;
}
};
//定義堆
template<class T>
class Heap{
public:
dNode h[M];
void heapify(int cur);
int n;
//數(shù)組下標從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;
}
//對數(shù)組h建立成堆
void build();
//插入一個元素
void insert(T data);
//彈出堆頂元素
void pop(){
h[0] = h[--n];
heapify(0);
};
//堆頂元素
T top(){return h[0];}
//打印數(shù)組中的全部元素
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);
}
}
/**
* 插入的過程就是將data放到數(shù)組下標為n的
* 那里,也就是數(shù)組的最后一個的元素后面
*
* 插入之后需要做的就是做保持堆性質(zhì)的工作,這個非常簡單
* 因為所做的工作就是將新增的data放到一條【有序鏈】上的合適位置(放入data后依然有序)
* 這條有序鏈就是【data的上一個元素】到root之間的一條鏈,這條鏈絕對是有序的
* 你就完全將這條鏈當(dāng)做是一個數(shù)組(只是上一個元素的下標不是減一)
* 假如需要放的位置是m, 那么就將m ———— (n - 1)的元素全部向下移動,然后將鏈尾被
* 擠出來的data放到位置m那里就好了
*/
template<class T>
void Heap<T>::insert(T data){
h[n++] = data;
T tmp = data; //將新增的節(jié)點保存
int cur = n - 1; //當(dāng)前節(jié)點,由于之前n++過了,所以減一
//循環(huán)找到合適放tmp的位置,并不斷向后移動元素給待放的tmp騰出位置
while(cur > 0 && h[P(cur)] < tmp){ //當(dāng)tmp比cur的父親大的時候
h[cur] = h[P(cur)];
cur = P(cur);
}
//現(xiàn)在的cur位置就是合適放tmp的位置了
h[cur] = tmp;
}
/**
* 調(diào)整cur這棵樹滿足堆(最大堆)
* 從cur的兩個孩子中找到最大值A(chǔ)和cur交換
* 然后從剛才最大值A(chǔ)中那個節(jié)點的位置遞歸調(diào)整(向下調(diào)整)
*/
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;
}
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10深入理解堆排序及其分析
- 01-10用C語言實現(xiàn)單鏈表的各種操作(一)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery