Update the section of heap.

pull/516/head
krahets 2 years ago
parent b9178bc7d6
commit 11c835d79c

@ -36,11 +36,9 @@ class MaxHeap {
/* 交换元素 */ /* 交换元素 */
void _swap(int i, int j) { void _swap(int i, int j) {
int a = _maxHeap[i]; int tmp = _maxHeap[i];
int b = _maxHeap[j]; _maxHeap[i] = _maxHeap[j];;
int tem = a; _maxHeap[j] = tmp;
_maxHeap[i] = b;
_maxHeap[j] = tem;
} }
/* 获取堆大小 */ /* 获取堆大小 */

@ -41,10 +41,8 @@ class MaxHeap {
/* 交换元素 */ /* 交换元素 */
private void swap(int i, int j) { private void swap(int i, int j) {
int a = maxHeap.get(i); int tmp = maxHeap.get(i);
int b = maxHeap.get(j); maxHeap.set(i, maxHeap.get(j));
int tmp = a;
maxHeap.set(i, b);
maxHeap.set(j, tmp); maxHeap.set(j, tmp);
} }

@ -37,10 +37,8 @@ class MaxHeap {
/* 交换元素 */ /* 交换元素 */
#swap(i, j) { #swap(i, j) {
const a = this.#maxHeap[i], const tmp = this.#maxHeap[i];
b = this.#maxHeap[j], this.#maxHeap[i] = this.#maxHeap[j];
tmp = a;
this.#maxHeap[i] = b;
this.#maxHeap[j] = tmp; this.#maxHeap[j] = tmp;
} }

@ -35,8 +35,7 @@ class MaxHeap:
def swap(self, i: int, j: int): def swap(self, i: int, j: int):
"""交换元素""" """交换元素"""
a, b = self.max_heap[i], self.max_heap[j] self.max_heap[i], self.max_heap[j] = self.max_heap[j], self.max_heap[i]
self.max_heap[i], self.max_heap[j] = b, a
def size(self) -> int: def size(self) -> int:
"""获取堆大小""" """获取堆大小"""

@ -36,10 +36,8 @@ class MaxHeap {
/* 交换元素 */ /* 交换元素 */
private swap(i: number, j: number): void { private swap(i: number, j: number): void {
const a = this.maxHeap[i], const tmp = this.maxHeap[i];
b = this.maxHeap[j], this.maxHeap[i] = this.maxHeap[j];
tmp = a;
this.maxHeap[i] = b;
this.maxHeap[j] = tmp; this.maxHeap[j] = tmp;
} }

@ -48,10 +48,8 @@ pub fn MaxHeap(comptime T: type) type {
// //
fn swap(self: *Self, i: usize, j: usize) !void { fn swap(self: *Self, i: usize, j: usize) !void {
var a = self.max_heap.?.items[i]; var tmp = self.max_heap.?.items[i];
var b = self.max_heap.?.items[j]; try self.max_heap.?.replaceRange(i, 1, &[_]T{self.max_heap.?.items[j]});
var tmp = a;
try self.max_heap.?.replaceRange(i, 1, &[_]T{b});
try self.max_heap.?.replaceRange(j, 1, &[_]T{tmp}); try self.max_heap.?.replaceRange(j, 1, &[_]T{tmp});
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 57 KiB

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 65 KiB

After

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 KiB

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 92 KiB

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 86 KiB

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 81 KiB

After

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 83 KiB

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 76 KiB

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 108 KiB

After

Width:  |  Height:  |  Size: 110 KiB

@ -505,6 +505,15 @@
=== "<6>" === "<6>"
![heap_push_step6](heap.assets/heap_push_step6.png) ![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)
设节点总数为 $n$ ,则树的高度为 $O(\log n)$ 。由此可知,堆化操作的循环轮数最多为 $O(\log n)$ **元素入堆操作的时间复杂度为 $O(\log n)$** 。 设节点总数为 $n$ ,则树的高度为 $O(\log n)$ 。由此可知,堆化操作的循环轮数最多为 $O(\log n)$ **元素入堆操作的时间复杂度为 $O(\log n)$** 。
=== "Java" === "Java"
@ -712,5 +721,5 @@
## 堆常见应用 ## 堆常见应用
- **优先队列**:堆通常作为实现优先队列的首选数据结构,其入队和出队操作的时间复杂度均为 $O(\log n)$ ,而建队操作为 $O(n)$ ,这些操作都非常高效。 - **优先队列**:堆通常作为实现优先队列的首选数据结构,其入队和出队操作的时间复杂度均为 $O(\log n)$ ,而建队操作为 $O(n)$ ,这些操作都非常高效。
- **堆排序**:给定一组数据,我们可以用它们建立一个堆,然后不断地执行元素出堆操作,从而得到有序数据。当然,堆排序还有一种更优雅的实现,详见后续的堆排序章节。 - **堆排序**:给定一组数据,我们可以用它们建立一个堆,然后不断地执行元素出堆操作,从而得到有序数据。然而,我们通常会使用一种更优雅的方式实现堆排序,详见后续的堆排序章节。
- **获取最大的 $k$ 个元素**:这是一个经典的算法问题,同时也是一种典型应用,例如选择热度前 10 的新闻作为微博热搜,选取销量前 10 的商品等。 - **获取最大的 $k$ 个元素**:这是一个经典的算法问题,同时也是一种典型应用,例如选择热度前 10 的新闻作为微博热搜,选取销量前 10 的商品等。

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 46 KiB

Loading…
Cancel
Save