使用数组实现
- 添加元素: 数组末尾append 从堆底向上调整 如果发现父节点比当前节点小
就交换位置 - 移除元素: 数组末尾调整到数组头部(堆顶) 从堆顶向下调整 如果发现子节点比当前节点小交换位置
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
interface IAdjust<T> {
boolean needAdjust(T parent, T child);
}
public class MyHeap<T> {
private List<T> array = new ArrayList<>();
// 判断右边元素是否比左边元素更具有堆顶的优先级
private IAdjust<T> adjustFunc;
public MyHeap(IAdjust<T> adjustFunc) {
this.adjustFunc = adjustFunc;
}
public void add(T ele) {
array.add(ele);
if (array.size() == 1) {
return;
}
adjustFromTail();
}
public T pop() {
if (array.size() == 1) {
return array.remove(0);
}
T ele = array.get(array.size() - 1);
array.remove(array.size() - 1);
ele = array.set(0, ele);
adjustFromHeap();
return ele;
}
private void adjustFromHeap() {
int cursor = 0;
while (cursor < array.size()) {
int leftIndex = getLeftChild(cursor);
int rightIndex = getRightChild(cursor);
if (leftIndex >= array.size()) {
return;
}
int swapIndex = leftIndex;
if (rightIndex < array.size() && adjustFunc.needAdjust(array.get(leftIndex), array.get(rightIndex))) {
swapIndex = rightIndex;
}
if (adjustFunc.needAdjust(array.get(cursor), array.get(swapIndex))) {
swap(cursor, swapIndex);
cursor = swapIndex;
} else {
break;
}
}
}
private void adjustFromTail() {
int cursor = array.size() - 1;
while (cursor > 0) {
int parentIndex = getParent(cursor);
T parent = array.get(parentIndex);
if (adjustFunc.needAdjust(parent, array.get(cursor))) {
swap(cursor, parentIndex);
cursor = parentIndex;
} else {
break;
}
}
}
private void swap(int i, int j) {
Collections.swap(array, i, j);
}
private int getParent(int index) {
return (index - 1) >> 1;
}
private int getLeftChild(int index) {
return (index << 1) + 1;
}
private int getRightChild(int index) {
return (index + 1) << 1;
}
public String serializeThis() {
StringBuilder stringBuilder = new StringBuilder();
List<Integer> currNodes = Arrays.asList(0);
stringBuilder.append(array.get(0).toString());
stringBuilder.append("\n");
while (true) {
currNodes = getChildNodes(currNodes);
if (currNodes.size() == 0) {
break;
}
currNodes.forEach(node -> {
stringBuilder.append(array.get(node).toString());
stringBuilder.append(" ");
});
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
private List<Integer> getChildNodes(List<Integer> curr) {
List<Integer> result = new ArrayList<>();
for (Integer node: curr) {
int left = getLeftChild(node);
int right = getRightChild(node);
if (left < array.size()) {
result.add(left);
}
if (right < array.size()) {
result.add(right);
}
}
return result;
}
public static void main(String[] args) {
IAdjust<Integer> iAdjust = (left, right) -> right < left;
MyHeap<Integer> myHeap = new MyHeap<>(iAdjust);
myHeap.add(1);
myHeap.add(2);
myHeap.add(3);
System.out.println(myHeap.serializeThis());
myHeap.add(-1);
myHeap.add(100);
myHeap.add(-100);
System.out.println(myHeap.array.toString());
System.out.println(myHeap.serializeThis());
System.out.println(myHeap.pop());
System.out.println(myHeap.serializeThis());
}
}
输出
1
2 3
[-100, 1, -1, 2, 100, 3]
-100
1 -1
2 100 3
-100
-1
1 3
2 100