当前位置:网站首页>Binary heap implementation (priority queue implementation)
Binary heap implementation (priority queue implementation)
2022-07-05 03:20:00 【Ape baby】
package heap;
/** * @author: huzc * @date: 2022/2/13 15:28 * @description: Priority queue implementation */
public class MaxPQ<K extends Comparable<K>>{
private K[] array;
// Element number
private int n = 0;
public MaxPQ(int cap) {
array = (K[]) new Comparable[cap + 1];
}
public K max() {
return array[1];
}
private void swim(int k) {
// If you float to the top of the pile , You can't go up again
while(k > 1 && less(parent(k),k)) {
// If the first k The elements are bigger than the upper layer
// take k Change it
swap(parent(k),k);
k = parent(k);
}
}
private void sink(int k) {
// If it sinks to the bottom of the pile , It doesn't sink
while (left(k) <= n) {
// Let's assume that the left node is larger
int older = left(k);
// If there is a node on the right , Compare the size
if (right(k) <= n && less(older, right(k)))
older = right(k);
// node k Older than both children , You don't have to sink
if (less(older, k)) break;
// otherwise , Does not conform to the structure of the largest heap , sinking k node
swap(k, older);
k = older;
}
}
public void insert(K e) {
n++;
// Add new elements to the end
array[n] = e;
// And let it float up to the right position
swim(n);
}
public K delMax() {
// The top of the largest heap is the largest element
K max = array[1];
// Change the biggest element to the last , Delete
swap(1,n);
array[n] = null;
// Give Way array[1] Sink to the right position
sink(1);
return max;
}
private void swap(int i, int j) {
K temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private boolean less(int i, int j) {
return array[i].compareTo(array[j]) < 0;
}
public int parent(int root) {
return root / 2;
}
public int left(int root) {
return root * 2;
}
public int right(int root) {
return root * 2 + 1;
}
}
边栏推荐
- 看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事
- LeetCode 234. Palindrome linked list
- 2021 Li Hongyi machine learning (2): pytorch
- SFTP cannot connect to the server # yyds dry goods inventory #
- [Chongqing Guangdong education] 2777t green space planning reference questions of National Open University in autumn 2018
- Daily question 2 12
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- GFS分布式文件系统
- Vb+access hotel service management system
- SQL injection exercise -- sqli Labs
猜你喜欢
ELK日志分析系统
Sqoop命令
Azkaban实战
el-select,el-option下拉选择框
Pat grade a 1119 pre- and post order traversals (30 points)
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Design and practice of kubernetes cluster and application monitoring scheme
How to learn to get the embedding matrix e # yyds dry goods inventory #
1.五层网络模型
Devtools的簡單使用
随机推荐
Kubernetes - identity and authority authentication
Design and practice of kubernetes cluster and application monitoring scheme
Single box check box
How to make OS X read bash_ Profile instead of Profile file - how to make OS X to read bash_ profile not . profile file
Linux安装Redis
Pdf things
FBO and RBO disappeared in webgpu
2021 Li Hongyi machine learning (2): pytorch
How to learn to get the embedding matrix e # yyds dry goods inventory #
Flume配置4——自定义MYSQLSource
Performance of calling delegates vs methods
8. Commodity management - commodity classification
Anchor free series network yolox source code line by line explanation four (a total of ten, ensure line by line explanation, after reading, you can change the network at will, not just as a participan
Eight days of learning C language - while loop (embedded) (single chip microcomputer)
What is the most effective way to convert int to string- What is the most efficient way to convert an int to a String?
Share the newly released web application development framework based on blazor Technology
[2022 repair version] community scanning code into group activity code to drain the complete operation source code / connect the contract free payment interface / promote the normal binding of subordi
Yyds dry goods inventory embedded matrix
Cette ADB MySQL prend - elle en charge SQL Server?
Is there any way to change the height of the uinavigationbar in the storyboard without using the UINavigationController?