国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

堆結構

2019-11-14 09:32:29
字體:
來源:轉載
供稿:網友

堆的主要操作:

*建立一個堆 *往堆里添加新元素 *訪問最大/最小值 *刪除最大/最小值

模板如下:

#include<cstdio>#define MAX_SIZE 100int A[MAX_SIZE]={0,1,2,3,4,5,6,7,8,9},N=9; //下標從1開始 void downAdjustment(int A[],int idx);//向下調整 void buildHeap(){ int i; for(int i=N/2;i>=1;i--) downAdjustment(A,i);} void downAdjustment(int A[],int idx){ int leftChild= 2*idx; int rightChild= 2*idx+1; int max; if(leftChild > N) return; if( rightChild >N||(A[leftChild] > A[rightChild]) ) max=leftChild; else max=rightChild; if(A[idx] < A[max]){ int tmp=A[idx]; A[idx]= A[max]; A[max]=tmp; downAdjustment(A,max);//遞歸向下調整 } }//向上調整 void upAdjustment(int A[],int idx){ int parent= idx/2; if(parent < 1) return; if(A[idx] > A[parent]){ int tmp=A[idx]; A[idx]=A[parent]; A[parent]=tmp; upAdjustment(A,parent);//遞歸向上調整 }}//添加新元素 void addAElement(int ele){ A[++N] = ele; upAdjustment(A,N);}//訪問最大值 int maxElement(){ return A[1]; } //刪除最大值 void deleteMax(){ A[1]=A[N]; N--; downAdjustment(A,1);}int main(){ buildHeap(); for(int i=1;i<=N;i++)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 保山市| 柳林县| 牟定县| 镶黄旗| 广饶县| 集安市| 关岭| 泸水县| 鄂州市| 红河县| 于田县| 顺义区| 青河县| 卓资县| 出国| 丘北县| 普宁市| 漯河市| 锡林浩特市| 深圳市| 晋江市| 黔东| 溧水县| 镇雄县| 石屏县| 永仁县| 呈贡县| 宣武区| 土默特右旗| 宣恩县| 汉中市| 北川| 澳门| 佛坪县| 龙胜| 六安市| 墨玉县| 金湖县| 犍为县| 碌曲县| 徐州市|