Implementation of Min Heap
// empty Min Heap for practice:
public class MinHeap {
private int[] array;
private int size;
private Comparator<E> comparator;
public MinHeap(int[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("input array can not be null or empty");
}
this.array = Arrays.copyOf(array, array.length);
size = array.length;
this.comparator = comparator;
heapify();
}
private void heapify() {
// the range of heapify[0, size / 2 - 1]
for (int i = size / 2 - 1; i >= 0; i--) {
percolateDown(i);
}
public MinHeap(int cap) {
if (cap <= 0) {
throw new IllegalArgumentException("capacity can not be <= 0");
}
array = new int[cap];
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == array.length;
}
private void percolateUp(int index) {
while (index > 0) {
int parentIndex = (index - 1) / 2;
if (array[parentIndex] > array[index]) {
swap(parentIndex, index);
} else {
break;
}
**index = parentIndex;**
}
private void percolateDown(int index) {
// leafIndex > size / 2 - 1
while (index <= size / 2 - 1) {
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
// 选出左孩子和右孩子中更小的那个进行swap
int swapCandidate = leftChildIndex;
// 只有当右孩子存在且它的值更小的情况下,我们才将右孩子作为swapCandidate
if (rightChildInde <= size - 1 &&
array[rightChildIndex] < array[leftChildIndex]) {
swapCandidate = rightChildIndex;
}
if (array[index] > array[swapCandidate]) {
swap(index, swapCandidate);
} else {
break;
}
**index = swapCandidate;
}**
}
public int peek() {
if (size == 0) {
throw new NoSuchElementException("heap is empty");
}
return array[0];
}
public int poll() {
if (size == 0) {
throw new NoSuchElementException("heap is empty");
}
int result = array[0];
array[0] = array[size - 1];
size--;
percolateDown(0);
// size--如果写到这里就是错的,percolateDown里面会用到size,所以size值要及时更新
return result;
}
public void offer(int ele) {
if (size == array.length) {
array = Arrays.copyOf(array, (int)(array.length * 1.5));
}
array[size] = ele;
size++;
}
// update shoule return the original value
public int update(int index, int ele) {
// check the index is valid
if (index < 0 || index >= size - 1) {
throw new ArrayIndexOutOfBoundsException("invalid index range");
}
int result = array[index];
array[index] = ele;
if (result > ele) {
percolateUp(index);
} else {
percolateDown(index);
}
return result;
}
private void swap(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public class MinHeap {
private int[] array;
private int size;
public MinHeap(int[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("input array can not be null or empty");
}
this.array = array;
size = array.length;
heapify();
}
private void heapify() {
}
public MinHeap(int cap) {
if (cap <= 0) {
throw new IllegalArgumentException("capacity can not be <= 0");
}
array = new int[cap];
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == array.length;
}
private void percolateUp(int index) {
}
private void percolateDown(int index) {
}
public int peek() {
}
public int poll() {
}
public void offer(int ele) {
}
public int update(int index, int ele) {
}
private void swap(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}