当前位置:网站首页>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;
}
}
边栏推荐
- Azkaban安装部署
- Basic authorization command for Curl
- This + closure + scope interview question
- LeetCode 234. Palindrome linked list
- Yyds dry goods inventory embedded matrix
- 040. (2.9) relieved
- 2021 Li Hongyi machine learning (3): what if neural network training fails
- 1. Five layer network model
- Zabbix
- Design and implementation of community hospital information system
猜你喜欢
Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
Flume configuration 4 - customize mysqlsource
Linux安装Redis
Pat grade a 1119 pre- and post order traversals (30 points)
Vb+access hotel service management system
New interesting test applet source code_ Test available
Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers
Port, domain name, protocol.
Azkaban installation and deployment
Class inheritance in C #
随机推荐
[micro service SCG] 33 usages of filters
Usage scenarios and solutions of ledger sharing
Kbp206-asemi rectifier bridge kbp206
Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme
1. Five layer network model
Talk about the SQL server version of DTM sub transaction barrier function
【微服务|SCG】Filters的33种用法
Structure of ViewModel
Design of KTV intelligent dimming system based on MCU
Yyds dry goods inventory embedded matrix
Clean up PHP session files
Good documentation
this+闭包+作用域 面试题
Talk about the SQL server version of DTM sub transaction barrier function
Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
Pat grade a 1119 pre- and post order traversals (30 points)
Design and implementation of community hospital information system
Why is this an undefined behavior- Why is this an undefined behavior?
el-select,el-option下拉选择框
Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan