6
6
7
7
use hello_algo_rust:: include:: print_util;
8
8
9
- use std:: collections:: BinaryHeap ;
9
+ use std:: { cmp :: Reverse , collections:: BinaryHeap } ;
10
10
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) ; // 元素入堆
13
13
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 ( ) ) ;
15
20
}
16
21
17
- fn test_pop ( heap : & mut BinaryHeap < i32 > , flag : i32 ) {
22
+ fn test_pop_max ( heap : & mut BinaryHeap < i32 > ) {
18
23
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 ( ) ) ;
21
31
}
22
32
23
33
/* Driver Code */
@@ -26,31 +36,29 @@ fn main() {
26
36
// 初始化小顶堆
27
37
#[ allow( unused_assignments) ]
28
38
let mut min_heap = BinaryHeap :: new ( ) ;
29
- // Rust 的 BinaryHeap 是大顶堆,当入队时将元素值乘以 -1 将其反转,当出队时将元素值乘以 -1 将其还原
30
- let min_heap_flag = -1 ;
39
+ // Rust 的 BinaryHeap 是大顶堆,小顶堆一般会“套上”Reverse
31
40
// 初始化大顶堆
32
41
let mut max_heap = BinaryHeap :: new ( ) ;
33
- let max_heap_flag = 1 ;
34
42
35
43
println ! ( "\n 以下测试样例为大顶堆" ) ;
36
44
37
45
/* 元素入堆 */
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 ) ;
43
51
44
52
/* 获取堆顶元素 */
45
- let peek = max_heap. peek ( ) . unwrap ( ) * max_heap_flag ;
53
+ let peek = max_heap. peek ( ) . unwrap ( ) ;
46
54
println ! ( "\n 堆顶元素为 {}" , peek) ;
47
55
48
56
/* 堆顶元素出堆 */
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) ;
54
62
55
63
/* 获取堆大小 */
56
64
let size = max_heap. len ( ) ;
@@ -65,9 +73,9 @@ fn main() {
65
73
min_heap = BinaryHeap :: from (
66
74
vec ! [ 1 , 3 , 2 , 5 , 4 ]
67
75
. into_iter ( )
68
- . map ( |val| min_heap_flag * val)
69
- . collect :: < Vec < i32 > > ( ) ,
76
+ . map ( |val| Reverse ( val) )
77
+ . collect :: < Vec < Reverse < i32 > > > ( ) ,
70
78
) ;
71
79
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 ( ) ) ;
73
81
}
0 commit comments