当前位置:网站首页>Priority queue (heap)
Priority queue (heap)
2022-07-06 00:01:00 【Snail at the end of the pyramid】
Catalog
The sequential storage of binary trees
storage
Use array to save binary tree structure , Method is to put the binary tree into the array by sequence traversal .
Generally, it is only suitable for representing complete binary tree , Because incomplete binary trees will waste space .
The main use of this method is the representation of the heap .
Subscript relation
- Known parents (parent) The subscript , be :
Left the child (left) Subscript = 2 * parent + 1;
The right child (right) Subscript = 2 * parent + 2; - Known child ( There's no distinction between left and right )(child) Subscript , be :
Parents (parent) Subscript = (child - 1) / 2;
Pile up
Concept
- The heap is logically a complete binary tree
- The heap is physically stored in an array
- Satisfy that the value of any node is greater than the value of the node in its subtree , It's called a lot of , Or a big pile , Or the largest heap . conversely , It's a small pile , Or a small pile of roots , Or the smallest heap .
Basic operation
Building the heap
public void creatBigHeap(int[] array) {
for(int i=0;i<array.length;i++) {
elem[i]=array[i];
usedSize++;
}
for(int parent=(usedSize-1-1)/2;parent>=0;parent--) {
// adjustment
shiftDown(parent,usedSize);
}
}
//parent: The root node of each tree len: The end position of each tree adjustment
public void shiftDown(int parent,int len) {
int child=2*parent+1;
while(child < len) {
if( child+1 < len &&elem[child] < elem[child+1]) {
child++;// Ensure the current subscript of the maximum value of the left and right children
}
if(elem[child] > elem[parent]) {
int tmp=elem[child];
elem[child]=elem[parent];
elem[parent]=tmp;
parent=child;
child=2*parent+1;
}else {
break;
}
}
}
The team
// Queue entry ( Take a large pile as an example )
public void offer(int val) {
if(isFull()) {
// Capacity expansion
elem=Arrays.copyOf(elem, elem.length *2);
}
elem[usedSize]=val;
usedSize++;
// Notice what's coming in here usedSize-1
shiftUp(usedSize-1);
}
public boolean isFull() {
return usedSize==elem.length;
}
// Adjust up
public void shiftUp(int child) {
int parent=(child-1)/2;
while(child >0) {
if(elem[child] > elem[parent]) {
int tmp=elem[child];
elem[child]=elem[parent];
elem[parent]=tmp;
child=parent;
parent=(child-1)/2;
}else {
break;
}
}
}
Out of the team
// Out of the team
public int poll() {
if(isEmpty()) {
throw new RuntimeException(" Priority queue is empty ");
}
int tmp=elem[0];
elem[0]=elem[usedSize-1];
elem[usedSize-1]=tmp;
usedSize--;
shiftDown(0,usedSize);
// Return the element to be dequeued
return tmp;
}
public boolean isEmpty() {
return usedSize==0;
}
public void shiftDown(int parent,int len) {
int child=2*parent+1;
while(child < len) {
if( child+1 < len &&elem[child] < elem[child+1]) {
child++;// Ensure the current subscript of the maximum value of the left and right children
}
if(elem[child] > elem[parent]) {
int tmp=elem[child];
elem[child]=elem[parent];
elem[parent]=tmp;
parent=child;
child=2*parent+1;
}else {
break;
}
}
}
Get team leader elements
// Get team leader elements
public int peek() {
if(isEmpty()) {
throw new RuntimeException(" Priority queue is empty ");
}
return elem[0];
}
top-K problem
Heap sort
public void heapSort() {
int end=usedSize-1;
while(end>0) {
int tmp=elem[0];
elem[0]=elem[end];
elem[end]=tmp;
shiftDown(0,end);
end--;
}
}
边栏推荐
- QT -- thread
- 【在线聊天】原来微信小程序也能回复Facebook主页消息!
- Spire Office 7.5.4 for NET
- 2022.7.5-----leetcode.729
- What if the C disk is not enough? Let's see how I can clean up 25g of temp disk space after I haven't redone the system for 4 years?
- How to rotate the synchronized / refreshed icon (EL icon refresh)
- C # input how many cards are there in each of the four colors.
- 妙才周刊 - 8
- Research notes I software engineering and calculation volume II (Chapter 1-7)
- JVM details
猜你喜欢
Wechat applet -- wxml template syntax (with notes)
Doppler effect (Doppler shift)
My colleagues quietly told me that flying Book notification can still play like this
CAS and synchronized knowledge
FFmpeg学习——核心模块
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
Key structure of ffmpeg - avframe
提升工作效率工具:SQL批量生成工具思想
Detailed explanation of APP functions of door-to-door appointment service
Bao Yan notebook IV software engineering and calculation volume II (Chapter 8-12)
随机推荐
跟着CTF-wiki学pwn——ret2libc1
Yunna | what are the main operating processes of the fixed assets management system
Doppler effect (Doppler shift)
FFT 学习笔记(自认为详细)
15 MySQL stored procedures and functions
【luogu P3295】萌萌哒(并查集)(倍增)
shardingsphere源码解析
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
[designmode] composite mode
Use CAS instead of synchronized
Gd32f4xx UIP protocol stack migration record
转:未来,这样的组织才能扛住风险
Qt QPushButton详解
数据库遇到的问题
How much do you know about the bank deposit business that software test engineers must know?
Open3D 点云随机添加噪声
The use of El cascader and the solution of error reporting
Determinant learning notes (I)
Qt 一个简单的word文档编辑器
DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明