詳解Java如何實(shí)現(xiàn)小頂堆和大頂堆
大頂堆
每個(gè)結(jié)點(diǎn)的值都大于或等于其左右孩子結(jié)點(diǎn)的值
小頂堆
每個(gè)結(jié)點(diǎn)的值都小于或等于其左右孩子結(jié)點(diǎn)的值
對(duì)比圖

實(shí)現(xiàn)代碼
public class HeapNode{
private int size;//堆大小
private int[] heap;//保存堆數(shù)組
//初始化堆
public HeapNode(int n) {
heap = new int[n];
size = 0;
}
//小頂堆建堆
public void minInsert(int key){
int i = this.size;
if (i==0) heap[0] = key;
else {
while (i>0 && heap[i/2]>key){
heap[i] = heap[i/2];
i = i/2;
}
heap[i] = key;
}
this.size++;
}
//大頂堆建堆
public void maxInsert(int key){
int i = this.size;
if (i==0) heap[0] = key;
else {
while (i>0 && heap[i/2]<key){
heap[i] = heap[i/2];
i = i/2;
}
heap[i] = key;
}
this.size++;
}
//小頂堆刪除
public int minDelete(){
if (this.size==0) return -1;
int top = heap[0];
int last = heap[this.size-1];
heap[0] = last;
this.size--;
//堆化
minHeapify(0);
return top;
}
//大頂堆刪除
public int maxDelete(){
if (this.size==0) return -1;
int top = heap[0];
int last = heap[this.size-1];
heap[0] = last;
this.size--;
//堆化
maxHeapify(0);
return top;
}
//小頂堆化
public void minHeapify(int i){
int L = 2*i,R=2*i+1,min;
if (L<=size && heap[L] < heap[i]) min = L;
else min = i;
if (R <= size && heap[R] < heap[min]) min = R;
if (min!=i){
int t = heap[min];
heap[min] = heap[i];
heap[i] = t;
minHeapify(min);
}
}
//大頂堆化
public void maxHeapify(int i){
int L = 2*i,R=2*i+1,max;
if (L<=size && heap[L] > heap[i]) max = L;
else max = i;
if (R <= size && heap[R] > heap[max]) max = R;
if (max!=i){
int t = heap[max];
heap[max] = heap[i];
heap[i] = t;
maxHeapify(max);
}
}
//輸出堆
public void print(){
for (int i = 0; i < this.size; i++) {
System.out.print(heap[i]+" ");
}
System.out.println();
}
}
測(cè)試
public class Heap {
static int[] a = {5,3,6,4,2,1};
static int n = a.length;
public static void main(String[] args){
HeapNode heapNode = new HeapNode(n);
for (int i = 0; i < n; i++) {
heapNode.maxInsert(a[i]);
}
heapNode.print();
for (int i = 0; i < n; i++) {
int min = heapNode.maxDelete();
System.out.print("堆頂:"+min+" 剩下堆元素:");
heapNode.print();
}
}
}
結(jié)果

到此這篇關(guān)于詳解Java如何實(shí)現(xiàn)小頂堆和大頂堆的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)小頂堆和大頂堆內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決springboot啟動(dòng)Logback報(bào)錯(cuò)ERROR in ch.qos.logback.cla
這篇文章主要介紹了解決springboot啟動(dòng)Logback報(bào)錯(cuò)ERROR in ch.qos.logback.classic.joran.action.ContextNameAction - Failed to rena問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Java關(guān)于BeabUtils.copyproperties的用法
這篇文章主要介紹了Java關(guān)于BeabUtils.copyproperties的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
jenkins按模塊進(jìn)行構(gòu)建遇到的問(wèn)題及解決方案
這篇文章主要介紹了jenkins按模塊進(jìn)行構(gòu)建的問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
SpringBoot 如何整合 ES 實(shí)現(xiàn) CRUD 操作
這篇文章主要介紹了SpringBoot 如何整合 ES 實(shí)現(xiàn) CRUD 操作,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-10-10
淺談異常結(jié)構(gòu)圖、編譯期異常和運(yùn)行期異常的區(qū)別
下面小編就為大家?guī)?lái)一篇淺談異常結(jié)構(gòu)圖、編譯期異常和運(yùn)行期異常的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09

