当前位置:网站首页>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--;
}
}
边栏推荐
- Shardingsphere source code analysis
- After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
- FFMPEG关键结构体——AVCodecContext
- Huawei equipment configuration ospf-bgp linkage
- [Luogu p3295] mengmengda (parallel search) (double)
- Problems encountered in the database
- wx.getLocation(Object object)申请方法,最新版
- How to get all the values stored in localstorage
- Use mapper: --- tkmapper
- 选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】
猜你喜欢
C reflection and type
What are the functions of Yunna fixed assets management system?
【DesignMode】组合模式(composite mode)
单商户V4.4,初心未变,实力依旧!
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
提升工作效率工具:SQL批量生成工具思想
选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】
How to get all the values stored in localstorage
18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
Bao Yan notebook IV software engineering and calculation volume II (Chapter 8-12)
随机推荐
20220703 week race: number of people who know the secret - dynamic rules (problem solution)
14 MySQL view
[day39 literature extensive reading] a Bayesian perspective on magnetic estimation
云呐|固定资产管理系统功能包括哪些?
Senparc. Weixin. Sample. MP source code analysis
妙才周刊 - 8
软件测试工程师必会的银行存款业务,你了解多少?
Redis high availability - master-slave replication, sentinel mode, cluster
Initialize your vector & initializer with a list_ List introduction
Gd32f4xx UIP protocol stack migration record
mysql-全局锁和表锁
[binary search tree] add, delete, modify and query function code implementation
Use mapper: --- tkmapper
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
FFT 学习笔记(自认为详细)
CloudCompare&PCL 点云随机添加噪声
JS can really prohibit constant modification this time!
shardingsphere源码解析
Upgrade openssl-1.1.1p for openssl-1.0.2k
Permission problem: source bash_ profile permission denied