--- comments: true --- # 8.1 堆 「堆 heap」是一种满足特定条件的完全二叉树,主要可分为图 8-1 所示的两种类型。 - 「大顶堆 max heap」:任意节点的值 $\geq$ 其子节点的值。 - 「小顶堆 min heap」:任意节点的值 $\leq$ 其子节点的值。 ![小顶堆与大顶堆](heap.assets/min_heap_and_max_heap.png)
图 8-1 小顶堆与大顶堆
堆作为完全二叉树的一个特例,具有以下特性。 - 最底层节点靠左填充,其他层的节点都被填满。 - 我们将二叉树的根节点称为“堆顶”,将底层最靠右的节点称为“堆底”。 - 对于大顶堆(小顶堆),堆顶元素(即根节点)的值分别是最大(最小)的。 ## 8.1.1 堆常用操作 需要指出的是,许多编程语言提供的是「优先队列 priority queue」,这是一种抽象数据结构,定义为具有优先级排序的队列。 实际上,**堆通常用作实现优先队列,大顶堆相当于元素按从大到小顺序出队的优先队列**。从使用角度来看,我们可以将“优先队列”和“堆”看作等价的数据结构。因此,本书对两者不做特别区分,统一使用“堆“来命名。 堆的常用操作见表 8-1 ,方法名需要根据编程语言来确定。表 8-1 堆的操作效率
图 8-2 堆的表示与存储
我们可以将索引映射公式封装成函数,方便后续使用。 === "Python" ```python title="my_heap.py" [class]{MaxHeap}-[func]{left} [class]{MaxHeap}-[func]{right} [class]{MaxHeap}-[func]{parent} ``` === "C++" ```cpp title="my_heap.cpp" [class]{MaxHeap}-[func]{left} [class]{MaxHeap}-[func]{right} [class]{MaxHeap}-[func]{parent} ``` === "Java" ```java title="my_heap.java" [class]{MaxHeap}-[func]{left} [class]{MaxHeap}-[func]{right} [class]{MaxHeap}-[func]{parent} ``` === "C#" ```csharp title="my_heap.cs" [class]{MaxHeap}-[func]{left} [class]{MaxHeap}-[func]{right} [class]{MaxHeap}-[func]{parent} ``` === "Go" ```go title="my_heap.go" [class]{maxHeap}-[func]{left} [class]{maxHeap}-[func]{right} [class]{maxHeap}-[func]{parent} ``` === "Swift" ```swift title="my_heap.swift" [class]{MaxHeap}-[func]{left} [class]{MaxHeap}-[func]{right} [class]{MaxHeap}-[func]{parent} ``` === "JS" ```javascript title="my_heap.js" [class]{MaxHeap}-[func]{#left} [class]{MaxHeap}-[func]{#right} [class]{MaxHeap}-[func]{#parent} ``` === "TS" ```typescript title="my_heap.ts" [class]{MaxHeap}-[func]{left} [class]{MaxHeap}-[func]{right} [class]{MaxHeap}-[func]{parent} ``` === "Dart" ```dart title="my_heap.dart" [class]{MaxHeap}-[func]{_left} [class]{MaxHeap}-[func]{_right} [class]{MaxHeap}-[func]{_parent} ``` === "Rust" ```rust title="my_heap.rs" [class]{MaxHeap}-[func]{left} [class]{MaxHeap}-[func]{right} [class]{MaxHeap}-[func]{parent} ``` === "C" ```c title="my_heap.c" [class]{maxHeap}-[func]{left} [class]{maxHeap}-[func]{right} [class]{maxHeap}-[func]{parent} ``` === "Zig" ```zig title="my_heap.zig" [class]{MaxHeap}-[func]{left} [class]{MaxHeap}-[func]{right} [class]{MaxHeap}-[func]{parent} ``` ### 2. 访问堆顶元素 堆顶元素即为二叉树的根节点,也就是列表的首个元素。 === "Python" ```python title="my_heap.py" [class]{MaxHeap}-[func]{peek} ``` === "C++" ```cpp title="my_heap.cpp" [class]{MaxHeap}-[func]{peek} ``` === "Java" ```java title="my_heap.java" [class]{MaxHeap}-[func]{peek} ``` === "C#" ```csharp title="my_heap.cs" [class]{MaxHeap}-[func]{peek} ``` === "Go" ```go title="my_heap.go" [class]{maxHeap}-[func]{peek} ``` === "Swift" ```swift title="my_heap.swift" [class]{MaxHeap}-[func]{peek} ``` === "JS" ```javascript title="my_heap.js" [class]{MaxHeap}-[func]{peek} ``` === "TS" ```typescript title="my_heap.ts" [class]{MaxHeap}-[func]{peek} ``` === "Dart" ```dart title="my_heap.dart" [class]{MaxHeap}-[func]{peek} ``` === "Rust" ```rust title="my_heap.rs" [class]{MaxHeap}-[func]{peek} ``` === "C" ```c title="my_heap.c" [class]{maxHeap}-[func]{peek} ``` === "Zig" ```zig title="my_heap.zig" [class]{MaxHeap}-[func]{peek} ``` ### 3. 元素入堆 给定元素 `val` ,我们首先将其添加到堆底。添加之后,由于 val 可能大于堆中其他元素,堆的成立条件可能已被破坏。因此,**需要修复从插入节点到根节点的路径上的各个节点**,这个操作被称为「堆化 heapify」。 考虑从入堆节点开始,**从底至顶执行堆化**。如图 8-3 所示,我们比较插入节点与其父节点的值,如果插入节点更大,则将它们交换。然后继续执行此操作,从底至顶修复堆中的各个节点,直至越过根节点或遇到无须交换的节点时结束。 === "<1>" ![元素入堆步骤](heap.assets/heap_push_step1.png) === "<2>" ![heap_push_step2](heap.assets/heap_push_step2.png) === "<3>" ![heap_push_step3](heap.assets/heap_push_step3.png) === "<4>" ![heap_push_step4](heap.assets/heap_push_step4.png) === "<5>" ![heap_push_step5](heap.assets/heap_push_step5.png) === "<6>" ![heap_push_step6](heap.assets/heap_push_step6.png) === "<7>" ![heap_push_step7](heap.assets/heap_push_step7.png) === "<8>" ![heap_push_step8](heap.assets/heap_push_step8.png) === "<9>" ![heap_push_step9](heap.assets/heap_push_step9.png)图 8-3 元素入堆步骤
设节点总数为 $n$ ,则树的高度为 $O(\log n)$ 。由此可知,堆化操作的循环轮数最多为 $O(\log n)$ ,**元素入堆操作的时间复杂度为 $O(\log n)$** 。 === "Python" ```python title="my_heap.py" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{sift_up} ``` === "C++" ```cpp title="my_heap.cpp" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{siftUp} ``` === "Java" ```java title="my_heap.java" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{siftUp} ``` === "C#" ```csharp title="my_heap.cs" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{siftUp} ``` === "Go" ```go title="my_heap.go" [class]{maxHeap}-[func]{push} [class]{maxHeap}-[func]{siftUp} ``` === "Swift" ```swift title="my_heap.swift" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{siftUp} ``` === "JS" ```javascript title="my_heap.js" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{#siftUp} ``` === "TS" ```typescript title="my_heap.ts" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{siftUp} ``` === "Dart" ```dart title="my_heap.dart" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{siftUp} ``` === "Rust" ```rust title="my_heap.rs" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{sift_up} ``` === "C" ```c title="my_heap.c" [class]{maxHeap}-[func]{push} [class]{maxHeap}-[func]{siftUp} ``` === "Zig" ```zig title="my_heap.zig" [class]{MaxHeap}-[func]{push} [class]{MaxHeap}-[func]{siftUp} ``` ### 4. 堆顶元素出堆 堆顶元素是二叉树的根节点,即列表首元素。如果我们直接从列表中删除首元素,那么二叉树中所有节点的索引都会发生变化,这将使得后续使用堆化修复变得困难。为了尽量减少元素索引的变动,我们采用以下操作步骤。 1. 交换堆顶元素与堆底元素(即交换根节点与最右叶节点)。 2. 交换完成后,将堆底从列表中删除(注意,由于已经交换,实际上删除的是原来的堆顶元素)。 3. 从根节点开始,**从顶至底执行堆化**。 如图 8-4 所示,**“从顶至底堆化”的操作方向与“从底至顶堆化”相反**,我们将根节点的值与其两个子节点的值进行比较,将最大的子节点与根节点交换。然后循环执行此操作,直到越过叶节点或遇到无须交换的节点时结束。 === "<1>" ![堆顶元素出堆步骤](heap.assets/heap_pop_step1.png) === "<2>" ![heap_pop_step2](heap.assets/heap_pop_step2.png) === "<3>" ![heap_pop_step3](heap.assets/heap_pop_step3.png) === "<4>" ![heap_pop_step4](heap.assets/heap_pop_step4.png) === "<5>" ![heap_pop_step5](heap.assets/heap_pop_step5.png) === "<6>" ![heap_pop_step6](heap.assets/heap_pop_step6.png) === "<7>" ![heap_pop_step7](heap.assets/heap_pop_step7.png) === "<8>" ![heap_pop_step8](heap.assets/heap_pop_step8.png) === "<9>" ![heap_pop_step9](heap.assets/heap_pop_step9.png) === "<10>" ![heap_pop_step10](heap.assets/heap_pop_step10.png)图 8-4 堆顶元素出堆步骤
与元素入堆操作相似,堆顶元素出堆操作的时间复杂度也为 $O(\log n)$ 。 === "Python" ```python title="my_heap.py" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{sift_down} ``` === "C++" ```cpp title="my_heap.cpp" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{siftDown} ``` === "Java" ```java title="my_heap.java" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{siftDown} ``` === "C#" ```csharp title="my_heap.cs" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{siftDown} ``` === "Go" ```go title="my_heap.go" [class]{maxHeap}-[func]{pop} [class]{maxHeap}-[func]{siftDown} ``` === "Swift" ```swift title="my_heap.swift" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{siftDown} ``` === "JS" ```javascript title="my_heap.js" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{#siftDown} ``` === "TS" ```typescript title="my_heap.ts" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{siftDown} ``` === "Dart" ```dart title="my_heap.dart" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{siftDown} ``` === "Rust" ```rust title="my_heap.rs" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{sift_down} ``` === "C" ```c title="my_heap.c" [class]{maxHeap}-[func]{pop} [class]{maxHeap}-[func]{siftDown} ``` === "Zig" ```zig title="my_heap.zig" [class]{MaxHeap}-[func]{pop} [class]{MaxHeap}-[func]{siftDown} ``` ## 8.1.3 堆常见应用 - **优先队列**:堆通常作为实现优先队列的首选数据结构,其入队和出队操作的时间复杂度均为 $O(\log n)$ ,而建队操作为 $O(n)$ ,这些操作都非常高效。 - **堆排序**:给定一组数据,我们可以用它们建立一个堆,然后不断地执行元素出堆操作,从而得到有序数据。然而,我们通常会使用一种更优雅的方式实现堆排序,详见后续的堆排序章节。 - **获取最大的 $k$ 个元素**:这是一个经典的算法问题,同时也是一种典型应用,例如选择热度前 10 的新闻作为微博热搜,选取销量前 10 的商品等。