2023-01-08 22:18:23 +08:00
|
|
|
/**
|
|
|
|
* File: my_heap.java
|
|
|
|
* Created Time: 2023-01-07
|
|
|
|
* Author: Krahets (krahets@163.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
package chapter_heap;
|
|
|
|
|
|
|
|
import include.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
2023-02-07 19:05:19 +08:00
|
|
|
/* 大顶堆 */
|
2023-01-08 22:18:23 +08:00
|
|
|
class MaxHeap {
|
|
|
|
// 使用列表而非数组,这样无需考虑扩容问题
|
|
|
|
private List<Integer> maxHeap;
|
|
|
|
|
2023-02-10 01:04:26 +08:00
|
|
|
/* 构造方法,根据输入列表建堆 */
|
2023-01-08 22:18:23 +08:00
|
|
|
public MaxHeap(List<Integer> nums) {
|
2023-01-16 19:30:54 +08:00
|
|
|
// 将列表元素原封不动添加进堆
|
2023-01-08 22:18:23 +08:00
|
|
|
maxHeap = new ArrayList<>(nums);
|
2023-04-09 04:32:17 +08:00
|
|
|
// 堆化除叶节点以外的其他所有节点
|
2023-01-08 22:18:23 +08:00
|
|
|
for (int i = parent(size() - 1); i >= 0; i--) {
|
2023-01-09 02:17:40 +08:00
|
|
|
siftDown(i);
|
2023-01-08 22:18:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 获取左子节点索引 */
|
2023-01-08 22:18:23 +08:00
|
|
|
private int left(int i) {
|
|
|
|
return 2 * i + 1;
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 获取右子节点索引 */
|
2023-01-08 22:18:23 +08:00
|
|
|
private int right(int i) {
|
|
|
|
return 2 * i + 2;
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 获取父节点索引 */
|
2023-01-08 22:18:23 +08:00
|
|
|
private int parent(int i) {
|
2023-01-09 02:17:40 +08:00
|
|
|
return (i - 1) / 2; // 向下整除
|
2023-01-08 22:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 交换元素 */
|
|
|
|
private void swap(int i, int j) {
|
2023-04-14 00:12:10 +08:00
|
|
|
int a = maxHeap.get(i);
|
|
|
|
int b = maxHeap.get(j);
|
|
|
|
int tmp = a;
|
2023-01-08 22:18:23 +08:00
|
|
|
maxHeap.set(i, b);
|
|
|
|
maxHeap.set(j, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
public int size() {
|
|
|
|
return maxHeap.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 访问堆顶元素 */
|
|
|
|
public int peek() {
|
|
|
|
return maxHeap.get(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
public void push(int val) {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 添加节点
|
2023-01-08 22:18:23 +08:00
|
|
|
maxHeap.add(val);
|
|
|
|
// 从底至顶堆化
|
2023-01-09 02:17:40 +08:00
|
|
|
siftUp(size() - 1);
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-01-09 02:17:40 +08:00
|
|
|
private void siftUp(int i) {
|
2023-01-08 22:18:23 +08:00
|
|
|
while (true) {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 获取节点 i 的父节点
|
2023-01-08 22:18:23 +08:00
|
|
|
int p = parent(i);
|
2023-04-09 04:32:17 +08:00
|
|
|
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
2023-01-08 22:18:23 +08:00
|
|
|
if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))
|
|
|
|
break;
|
2023-04-09 04:32:17 +08:00
|
|
|
// 交换两节点
|
2023-01-08 22:18:23 +08:00
|
|
|
swap(i, p);
|
2023-01-09 02:17:40 +08:00
|
|
|
// 循环向上堆化
|
2023-01-08 22:18:23 +08:00
|
|
|
i = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:31:05 +08:00
|
|
|
public int pop() {
|
2023-01-08 22:18:23 +08:00
|
|
|
// 判空处理
|
|
|
|
if (isEmpty())
|
|
|
|
throw new EmptyStackException();
|
2023-04-09 04:32:17 +08:00
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-01-08 22:18:23 +08:00
|
|
|
swap(0, size() - 1);
|
2023-04-09 04:32:17 +08:00
|
|
|
// 删除节点
|
2023-01-08 22:18:23 +08:00
|
|
|
int val = maxHeap.remove(size() - 1);
|
|
|
|
// 从顶至底堆化
|
2023-01-09 02:17:40 +08:00
|
|
|
siftDown(0);
|
2023-01-08 22:18:23 +08:00
|
|
|
// 返回堆顶元素
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-01-09 02:17:40 +08:00
|
|
|
private void siftDown(int i) {
|
2023-01-08 22:18:23 +08:00
|
|
|
while (true) {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-01-08 22:18:23 +08:00
|
|
|
int l = left(i), r = right(i), ma = i;
|
2023-01-09 02:17:40 +08:00
|
|
|
if (l < size() && maxHeap.get(l) > maxHeap.get(ma))
|
|
|
|
ma = l;
|
|
|
|
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
|
|
|
|
ma = r;
|
2023-04-09 04:32:17 +08:00
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
2023-04-14 00:12:10 +08:00
|
|
|
if (ma == i)
|
|
|
|
break;
|
2023-04-09 04:32:17 +08:00
|
|
|
// 交换两节点
|
2023-01-08 22:18:23 +08:00
|
|
|
swap(i, ma);
|
|
|
|
// 循环向下堆化
|
|
|
|
i = ma;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 打印堆(二叉树) */
|
|
|
|
public void print() {
|
|
|
|
Queue<Integer> queue = new PriorityQueue<>((a, b) -> { return b - a; });
|
|
|
|
queue.addAll(maxHeap);
|
|
|
|
PrintUtil.printHeap(queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class my_heap {
|
|
|
|
public static void main(String[] args) {
|
2023-01-12 04:08:45 +08:00
|
|
|
/* 初始化大顶堆 */
|
|
|
|
MaxHeap maxHeap = new MaxHeap(Arrays.asList(9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2));
|
|
|
|
System.out.println("\n输入列表并建堆后");
|
|
|
|
maxHeap.print();
|
2023-01-08 22:18:23 +08:00
|
|
|
|
|
|
|
/* 获取堆顶元素 */
|
|
|
|
int peek = maxHeap.peek();
|
|
|
|
System.out.format("\n堆顶元素为 %d\n", peek);
|
|
|
|
|
2023-01-12 04:08:45 +08:00
|
|
|
/* 元素入堆 */
|
|
|
|
int val = 7;
|
|
|
|
maxHeap.push(val);
|
|
|
|
System.out.format("\n元素 %d 入堆后\n", val);
|
|
|
|
maxHeap.print();
|
|
|
|
|
2023-01-09 02:17:40 +08:00
|
|
|
/* 堆顶元素出堆 */
|
2023-03-13 22:31:05 +08:00
|
|
|
peek = maxHeap.pop();
|
2023-01-12 04:08:45 +08:00
|
|
|
System.out.format("\n堆顶元素 %d 出堆后\n", peek);
|
|
|
|
maxHeap.print();
|
2023-01-08 22:18:23 +08:00
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
int size = maxHeap.size();
|
|
|
|
System.out.format("\n堆元素数量为 %d\n", size);
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
|
|
|
boolean isEmpty = maxHeap.isEmpty();
|
|
|
|
System.out.format("\n堆是否为空 %b\n", isEmpty);
|
|
|
|
}
|
|
|
|
}
|