当前位置:网站首页>堆(優先級隊列)
堆(優先級隊列)
2022-07-02 12:07:00 【菜菜不恰菜】

目錄
🥬堆的性質

總結:一顆完全二叉樹以層序遍曆方式放入數組中存儲,這種方式的主要用法就是堆的錶示。
🥬堆的分類


🥬堆的向下調整
現在有一個數組,邏輯上是完全二叉樹,我們通過從根節點開始的向下調整算法可以把它調整成一個小堆或者大堆。向下調整算法有一個前提:左右子樹必須是一個堆,才能調整。
以小堆為例:
1、先讓左右孩子結點比較,取最小值。
2、用較小的那個孩子結點與父親節點比較,如果孩子結點<父親節點,交換,反之,不交換。
3、循環往複,如果孩子結點的下標越界,則說明已經到了最後,就結束。

代碼示例:
//parent: 每棵樹的根節點
//len: 每棵樹的調整的結束比特置
public void shiftDown(int parent,int len){
int child=parent*2+1; //因為堆是完全二叉樹,沒有左孩子就一定沒有右孩子,所以最起碼是有左孩子的,至少有1個孩子
while(child<len){
if(child+1<len && elem[child]<elem[child+1]){
child++;//兩孩子結點比較取較小值
}
if(elem[child]<elem[parent]){
int tmp=elem[parent];
elem[parent]=elem[child];
elem[child]=tmp;
parent=child;
child=parent*2+1;
}else{
break;
}
}
}🥬堆的建立
public void creatHeap(int[] arr){
for(int i=0;i<arr.length;i++){
elem[i]=arr[i];
useSize++;
}
for(int parent=(useSize-1-1)/2;parent>=0;parent--){//數組下標從0開始
shiftDown(parent,useSize);
}
}建堆的空間複雜度為O(N),因為堆為一棵完全二叉樹,滿二叉樹也是一種完全二叉樹,我們用滿二叉樹(最壞情况下)來證明。

🥬堆得向上調整
現在有一個堆,我們需要在堆的末尾插入數據,再對其進行調整,使其仍然保持堆的結構,這就是向上調整。
以大堆為例:

代碼示例:
public void shiftup(int child){
int parent=(child-1)/2;
while(child>0){
if(elem[child]>elem[parent]){
int tmp=elem[parent];
elem[parent]=elem[child];
elem[child]=tmp;
child=parent;
parent=(child-1)/2;
}else{
break;
}
}
}🥬堆的常用操作
入隊列
往堆裏面加入元素,就是往最後一個比特置加入,然後在進行向上調整。
public boolean isFull(){
return elem.length==useSize;
}
public void offer(int val){
if(isFull()){
elem= Arrays.copyOf(elem,2*elem.length);//擴容
}
elem[useSize++]=val;
shiftup(useSize-1);
}出隊列
把堆裏元素删除,就把堆頂元素和最後一個元素交換,然後向整個數組大小减一,最後向下調整,就删除了棧頂元素。
public boolean isEmpty(){
return useSize==0;
}
public int poll(){
if(isEmpty()){
throw new RuntimeException("優先級隊列為空");
}
int tmp=elem[0];
elem[0]=elem[useSize-1];
elem[useSize-1]=tmp;
useSize--;
shiftDown(0,useSize);
return tmp;
}獲取隊首元素
public int peek() {
if (isEmpty()) {
throw new RuntimeException("優先級隊列為空");
}
return elem[0];
}🥬TopK 問題
給你6個數據,求前3個最大數據。這時候我們用堆怎麼做的?
解題思路:
1、如果求前K個最大的元素,要建一個小根堆。
2、如果求前K個最小的元素,要建一個大根堆。
3、第K大的元素。建一個小堆,堆頂元素就是第K大的元素。
4、第K小的元素。建一個大堆,堆頂元素就是第K大的元素。
舉個例子:求前n個最大數據

代碼示例:
public static int[] topK(int[] array,int k){
//創建一個大小為K的小根堆
PriorityQueue<Integer> minHeap=new PriorityQueue<>(k, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
});
//遍曆數組中元素,將前k個元素放入隊列中
for(int i=0;i<array.length;i++){
if(minHeap.size()<k){
minHeap.offer(array[i]);
}else{
//從k+1個元素開始,分別和堆頂元素比較
int top=minHeap.peek();
if(array[i]>top){
//先彈出後存入
minHeap.poll();
minHeap.offer(array[i]);
}
}
}
//將堆中元素放入數組中
int[] tmp=new int[k];
for(int i=0;i< tmp.length;i++){
int top=minHeap.poll();
tmp[i]=top;
}
return tmp;
}
public static void main(String[] args) {
int[] array={12,8,23,6,35,22};
int[] tmp=topK(array,3);
System.out.println(Arrays.toString(tmp));
}結果:

數組排序
再者說如果要對一個數組進行從小到大排序,要借助大根堆還是小根堆呢?
---->大根堆

代碼示例:
public void heapSort(){
int end=useSize-1;
while(end>0){
int tmp=elem[0];
elem[0]=elem[end];
elem[end]=tmp;
shiftDown(0,end);//假設這裏向下調整為大根堆
end--;
}
}🥬小結
以上就是今天的內容了,有什麼問題可以在評論區留言

边栏推荐
- GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
- uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
- How does Premiere (PR) import the preset mogrt template?
- YYGH-BUG-05
- 高德地图测试用例
- SVO2系列之深度滤波DepthFilter
- Seriation in R: How to Optimally Order Objects in a Data Matrice
- Jenkins user rights management
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- mysql索引和事务
猜你喜欢

Some problems encountered in introducing lvgl into esp32 Arduino

文件操作(详解!)

堆(优先级队列)

基于Arduino和ESP8266的连接手机热点实验(成功)

Pytorch builds LSTM to realize clothing classification (fashionmnist)

mysql索引和事务
![[visual studio 2019] create and import cmake project](/img/51/6c2575030c5103aee6c02bec8d5e77.jpg)
[visual studio 2019] create and import cmake project

HR wonderful dividing line

动态内存(进阶四)

Take you ten days to easily finish the finale of go micro services (distributed transactions)
随机推荐
File operation (detailed!)
字符串回文hash 模板题 O(1)判字符串是否回文
子线程获取Request
Flesh-dect (media 2021) -- a viewpoint of material decomposition
Repeat, tile and repeat in pytorch_ The difference between interleave
Data analysis - Matplotlib sample code
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
jenkins 凭证管理
史上最易懂的f-string教程,收藏這一篇就够了
B high and beautiful code snippet sharing image generation
On data preprocessing in sklearn
Test shift left and right
Power Spectral Density Estimates Using FFT---MATLAB
YYGH-BUG-05
还不会安装WSL 2?看这一篇文章就够了
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
Leetcode209 长度最小的子数组
多文件程序X32dbg动态调试
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
ESP32 Arduino 引入LVGL 碰到的一些问题