Java利用完全二叉樹創(chuàng)建大根堆和小根堆
大根堆
大根堆:每個(gè)結(jié)點(diǎn)的值不大于他的父親結(jié)點(diǎn)的值
分析如下:
假設(shè)對{ 27,15,19,18,28,34,65,49,25,37 }這樣一個(gè)集合的數(shù)據(jù)創(chuàng)建成堆;








代碼如下:
//建立大根堆
public class TestHeap{
public int[] array;
public int usedSize;//當(dāng)前有效數(shù)組長度
public TestHeap(){
this.array = new int[10];
this.usedSize = 0;
}
//初始化數(shù)組
public void InitArray(int[] arrayClone){
array = Arrays.copyOf(arrayClone, arrayClone.length);
usedSize = array.length;
}
//創(chuàng)建大根堆
public void createHeap(){
for(int parent = (usedSize - 1 - 1) / 2; parent >= 0; parent--){
adjustment(parent, usedSize);
}
}
//調(diào)整
public void adjustment(int parent, int len){
//左子樹結(jié)點(diǎn)下標(biāo)
int child = parent * 2 + 1;
//調(diào)整
while(child < len){
//先判斷有沒有右孩子,如果右,找出最大值
if(child + 1 < len && array[child] < array[child + 1]){
child++;//如果右子樹大,child++就指向他,如果左子樹大,就不用管,直接進(jìn)行下一步判斷交換
}
//若左右子樹的最大值大于父親結(jié)點(diǎn)則交換
if(array[child] > array[parent]){
swap(array, child, parent);
parent = child;
child = parent * 2 + 1;
} else{
break;
}
}
}
//交換
public void swap(int[] array, int child, int parent){
int tmp = array[child];
array[child] = array[parent];
array[parent] = tmp;
}
}小根堆
小根堆:每個(gè)結(jié)點(diǎn)的值不小于他的父親結(jié)點(diǎn)的值
分析與大根堆類似,只是比較出更小的進(jìn)行替換
代碼如下:
//建立大根堆
public class TestHeap{
public int[] array;
public int usedSize;//當(dāng)前有效數(shù)組長度
public TestHeap(){
this.array = new int[10];
this.usedSize = 0;
}
//初始化數(shù)組
public void InitArray(int[] arrayClone){
array = Arrays.copyOf(arrayClone, arrayClone.length);
usedSize = array.length;
}
//創(chuàng)建大根堆
public void createHeap(){
for(int parent = (usedSize - 1 - 1) / 2; parent >= 0; parent--){
adjustment(parent, usedSize);
}
}
//調(diào)整
public void adjustment(int parent, int len){
//左子樹結(jié)點(diǎn)下標(biāo)
int child = parent * 2 + 1;
//調(diào)整
while(child < len){
//先判斷有沒有右孩子,如果右,找出最小值
if(child + 1 < len && array[child] > array[child + 1]){
child++;//如果右子樹小,child++就指向他,如果左子樹小,就不用管,直接進(jìn)行下一步判斷交換
}
//若左右子樹的最大值小于父親結(jié)點(diǎn)則交換
if(array[child] < array[parent]){
swap(array, child, parent);
parent = child;
child = parent * 2 + 1;
} else{
break;
}
}
}
//交換
public void swap(int[] array, int child, int parent){
int tmp = array[child];
array[child] = array[parent];
array[parent] = tmp;
}
}到此這篇關(guān)于Java利用完全二叉樹創(chuàng)建大根堆和小根堆的文章就介紹到這了,更多相關(guān)Java大根堆 小根堆內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud Gateway實(shí)現(xiàn)限流功能詳解
SpringCloud Gateway 是 Spring Cloud 的一個(gè)全新項(xiàng)目,它旨在為微服務(wù)架構(gòu)提供一種簡單有效的統(tǒng)一的 API 路由管理方式。這篇文章主要介紹了SpringCloud Gateway實(shí)現(xiàn)限流,需要的朋友可以參考下2022-11-11
詳解Spring整合Quartz實(shí)現(xiàn)動態(tài)定時(shí)任務(wù)
本篇文章主要介紹了詳解Spring整合Quartz實(shí)現(xiàn)動態(tài)定時(shí)任務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
java方法重寫和super關(guān)鍵字實(shí)例詳解
這篇文章主要介紹了java方法重寫和super關(guān)鍵字實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
詳解Springboot集成sentinel實(shí)現(xiàn)接口限流入門
這篇文章主要介紹了詳解Springboot集成sentinel實(shí)現(xiàn)接口限流入門,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
詳解springMVC兩種方式實(shí)現(xiàn)多文件上傳及效率比較
本篇文章介紹了springMVC兩種方式實(shí)現(xiàn)多文件上傳及效率比較。springMVC實(shí)現(xiàn)多文件上傳有兩種,一種是字節(jié)流的方式進(jìn)行文件上傳,另外一種是使用springMVC包裝好的解析器進(jìn)行上傳,有興趣的可以了解一下。2016-12-12
微服務(wù)架構(gòu)設(shè)計(jì)RocketMQ基礎(chǔ)及環(huán)境整合
這篇文章主要介紹了微服務(wù)架構(gòu)設(shè)計(jì)入門RocketMQ的基礎(chǔ)及環(huán)境整合實(shí)現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
java結(jié)合WebSphere MQ實(shí)現(xiàn)接收隊(duì)列文件功能
WebSphereMQ,也稱MQSeries,以一致的、可靠的和易于管理的方式來連接應(yīng)用程序,并為跨部門、企業(yè)范圍的集成提供了可靠的基礎(chǔ)。通過為重要的消息和事務(wù)提供可靠的、一次且僅一次的傳遞,MQ可以處理復(fù)雜的通信協(xié)議,并動態(tài)地將消息傳遞工作負(fù)載分配給可用的資源。2015-10-10

