1.概念

  • 特殊的完全二叉树
  • 所有节点都大于等于(最大堆)或者小于等于(最小堆)它的子节点

image.pngimage.png

  • js 中通常用数组表示堆
    • 左侧节点的位置是 2*index + 1
    • 右侧节点的位置是 2*index + 2
    • 父节点位置是 (index - 1) / 2

image.png

2.应用

  • 堆能快速高效的找出最大值和最小值,时间复杂度 O(1)
  • 找出第 k 个最大(小)元素

第 k 个最大元素

  • 构建一个最小堆,并将元素依次插入堆中
  • 当堆的容量超过 k,就删除堆顶
  • 插入结束后,堆顶就是第 k 个最大元素

js 实现最小堆类

  • 在类里声明一个数组,用来装元素
  • 主要方法:插入,删除堆顶、获取堆顶、获取堆大小
  • 插入
    • 将值插入堆的底部,即数组尾部
    • 然后上移:将这个值和它的父节点交换,直到父节点小于等于这个插入的值
    • 大小为 k 的堆中插入元素的时间复杂度为 O(log k)
  • 删除堆顶
    • 用数组尾部元素替换堆顶(直接删除堆顶会破坏堆结构)
    • 然后下移:将新堆顶和它的子节点进行交换,直到子节点大于等于这个新堆项
    • 大小为 k 的堆中插入元素的时间复杂度为 O(log k)
  • 获取堆顶
    • 返回数组的头部
  • 获取堆的大小
    • 直接返回数组的长度
  1. class MinHeap {
  2. constructor() {
  3. this.heap = [];
  4. }
  5. swap(i1, i2) {
  6. const temp = this.heap[i1];
  7. this.heap[i1] = this.heap[i2];
  8. this.heap[i2] = temp;
  9. }
  10. getParentIndex(i) {
  11. return (i - 1) >> 1;
  12. }
  13. getLeftIndex(i) {
  14. return i * 2 + 1;
  15. }
  16. getRightIndex(i) {
  17. return i * 2 + 2;
  18. }
  19. shiftUp(index) {
  20. if (index == 0) { return; }
  21. const parentIndex = this.getParentIndex(index);
  22. if (this.heap[parentIndex] > this.heap[index]) {
  23. this.swap(parentIndex, index);
  24. this.shiftUp(parentIndex);
  25. }
  26. }
  27. shiftDown(index) {
  28. const leftIndex = this.getLeftIndex(index);
  29. const rightIndex = this.getRightIndex(index);
  30. if (this.heap[leftIndex] < this.heap[index]) {
  31. this.swap(leftIndex, index);
  32. this.shiftDown(leftIndex);
  33. }
  34. if (this.heap[rightIndex] < this.heap[index]) {
  35. this.swap(rightIndex, index);
  36. this.shiftDown(rightIndex);
  37. }
  38. }
  39. insert(value) {
  40. this.heap.push(value);
  41. this.shiftUp(this.heap.length - 1);
  42. }
  43. pop() {
  44. this.heap[0] = this.heap.pop();
  45. this.shiftDown(0);
  46. }
  47. peek() {
  48. return this.heap[0];
  49. }
  50. size() {
  51. return this.heap.length;
  52. }
  53. }
  54. const h = new MinHeap();
  55. h.insert(3);
  56. h.insert(2);
  57. h.insert(1);
  58. h.pop();