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


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery


