当前位置:网站首页>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--;
}
}
边栏推荐
- What are Yunna's fixed asset management systems?
- 跟着CTF-wiki学pwn——ret2libc1
- Effet Doppler (déplacement de fréquence Doppler)
- What is information security? What is included? What is the difference with network security?
- 数据库遇到的问题
- 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?
- 云呐|固定资产管理系统功能包括哪些?
- 用列表初始化你的vector&&initializer_list简介
- 用列錶初始化你的vector&&initializer_list簡介
- Make a short video clip number of we media film and television. Where can I download the material?
猜你喜欢

Spire Office 7.5.4 for NET

选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】

Tips for using pads router

零犀科技携手集智俱乐部:“因果派”论坛成功举办,“因果革命”带来下一代可信AI
![[online chat] the original wechat applet can also reply to Facebook homepage messages!](/img/d2/1fd4de4bfd433ed397c236ddb97a66.png)
[online chat] the original wechat applet can also reply to Facebook homepage messages!

MySQL之函数

FFMPEG关键结构体——AVFrame

XML配置文件(DTD详细讲解)

GD32F4xx uIP协议栈移植记录

Configuring OSPF load sharing for Huawei devices
随机推荐
认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
Gd32f4xx UIP protocol stack migration record
shardingsphere源码解析
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
QT QPushButton details
Open3D 点云随机添加噪声
传输层协议------UDP协议
妙才周刊 - 8
What are the functions of Yunna fixed assets management system?
[Luogu p3295] mengmengda (parallel search) (double)
Spire. PDF for NET 8.7.2
Senparc. Weixin. Sample. MP source code analysis
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)
[Luogu cf487e] tours (square tree) (tree chain dissection) (line segment tree)
Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI
[designmode] adapter pattern
Zhongjun group launched electronic contracts to accelerate the digital development of real estate enterprises
Bao Yan notes II software engineering and calculation volume II (Chapter 13-16)
MySQL global lock and table lock