当前位置:网站首页>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--;
}
}
边栏推荐
- VBA fast switching sheet
- How to rotate the synchronized / refreshed icon (EL icon refresh)
- 单商户V4.4,初心未变,实力依旧!
- wx.getLocation(Object object)申请方法,最新版
- Fiddler Everywhere 3.2.1 Crack
- 14 MySQL view
- 【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
- 云呐|固定资产管理系统功能包括哪些?
- [online chat] the original wechat applet can also reply to Facebook homepage messages!
- PADS ROUTER 使用技巧小记
猜你喜欢

Transport layer protocol ----- UDP protocol

What are the functions of Yunna fixed assets management system?

CAS and synchronized knowledge

4 points tell you the advantages of the combination of real-time chat and chat robots

【二叉搜索树】增删改查功能代码实现
![[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)](/img/d6/c3128e26d7e629b7f128c551cd03a7.png)
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
![[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!

Senparc.Weixin.Sample.MP源码剖析

My colleagues quietly told me that flying Book notification can still play like this

PV静态创建和动态创建
随机推荐
Upgrade openssl-1.1.1p for openssl-1.0.2k
Open source CRM customer relationship system management system source code, free sharing
[Luogu cf487e] tours (square tree) (tree chain dissection) (line segment tree)
Mathematical model Lotka Volterra
Problems encountered in the database
Use mapper: --- tkmapper
用列表初始化你的vector&&initializer_list简介
多普勒效应(多普勒频移)
跟着CTF-wiki学pwn——ret2libc1
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
Cloudcompare & PCL point cloud randomly adds noise
Zhuan: in the future, such an organization can withstand the risks
5. Logistic regression
用列錶初始化你的vector&&initializer_list簡介
Add noise randomly to open3d point cloud
Tips for using pads router
What are Yunna's fixed asset management systems?
【在线聊天】原来微信小程序也能回复Facebook主页消息!
【SQL】各主流数据库sql拓展语言(T-SQL 、 PL/SQL、PL/PGSQL)
PV static creation and dynamic creation