Skip to content

Commit 7751ea1

Browse files
authored
update rust heap (#1761)
1 parent 5a4aa8c commit 7751ea1

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

codes/rust/chapter_heap/heap.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@
66

77
use hello_algo_rust::include::print_util;
88

9-
use std::collections::BinaryHeap;
9+
use std::{cmp::Reverse, collections::BinaryHeap};
1010

11-
fn test_push(heap: &mut BinaryHeap<i32>, val: i32, flag: i32) {
12-
heap.push(flag * val); // 元素入堆
11+
fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) {
12+
heap.push(val); // 元素入堆
1313
println!("\n元素 {} 入堆后", val);
14-
print_util::print_heap(heap.iter().map(|&val| flag * val).collect());
14+
print_util::print_heap(heap.iter().map(|&val| val).collect());
15+
}
16+
fn test_push_min(heap: &mut BinaryHeap<Reverse<i32>>, val: i32) {
17+
heap.push(Reverse(val)); // 元素入堆
18+
println!("\n元素 {} 入堆后", val);
19+
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
1520
}
1621

17-
fn test_pop(heap: &mut BinaryHeap<i32>, flag: i32) {
22+
fn test_pop_max(heap: &mut BinaryHeap<i32>) {
1823
let val = heap.pop().unwrap();
19-
println!("\n堆顶元素 {} 出堆后", flag * val);
20-
print_util::print_heap(heap.iter().map(|&val| flag * val).collect());
24+
println!("\n堆顶元素 {} 出堆后", val);
25+
print_util::print_heap(heap.iter().map(|&val| val).collect());
26+
}
27+
fn test_pop_min(heap: &mut BinaryHeap<Reverse<i32>>) {
28+
let val = heap.pop().unwrap().0;
29+
println!("\n堆顶元素 {} 出堆后", val);
30+
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
2131
}
2232

2333
/* Driver Code */
@@ -26,31 +36,29 @@ fn main() {
2636
// 初始化小顶堆
2737
#[allow(unused_assignments)]
2838
let mut min_heap = BinaryHeap::new();
29-
// Rust 的 BinaryHeap 是大顶堆,当入队时将元素值乘以 -1 将其反转,当出队时将元素值乘以 -1 将其还原
30-
let min_heap_flag = -1;
39+
// Rust 的 BinaryHeap 是大顶堆,小顶堆一般会“套上”Reverse
3140
// 初始化大顶堆
3241
let mut max_heap = BinaryHeap::new();
33-
let max_heap_flag = 1;
3442

3543
println!("\n以下测试样例为大顶堆");
3644

3745
/* 元素入堆 */
38-
test_push(&mut max_heap, 1, max_heap_flag);
39-
test_push(&mut max_heap, 3, max_heap_flag);
40-
test_push(&mut max_heap, 2, max_heap_flag);
41-
test_push(&mut max_heap, 5, max_heap_flag);
42-
test_push(&mut max_heap, 4, max_heap_flag);
46+
test_push_max(&mut max_heap, 1);
47+
test_push_max(&mut max_heap, 3);
48+
test_push_max(&mut max_heap, 2);
49+
test_push_max(&mut max_heap, 5);
50+
test_push_max(&mut max_heap, 4);
4351

4452
/* 获取堆顶元素 */
45-
let peek = max_heap.peek().unwrap() * max_heap_flag;
53+
let peek = max_heap.peek().unwrap();
4654
println!("\n堆顶元素为 {}", peek);
4755

4856
/* 堆顶元素出堆 */
49-
test_pop(&mut max_heap, max_heap_flag);
50-
test_pop(&mut max_heap, max_heap_flag);
51-
test_pop(&mut max_heap, max_heap_flag);
52-
test_pop(&mut max_heap, max_heap_flag);
53-
test_pop(&mut max_heap, max_heap_flag);
57+
test_pop_max(&mut max_heap);
58+
test_pop_max(&mut max_heap);
59+
test_pop_max(&mut max_heap);
60+
test_pop_max(&mut max_heap);
61+
test_pop_max(&mut max_heap);
5462

5563
/* 获取堆大小 */
5664
let size = max_heap.len();
@@ -65,9 +73,9 @@ fn main() {
6573
min_heap = BinaryHeap::from(
6674
vec![1, 3, 2, 5, 4]
6775
.into_iter()
68-
.map(|val| min_heap_flag * val)
69-
.collect::<Vec<i32>>(),
76+
.map(|val| Reverse(val))
77+
.collect::<Vec<Reverse<i32>>>(),
7078
);
7179
println!("\n输入列表并建立小顶堆后");
72-
print_util::print_heap(min_heap.iter().map(|&val| min_heap_flag * val).collect());
80+
print_util::print_heap(min_heap.iter().map(|&val| val.0).collect());
7381
}

0 commit comments

Comments
 (0)