当前位置:网站首页>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;
}
}
边栏推荐
- Kubernetes - Multi cluster management
- [system security] ten thousand words summary system virtualization container bottom layer principle experiment
- Clean up PHP session files
- What is the most effective way to convert int to string- What is the most efficient way to convert an int to a String?
- New interesting test applet source code_ Test available
- Zero foundation uses paddlepaddle to build lenet-5 network
- C file in keil cannot be compiled
- SQL performance optimization skills
- Sqoop command
- Is there any way to change the height of the uinavigationbar in the storyboard without using the UINavigationController?
猜你喜欢

Devtools的简单使用

Huawei MPLS experiment

腾讯云,实现图片上传

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector

qrcode:将文本生成二维码

Bumblebee: build, deliver, and run ebpf programs smoothly like silk

ELK日志分析系统
![[groovy] loop control (number injection function implements loop | times function | upto function | downto function | step function | closure can be written outside as the final parameter)](/img/45/6cb796364efe16d54819ac10fb7d05.jpg)
[groovy] loop control (number injection function implements loop | times function | upto function | downto function | step function | closure can be written outside as the final parameter)

Sqoop安装
随机推荐
Qrcode: generate QR code from text
Spark SQL learning bullet 2
Use of kubesphere configuration set (configmap)
040. (2.9) relieved
Tencent cloud, realize image upload
Elfk deployment
平台入驻与独立部署优缺点对比
Flume configuration 4 - customize mysqlsource
Elk log analysis system
2021 Li Hongyi machine learning (2): pytorch
Une question est de savoir si Flink SQL CDC peut définir le parallélisme. Si le parallélisme est supérieur à 1, il y aura un problème d'ordre?
In MySQL Association query, the foreign key is null. What if the data cannot be found?
Zero foundation uses paddlepaddle to build lenet-5 network
Delphi free memory
Azkaban实战
Azkaban安装部署
PHP cli getting input from user and then dumping into variable possible?
返回二叉树中两个节点的最低公共祖先
Pdf things
Machine learning experiment report 1 - linear model, decision tree, neural network part