*** 二叉堆本质上是一颗完全二叉树(除最后一层外,各层的节点数达到最大,最后一层的节点都在左边) ***
- 最大堆:父节点的值大于孩子节点
- 最小堆:父节点的值小于孩子节点
以最大堆为列:
1.插入
插入到二叉堆的最后一个位置,再向上调整
向上调整:与父节点比较,大于父则节点交换,直到小于父节点
时间复杂度 o(log n)
2.删除节点
只能删除堆顶节点,将堆的最后一个节点放到顶点,设当前节点为顶点,再向下调整
向下调整:比较左右节点,选出大的节点与当前节点比较,若大的节点大于当前节点,则交换位置,继续向下调整
时间复杂度 o(log n)
3.构建二叉堆
即将一个无序的二叉树调整为二叉堆,本质是让所有非叶子节点下沉。从最后一个非叶子节点开始(保证下沉时子树为二叉堆),直到顶点。
构建堆的时间复杂度为O(n)
假如有N个节点,那么高度为H=logN ,最后一层每个父节点最多只需要下调1次,倒数第二层最多只需要下调2次,顶点最多需要下调H次,而最后一层父节点共有2(H-1)个,倒数第二层公有2(H-2),顶点只有1(2^0)个,所以总共的时间复杂度为s = 1 * 2^(H-1) + 2 * 2^(H-2) + … + (H-1) * 2^1 + H * 2^0
2s=2^H+.2 * 2^(H-1)…+H2
s=2s-s=2^H+1 * 2(H-1).+1*2(H-2)…+1 * 2-H2^0=2 * 2^H - 2 - H*2
将H代入后s= 2N - 2 - log2(N),近似的时间复杂度就是O(N)。
链接:https://www.zhihu.com/question/20729324/answer/231955716
4. 实现
堆使用数组进行。
根节点下标为0
下标 n的节点的孩子节点为2 * n+1和2* (n+1)
Java优先队列实现的注释 children of queue[n] are queue[2*n+1] and queue[2*(n+1)]
则 下标为n 的节点的父节点下标一定是(n-1)/2
第一个非叶子节点的数组下标是(queue.length-2)/2
5.优先队列
java 中没有实现堆,只有优先队列(默认是最小优先队列,通过Comparator改变)
优先队列即是堆一种应用。
-
最大优先队列,每次出队的元素必定是最大的元素
-
最小优先队列,每次出队的元素必定是最小的元素
java 优先队列
//初始容量
private static final int DEFAULT_INITIAL_CAPACITY = 11;
//存储结构
transient Object[] queue; // non-private to simplify nested class access
//当前实际元素个数
private int size = 0;
//需要执行的比较操作的实现
private final Comparator<? super E> comparator;
//修改次数,fail-first 机制
transient int modCount = 0
//队列大小上限
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE -8;
//java优先队列构造函数
public PriorityQueue(int initialCapacity,
Comparator<? super E> comparator) {
// Note: This restriction of at least one is not actually needed,
// but continues for 1.5 compatibility
if (initialCapacity < 1)
throw new IllegalArgumentException();
this.queue = new Object[initialCapacity];
this.comparator = comparator;
}
添加 元素
public boolean add(E e) {
return offer(e);
}
public boolean offer(E e) {
if (e == null)
throw new NullPointerException();
modCount++;
//插入位置是堆的最后
int i = size;
if (i >= queue.length)
//数组扩容
grow(i + 1);
size = i + 1;
if (i == 0)
queue[0] = e;
else
//插入
siftUp(i, e);
return true;
}
private void siftUp(int k, E x) {
if (comparator != null)
siftUpUsingComparator(k, x);
else
//没有传入comparator根据自然排序向上调整
siftUpComparable(k, x);
}
//向上调整的实现代码
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
//下标为n 的节点的父节点下标一定是(n-1)/2
int parent = (k - 1) >>> 1;
Object e = queue[parent];
//插入值大于父节点的值则循环结束
if (comparator.compare(x, (E) e) >= 0)
break;
queue[k] = e;
k = parent;
}
//这里循环中没做交换,只是将父节点的值赋值给子节点,最后再将插入值赋给最终下标的节点
queue[k] = x;
}
删除元素
public E poll() {
if (size == 0)
return null;
int s = --size;
modCount++;
E result = (E) queue[0];
E x = (E) queue[s];
queue[s] = null;
if (s != 0)
//向下调整
siftDown(0, x);
return result;
}
private void siftDown(int k, E x) {
if (comparator != null)
siftDownUsingComparator(k, x);
else
siftDownComparable(k, x);
}
private void siftDownUsingComparator(int k, E x) {
int half = size >>> 1;
while (k < half) {
//下标 n的节点的孩子节点为2 * n+1和2* (n+1)
int child = (k << 1) + 1;
Object c = queue[child];
int right = child + 1;
//得到左右孩子的最小值
if (right < size &&
comparator.compare((E) c, (E) queue[right]) > 0)
c = queue[child = right];
//将最大值大于当前节点的值退出循环
if (comparator.compare(x, (E) c) <= 0)
break;
queue[k] = c;
k = child;
}
queue[k] = x;
}