数据结构-堆

堆结构含义

堆是一种特殊的完全二叉树,也叫二叉堆,有两种形态,大顶堆和小顶堆

  • 大顶堆,每一个节点的值都必须大于等于其子树中每个节点

  • 小顶堆,每一个节点的值都必须小于等于其子树中每个节点

如图,是一个小顶堆,可表示为 [3, 4, 6, 5, 8, 9, 10, 7]

完全二叉树由于其结构适合用数组来存储,可以利用数组高效的随机读写,来找父子节点,比用指针更加节约空间

堆化操作

一个基于数组的二叉堆,父子节点下标之间满足一定的关系

  • 父节点下标为 index,左孩子为 2*index+1,右孩子为 2*index+2

  • 子节点下标为 index,父节点下标为 (index -1)/2

堆操作有两种,入堆和出堆。入堆时,会将元素追加数组后方,堆 size+1;出堆时会将数组第一个元素和最后一个有效元素交换,堆 size-1。接下来,会执行堆化操作,即节点从堆顶自上而下或从堆尾至下而上通过比较交换,找到合适位置的过程

通常将自上而下的堆化叫 heapify,自下而上的堆化叫 heapInsert

小顶堆示例代码:

java 复制代码
public class MaxTopHeap {

    public final int[] arr;
    public int heapSize;

    public MaxTopHeap(int[] arr, int heapSize) {
        this.arr = arr;
        this.heapSize = heapSize;
    }

    /**
     * 加入新数
     * 返回最后确定的 index
     */
    public int add(int val) {
        // 加入新数
        arr[heapSize] = val;
        heapSize++;
        return heapInsert(arr, heapSize - 1);
    }

    /**
     * 删除并返回堆顶的数
     */
    public int removeTop() {
        int top = arr[0];
        swap(arr, 0, heapSize - 1);
        heapSize--;
        heapify(arr, 0, heapSize);
        return top;
    }

    /**
     * 当一个数在 index 上,尝试向上调整
     * 返回最后确定的 index
     */
    public static int heapInsert(int[] arr, int index) {
        // 计算
        int pIndex = (index - 1) / 2;
        while (arr[index] > arr[pIndex]) {
            swap(arr, index, pIndex);
            index = pIndex;
            pIndex = (pIndex - 1) / 2;
        }
        return pIndex;
    }

    /**
     * 当一个数在 index 上,尝试向下调整
     * 返回最后确定的 index
     */
    public static int heapify(int[] arr, int index, int heapSize) {
        // 左孩子
        int left = 2 * index + 1;
        while (left < heapSize) {
            int maxChildIndex = left;
            // 比较左右孩子
            if (left + 1 < heapSize) {
                maxChildIndex = arr[left + 1] > arr[left] ? left + 1 : left;
            }
            // 比较父和大孩子
            int largestIndex = arr[maxChildIndex] > arr[index] ? maxChildIndex : index;
            if (largestIndex == index) {
                break;
            }
            swap(arr, index, largestIndex);
            index = largestIndex;
            left = 2 * index + 1;
        }
        return index;
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

常见用堆的场景

堆常用来解决 TopK 问题,即在一些数据中,取出前 K 个最大(小)值

基于堆的特性,可用来排序,将要排的数据入堆再依次弹出堆顶,最终得到排好序的数组

堆排序的时间复杂度是 <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n l o g n ) O(nlogn) </math>O(nlogn)​,空间复杂度是 O(1),是不稳定排序算法

java 复制代码
// 堆排序
public class HeapSort {

    public static void sort(int[] arr) {
        MaxTopHeap heap = new MaxTopHeap(arr, 0);
        for (int j : arr) {
            heap.add(j);
        }
        for (int i = 0; i < arr.length; i++) {
            heap.removeTop();
        }
    }
}

Java 中堆的实现是 PriorityQueue,优先队列,可以构造出大顶堆和小顶堆

java 复制代码
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
相关推荐
荒诞硬汉27 分钟前
数组常见算法
java·数据结构·算法
2301_800256111 小时前
B+树:数据库的基石 R树:空间数据的索引专家 四叉树:空间划分的网格大师
数据结构·数据库·b树·机器学习·postgresql·r-tree
码农幻想梦1 小时前
第九章 高级数据结构
数据结构
AlenTech1 小时前
206. 反转链表 - 力扣(LeetCode)
数据结构·leetcode·链表
大厂技术总监下海2 小时前
用户行为分析怎么做?ClickHouse + 嵌套数据结构,轻松处理复杂事件
大数据·数据结构·数据库
AI科技星2 小时前
光速飞行器动力学方程的第一性原理推导、验证与范式革命
数据结构·人工智能·线性代数·算法·机器学习·概率论
余瑜鱼鱼鱼2 小时前
Java数据结构:从入门到精通(十)
数据结构
好奇龙猫2 小时前
【大学院-筆記試験練習:线性代数和数据结构(5)】
数据结构·线性代数
爱吃生蚝的于勒3 小时前
【Linux】进程间通信之匿名管道
linux·运维·服务器·c语言·数据结构·c++·vim