package class06;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* 只把标记1,和标记2,两处的大于号,改成小于号,就是小根堆。
*/
public class Code02_Heap2 {
static class MyMaxHeap {
private int[] heap;
private final int limit;//数组的长度
private int heapSize;//优先级队列的有效长度
public MyMaxHeap(int limit) {
heap = new int[limit];
this.limit = limit;
heapSize = 0;
}
public boolean isEmpty() {
return heapSize == 0;
}
public boolean isFull() {
return heapSize == limit;
}
//向堆中添加一个元素,并且使原来的堆,维持大根堆结构。
public void push(int value) {
if (heapSize == limit) {
throw new RuntimeException("your heap is full!");
}
heap[heapSize] = value;//将有效长度增加一个,并赋值为当前加入的值。
heapInsert(heap, heapSize++);//添加操作
}
//添加操作,优化之前的版本。
private void heapInsert0(int[] heap, int index) {
//如果一个父节点索引是i,则它的左孩子的索引是2*i-1,它的右孩子的索引是2*i+1。
int parentIndex = (index - 1) / 2;
//如果当前数大于它的父节点,就一直循环。
//跳出循环有两种情况://1.当前数小于等于父节点。2.当前数走到顶了,没有父节点了。它自己也是不大于它自己的。
while (heap[index] > heap[parentIndex]) {
swap(heap, index, parentIndex);//当前数和父节点,交换。
index = parentIndex;
parentIndex = (parentIndex - 1) / 2;//当前数来到父节点的位置。
}
}
//添加操作
private void heapInsert(int[] arr, int index) {
while (arr[index] < arr[(index - 1) / 2]) {//index和parentIndex比较。大就交换。小于等于就不交换。
swap(arr, index, (index - 1) / 2);
index = (index - 1) / 2;//index指针向上,来到新位置,即它的父节点的位置。
}
}
//弹出一个值。
//并且这个值是最大值。弹出它之后,继续维持大根堆的结构不变。
public int pop() {
int ans = heap[0];//先记录最大值
swap(heap, 0, --heapSize);//将0位置的值和最后一个有效位置的值,交换。limit值总数组的长度。heapSize是有效长度。
heapify(heap, 0, heapSize);//堆化。此时的heapSize已经是,原有效长度减一了。
return ans;//返回最大值
}
//堆化。即维持大根堆的结构。
private void heapify(int[] arr, int index, int heapSize) {
int left = 2 * index + 1;//左孩子的索引,等于二倍的当前索引再加一。left = 2 * i + 1。
while (left < heapSize) {//只要有左孩子,就循环。此时的heapSize已经是,原有效长度减一了。
//如果有右孩子,并且右孩子的值大于左孩子的值。那么largest为右孩子的索引,即left+1。
//否则largest为左孩子的索引,即left。
int largest = left + 1 < heapSize && arr[left + 1] </*>*/ arr[left] ? left + 1 : left;//标记1
//如果较大孩子的值,大于当前值。那么largest保持largest不变。
//否则就是,较大孩子的值小于等于当前值。那么largest就是当前数的索引,即index。
largest = arr[largest] </*>*/ arr[index] ? largest : index;//标记2
//如果largest等于index,则跳出循环。也就是较大孩子的值小于等于当前值,当然就停止后续的交换等操作。
if (largest == index) {
break;
}
//走到这,说明,当前数需要向下和较大孩子交换。
swap(arr, largest, index);
index = largest;//当前数的指针向下移动,来到较大孩子的位置。
left = 2 * index + 1;//当前数的左孩子,向下移动,来到新的位置。
}
}
public void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
public static class MaxHeapForTest {
private int[] arr;
private final int limit;
private int size;
public MaxHeapForTest(int l) {
arr = new int[l];
this.limit = l;
size = 0;
}
public void push(int value) {
if (size >= limit) {
throw new RuntimeException("heap is full!");
}
arr[size++] = value;
}
public int pop() {
int maxIndex = 0;
for (int index = 1; index < size; index++) {
if (arr[index] /*>*/ < arr[maxIndex]) {
maxIndex = index;
}
}
int ans = arr[maxIndex];
arr[maxIndex] = arr[--size];
return ans;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == limit;
}
}
//自定义比较器
public static class MyComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;//倒序
}
}
public static void main1(String[] args) {
int[] arr = {5, 1, 3, 4, 6, 9};
MyMaxHeap myMaxHeap = new MyMaxHeap(arr.length);
for (int i = 0; i < arr.length; i++) {
myMaxHeap.push(arr[i]);
}
while (!myMaxHeap.isEmpty()) {
System.out.println(myMaxHeap.pop());
}
}
public static void main(String[] args) {
//PriorityQueue:优先级队列,默认排序是小根堆。
//可以通过传入排序规则,将它改成大根堆。
// PriorityQueue<Integer> heap = new PriorityQueue<>();//小根堆
PriorityQueue<Integer> heap = new PriorityQueue<>(new MyComparator());//大根堆
heap.add(2);
heap.add(3);
heap.add(0);
heap.add(8);
while (!heap.isEmpty()) {
Integer poll = heap.poll();
System.out.println("poll = " + poll);
}
System.out.println("===================================");
int maxValue = 100;
int limit = 100;
int testTimes = 1000;
System.out.println("test start!");
for (int i = 0; i < testTimes; i++) {
int curLimit = (int) (Math.random() * limit) + 1;
MyMaxHeap myMaxHeap = new MyMaxHeap(curLimit);
MaxHeapForTest test = new MaxHeapForTest(curLimit);
int curOpTimes = (int) (Math.random() * limit);
for (int j = 0; j < curOpTimes; j++) {
if (myMaxHeap.isEmpty() != test.isEmpty()) {
System.out.println("oops1!");
}
if (myMaxHeap.isFull() != test.isFull()) {
System.out.println("oops2!");
}
if (myMaxHeap.isEmpty()) {
int curValue = (int) (Math.random() * maxValue);
myMaxHeap.push(curValue);
test.push(curValue);
} else if (myMaxHeap.isFull()) {
if (myMaxHeap.pop() != test.pop()) {
System.out.println("oops3!");
}
} else {
if (Math.random() < 0.5) {
int curValue = (int) (Math.random() * maxValue);
myMaxHeap.push(curValue);
test.push(curValue);
} else {
if (myMaxHeap.pop() != test.pop()) {
System.out.println("oops4!");
}
}
}
}
}
System.out.println("test end!");
}
}