forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.rs
More file actions
71 lines (59 loc) · 2.03 KB
/
Copy pathheap.rs
File metadata and controls
71 lines (59 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* File: heap.rs
* Created Time: 2023-07-16
* Author: night-cruise (2586447362@qq.com)
*/
use hello_algo_rust::include::print_util;
use std::{cmp::Reverse, collections::BinaryHeap};
fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) {
heap.push(val); // 元素入堆
println!("\n元素 {} 入堆后", val);
print_util::print_heap(heap.iter().map(|&val| val).collect());
}
fn test_pop_max(heap: &mut BinaryHeap<i32>) {
let val = heap.pop().unwrap();
println!("\n堆顶元素 {} 出堆后", val);
print_util::print_heap(heap.iter().map(|&val| val).collect());
}
/* Driver Code */
fn main() {
/* 初始化堆 */
// 初始化小顶堆
#[allow(unused_assignments)]
let mut min_heap = BinaryHeap::new();
// Rust 的 BinaryHeap 是大顶堆,小顶堆一般会“套上”Reverse
// 初始化大顶堆
let mut max_heap = BinaryHeap::new();
println!("\n以下测试样例为大顶堆");
/* 元素入堆 */
test_push_max(&mut max_heap, 1);
test_push_max(&mut max_heap, 3);
test_push_max(&mut max_heap, 2);
test_push_max(&mut max_heap, 5);
test_push_max(&mut max_heap, 4);
/* 获取堆顶元素 */
let peek = max_heap.peek().unwrap();
println!("\n堆顶元素为 {}", peek);
/* 堆顶元素出堆 */
test_pop_max(&mut max_heap);
test_pop_max(&mut max_heap);
test_pop_max(&mut max_heap);
test_pop_max(&mut max_heap);
test_pop_max(&mut max_heap);
/* 获取堆大小 */
let size = max_heap.len();
println!("\n堆元素数量为 {}", size);
/* 判断堆是否为空 */
let is_empty = max_heap.is_empty();
println!("\n堆是否为空 {}", is_empty);
/* 输入列表并建堆 */
// 时间复杂度为 O(n) ,而非 O(nlogn)
min_heap = BinaryHeap::from(
vec![1, 3, 2, 5, 4]
.into_iter()
.map(|val| Reverse(val))
.collect::<Vec<Reverse<i32>>>(),
);
println!("\n输入列表并建立小顶堆后");
print_util::print_heap(min_heap.iter().map(|&val| val.0).collect());
}