-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.rs
144 lines (121 loc) · 3.57 KB
/
heap.rs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#[derive(PartialEq)]
pub enum HeapType {
Min,
Max
}
pub struct Heap<T> {
data: Vec<T>,
heap_type: HeapType
}
impl<T: Ord + Clone + Copy> Heap<T> {
pub fn new(heap_type: HeapType) -> Self {
Self {
data: vec![],
heap_type
}
}
pub fn is_empty(&self) -> bool {
self.data.len() == 0
}
pub fn push(&mut self, item: T) {
let old_len = self.data.len();
self.data.push(item);
self.sift_up(old_len);
}
pub fn pop(&mut self) -> Option<T> {
if self.is_empty() {
return None;
}
let max = self.data[0];
let last = self.data.remove(self.data.len() - 1);
if self.data.len() > 0 {
self.data[0] = last;
self.sift_down(0);
}
Some(max)
}
fn sift_up(&mut self, index: usize) {
if index == 0 {
return;
}
let parent_index = (index - 1) / 2;
let should_swap = match self.heap_type {
HeapType::Max => |a: &T, b: &T| a > b,
HeapType::Min => |a: &T, b: &T| a < b,
};
if should_swap(&self.data[index], &self.data[parent_index]) {
self.data.swap(index, parent_index);
self.sift_up(parent_index);
}
}
fn sift_down(&mut self, index: usize) {
let left_child_index = index * 2 + 1;
let right_child_index = index * 2 + 2;
if !self.is_valid_index(left_child_index) {
return;
}
let comparison = match self.heap_type {
HeapType::Max => |a: &T, b: &T| a > b,
HeapType::Min => |a: &T, b: &T| a < b,
};
let mut child_index = left_child_index;
if self.is_valid_index(right_child_index) && comparison(&self.data[right_child_index], &self.data[left_child_index]) {
child_index = right_child_index;
}
if comparison(&self.data[child_index], &self.data[index]) {
self.data.swap(index, child_index);
self.sift_down(child_index);
}
}
fn is_valid_index(&self, index: usize) -> bool {
index < self.data.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_min_heap() {
let min_heap: Heap<i32> = Heap::new(HeapType::Min);
assert!(min_heap.is_empty());
}
#[test]
fn empty_max_heap() {
let max_heap: Heap<i32> = Heap::new(HeapType::Max);
assert!(max_heap.is_empty());
}
#[test]
fn test_single_node_min_heap() {
let mut min_heap = Heap::new(HeapType::Min);
min_heap.push(10);
assert_eq!(min_heap.pop(), Some(10));
assert!(min_heap.is_empty());
}
#[test]
fn test_single_node_max_heap() {
let mut max_heap = Heap::new(HeapType::Max);
max_heap.push(10);
assert_eq!(max_heap.pop(), Some(10));
assert!(max_heap.is_empty());
}
#[test]
fn test_multiple_nodes_min_heap() {
let mut min_heap = Heap::new(HeapType::Min);
min_heap.push(10);
min_heap.push(5);
min_heap.push(15);
assert_eq!(min_heap.pop(), Some(5));
assert_eq!(min_heap.pop(), Some(10));
assert_eq!(min_heap.pop(), Some(15));
}
#[test]
fn test_multiple_nodes_max_heap() {
let mut max_heap = Heap::new(HeapType::Max);
max_heap.push(10);
max_heap.push(5);
max_heap.push(15);
assert_eq!(max_heap.pop(), Some(15));
assert_eq!(max_heap.pop(), Some(10));
assert_eq!(max_heap.pop(), Some(5));
}
}