什么是堆?
堆是一种特殊的完全二叉树。完全二叉树的每层节点都完全填满,在最后一层上如果没有满,则只缺少右边的若干节点。
- 最大堆:所有的节点都大于等于它的子节点
- 最小堆:所有的节点都小于等于它的子节点
JS中的堆
JS中通常使用数组表示堆
- 左侧子节点的位置是2*index + 1
- 右侧子节点的位置是2*index + 2
- 父节点位置是 (index - 1)/2
实现最小堆类: 在类中,声明一个数组,用来装元素。主要方法:插入、删除堆顶、获取堆顶、获取堆大小
插入元素:
- 将值插入堆的底部,即数组的尾部
- 然后上移:将这个值和它的父节点进行交换,直到父节点小于等于这个插入的值
- 大小为k的堆中插入元素的时间复杂度为O(logk)
删除堆顶:
- 用数组尾部元素替换堆顶(直接删除堆顶会破坏堆结构)
- 然后下移:将新堆顶和它的子节点进行交换,直到子节点大于等于这个新堆顶
- 大小为K的堆中删除堆顶的时间复杂度为O(logk)
获取堆顶:
- 返回数组的头部
获取堆的大小:
- 返回数组的长度
class MinHeap{
constructor(){
this.heap = [];
}
getParentIndex(index){
return Math.floor((index - 1) / 2); //(index -1) >> 1
}
swap(i1, i2){
let temp = this.heap[i1];
this.heap[i1] = this.heap[i2];
this.heap[i2] = temp;
}
shiftUp(index){
if(index == 0){
return;
}
const parentIndex = this.getParentIndex(index);
if (this.heap[parentIndex] > this.heap[index]) {
this.swap(parentIndex, index);
this.shiftUp(parentIndex);
}
}
insert(value){
this.heap.push(value);
this.shiftUp(this.heap.length - 1);
}
getLeftIndex(index){
return 2 * index + 1;
}
getRightIndex(index){
return 2 * index + 2;
}
shiftDown(index){
let leftIndex = this.getLeftIndex(index);
let rightIndex = this.getRightIndex(index);
if (this.heap[leftIndex] < this.heap[index]){
this.swap(leftIndex, index);
this.shiftDown(leftIndex);
}
if (this.heap[rightIndex] < this.heap[index]) {
this.swap(rightIndex, index);
this.shiftDown(rightIndex);
}
}
shift(){
this.heap[0] = this.heap.pop();
this.shiftDown(0);
}
peek(){
return this.heap[0];
}
size(){
return this.heap.length;
}
}
let minHeap = new MinHeap();
minHeap.insert(3);
minHeap.insert(2);
minHeap.insert(1);
minHeap.shift();
堆的应用
堆能高效、快速的找出最大值和最小值,时间复杂度O(1)
找出第K个最大(小)元素
- 1、构建一个最小堆,并将元素依次插入堆中
- 2、当堆的容量超过K,就删除堆顶
- 3、插入结束后,堆顶就是第K个最大元素