-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathheap.rs
333 lines (303 loc) · 9.38 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//! A generic heap data structure.
//!
//! This module provides a `Heap` implementation that can function as either a
//! min-heap or a max-heap. It supports common heap operations such as adding,
//! removing, and iterating over elements. The heap can also be created from
//! an unsorted vector and supports custom comparators for flexible sorting
//! behavior.
use std::{cmp::Ord, slice::Iter};
/// A heap data structure that can be used as a min-heap, max-heap or with
/// custom comparators.
///
/// This struct manages a collection of items where the heap property is maintained.
/// The heap can be configured to order elements based on a provided comparator function,
/// allowing for both min-heap and max-heap functionalities, as well as custom sorting orders.
pub struct Heap<T> {
items: Vec<T>,
comparator: fn(&T, &T) -> bool,
}
impl<T> Heap<T> {
/// Creates a new, empty heap with a custom comparator function.
///
/// # Parameters
/// - `comparator`: A function that defines the heap's ordering.
///
/// # Returns
/// A new `Heap` instance.
pub fn new(comparator: fn(&T, &T) -> bool) -> Self {
Self {
items: vec![],
comparator,
}
}
/// Creates a heap from a vector and a custom comparator function.
///
/// # Parameters
/// - `items`: A vector of items to be turned into a heap.
/// - `comparator`: A function that defines the heap's ordering.
///
/// # Returns
/// A `Heap` instance with the elements from the provided vector.
pub fn from_vec(items: Vec<T>, comparator: fn(&T, &T) -> bool) -> Self {
let mut heap = Self { items, comparator };
heap.build_heap();
heap
}
/// Constructs the heap from an unsorted vector by applying the heapify process.
fn build_heap(&mut self) {
let last_parent_idx = (self.len() / 2).wrapping_sub(1);
for idx in (0..=last_parent_idx).rev() {
self.heapify_down(idx);
}
}
/// Returns the number of elements in the heap.
///
/// # Returns
/// The number of elements in the heap.
pub fn len(&self) -> usize {
self.items.len()
}
/// Checks if the heap is empty.
///
/// # Returns
/// `true` if the heap is empty, `false` otherwise.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Adds a new element to the heap and maintains the heap property.
///
/// # Parameters
/// - `value`: The value to add to the heap.
pub fn add(&mut self, value: T) {
self.items.push(value);
self.heapify_up(self.len() - 1);
}
/// Removes and returns the root element from the heap.
///
/// # Returns
/// The root element if the heap is not empty, otherwise `None`.
pub fn pop(&mut self) -> Option<T> {
if self.is_empty() {
return None;
}
let next = Some(self.items.swap_remove(0));
if !self.is_empty() {
self.heapify_down(0);
}
next
}
/// Returns an iterator over the elements in the heap.
///
/// # Returns
/// An iterator over the elements in the heap, in their internal order.
pub fn iter(&self) -> Iter<'_, T> {
self.items.iter()
}
/// Moves an element upwards to restore the heap property.
///
/// # Parameters
/// - `idx`: The index of the element to heapify up.
fn heapify_up(&mut self, mut idx: usize) {
while let Some(pdx) = self.parent_idx(idx) {
if (self.comparator)(&self.items[idx], &self.items[pdx]) {
self.items.swap(idx, pdx);
idx = pdx;
} else {
break;
}
}
}
/// Moves an element downwards to restore the heap property.
///
/// # Parameters
/// - `idx`: The index of the element to heapify down.
fn heapify_down(&mut self, mut idx: usize) {
while self.children_present(idx) {
let cdx = {
if self.right_child_idx(idx) >= self.len() {
self.left_child_idx(idx)
} else {
let ldx = self.left_child_idx(idx);
let rdx = self.right_child_idx(idx);
if (self.comparator)(&self.items[ldx], &self.items[rdx]) {
ldx
} else {
rdx
}
}
};
if (self.comparator)(&self.items[cdx], &self.items[idx]) {
self.items.swap(idx, cdx);
idx = cdx;
} else {
break;
}
}
}
/// Returns the index of the parent of the element at `idx`.
///
/// # Parameters
/// - `idx`: The index of the element.
///
/// # Returns
/// The index of the parent element if it exists, otherwise `None`.
fn parent_idx(&self, idx: usize) -> Option<usize> {
if idx > 0 {
Some((idx - 1) / 2)
} else {
None
}
}
/// Checks if the element at `idx` has children.
///
/// # Parameters
/// - `idx`: The index of the element.
///
/// # Returns
/// `true` if the element has children, `false` otherwise.
fn children_present(&self, idx: usize) -> bool {
self.left_child_idx(idx) < self.len()
}
/// Returns the index of the left child of the element at `idx`.
///
/// # Parameters
/// - `idx`: The index of the element.
///
/// # Returns
/// The index of the left child.
fn left_child_idx(&self, idx: usize) -> usize {
idx * 2 + 1
}
/// Returns the index of the right child of the element at `idx`.
///
/// # Parameters
/// - `idx`: The index of the element.
///
/// # Returns
/// The index of the right child.
fn right_child_idx(&self, idx: usize) -> usize {
self.left_child_idx(idx) + 1
}
}
impl<T> Heap<T>
where
T: Ord,
{
/// Creates a new min-heap.
///
/// # Returns
/// A new `Heap` instance configured as a min-heap.
pub fn new_min() -> Heap<T> {
Self::new(|a, b| a < b)
}
/// Creates a new max-heap.
///
/// # Returns
/// A new `Heap` instance configured as a max-heap.
pub fn new_max() -> Heap<T> {
Self::new(|a, b| a > b)
}
/// Creates a min-heap from an unsorted vector.
///
/// # Parameters
/// - `items`: A vector of items to be turned into a min-heap.
///
/// # Returns
/// A `Heap` instance configured as a min-heap.
pub fn from_vec_min(items: Vec<T>) -> Heap<T> {
Self::from_vec(items, |a, b| a < b)
}
/// Creates a max-heap from an unsorted vector.
///
/// # Parameters
/// - `items`: A vector of items to be turned into a max-heap.
///
/// # Returns
/// A `Heap` instance configured as a max-heap.
pub fn from_vec_max(items: Vec<T>) -> Heap<T> {
Self::from_vec(items, |a, b| a > b)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_heap() {
let mut heap: Heap<i32> = Heap::new_max();
assert_eq!(heap.pop(), None);
}
#[test]
fn test_min_heap() {
let mut heap = Heap::new_min();
heap.add(4);
heap.add(2);
heap.add(9);
heap.add(11);
assert_eq!(heap.len(), 4);
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.pop(), Some(4));
assert_eq!(heap.pop(), Some(9));
heap.add(1);
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(11));
assert_eq!(heap.pop(), None);
}
#[test]
fn test_max_heap() {
let mut heap = Heap::new_max();
heap.add(4);
heap.add(2);
heap.add(9);
heap.add(11);
assert_eq!(heap.len(), 4);
assert_eq!(heap.pop(), Some(11));
assert_eq!(heap.pop(), Some(9));
assert_eq!(heap.pop(), Some(4));
heap.add(1);
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), None);
}
#[test]
fn test_iter_heap() {
let mut heap = Heap::new_min();
heap.add(4);
heap.add(2);
heap.add(9);
heap.add(11);
let mut iter = heap.iter();
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), Some(&9));
assert_eq!(iter.next(), Some(&11));
assert_eq!(iter.next(), None);
assert_eq!(heap.len(), 4);
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.pop(), Some(4));
assert_eq!(heap.pop(), Some(9));
assert_eq!(heap.pop(), Some(11));
assert_eq!(heap.pop(), None);
}
#[test]
fn test_from_vec_min() {
let vec = vec![3, 1, 4, 1, 5, 9, 2, 6, 5];
let mut heap = Heap::from_vec_min(vec);
assert_eq!(heap.len(), 9);
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(2));
heap.add(0);
assert_eq!(heap.pop(), Some(0));
}
#[test]
fn test_from_vec_max() {
let vec = vec![3, 1, 4, 1, 5, 9, 2, 6, 5];
let mut heap = Heap::from_vec_max(vec);
assert_eq!(heap.len(), 9);
assert_eq!(heap.pop(), Some(9));
assert_eq!(heap.pop(), Some(6));
assert_eq!(heap.pop(), Some(5));
heap.add(10);
assert_eq!(heap.pop(), Some(10));
}
}