package structures;
public class MaxHeap<E extends Comparable<E>> {
private Array<E> data;
public MaxHeap(int capacity) {
data = new Array<>(capacity);
}
public MaxHeap() {
this(10);
}
public MaxHeap(E[] arr) {
data = new Array<>(arr);
for (int i = parent(arr.length - 1); i >= 0; i--) {
siftDown(i);
}
}
public boolean isEmpty() {
return data.isEmpty();
}
public int getSize() {
return data.getSize();
}
private int parent(int index) {
if (index <= 0) {
throw new IllegalArgumentException("index is not illegal");
}
return (index - 1) / 2;
}
private int leftChild(int index) {
return index * 2 + 1;
}
private int rightChild(int index) {
return index * 2 + 2;
}
public void add(E e) {
data.addLast(e);
siftUp(data.getSize() - 1);
}
private void siftUp(int k) {
while (k > 0 && data.get(parent(k)).compareTo(data.get(k)) < 0) {
data.swap(k, parent(k));
k = parent(k);
}
}
public E extractMax() {
E ret = findMax();
data.swap(0, data.getSize() - 1);
data.removeLast();
siftDown(0);
return ret;
}
private void siftDown(int k) {
while (leftChild(k) < data.getSize()) {
int j = leftChild(k);
//找到k孩子节点中最大值的节点 j+1代表右孩子
if (j + 1 < data.getSize() && data.get(j + 1).compareTo(data.get(j)) > 0) {
//说明有右孩子 且右孩子值最大
j ++; //代表j是孩子节点最大值的索引
}
if (data.get(k).compareTo(data.get(j)) >= 0) {
//k索引上的值最大 不需交换 退出循环
break;
}
data.swap(k, j);
k = j;
}
}
public E findMax() {
if (data.isEmpty()) {
throw new IllegalArgumentException("heap is empty");
}
return data.get(0);
}
//替换掉最大值
public E replace(E e) {
E ret = findMax();
data.set(0, e);
siftDown(0);
return ret;
}
}
网友评论