-
Notifications
You must be signed in to change notification settings - Fork 73
/
heap.rs
480 lines (452 loc) · 10.7 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/// Heap implementation.
///
/// This is an internal structure used by the Min/Max Heap implementations.
struct Heap<T: Ord + Copy> {
pq: Vec<T>,
n: usize,
}
/// MaxHeap implementation.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MaxHeap;
///
/// let mut heap = MaxHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// assert_eq!(heap.is_empty(), false);
/// assert_eq!(heap.size(), 5);
/// assert_eq!(heap.del_max(), 5);
/// assert_eq!(heap.del_max(), 4);
/// assert_eq!(heap.del_max(), 3);
/// assert_eq!(heap.del_max(), 2);
/// assert_eq!(heap.del_max(), 1);
/// assert_eq!(heap.is_empty(), true);
/// ```
pub struct MaxHeap<T: Ord + Copy> {
heap: Heap<T>,
}
/// MinHeap implementation.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MinHeap;
///
/// let mut heap = MinHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// assert_eq!(heap.is_empty(), false);
/// assert_eq!(heap.size(), 5);
/// assert_eq!(heap.del_min(), 1);
/// assert_eq!(heap.del_min(), 2);
/// assert_eq!(heap.del_min(), 3);
/// assert_eq!(heap.del_min(), 4);
/// assert_eq!(heap.del_min(), 5);
/// assert_eq!(heap.is_empty(), true);
/// ```
pub struct MinHeap<T: Ord + Copy> {
heap: Heap<T>,
}
impl<T: Ord + Copy> Heap<T> {
fn new() -> Heap<T> {
Heap {
pq: Vec::new(),
n: 0,
}
}
fn is_empty(&self) -> bool {
self.n == 0
}
fn size(&self) -> usize {
self.n
}
fn insert(&mut self, key: T, less: fn(T, T) -> bool) {
if self.is_empty() {
self.pq.insert(0, key);
}
self.n += 1;
self.pq.insert(self.n, key);
self.swim(self.n, less);
}
fn del(&mut self, less: fn(T, T) -> bool) -> T {
let item = self.peek();
self.exch(1, self.pq.len() - 1);
self.pq.remove(self.pq.len() - 1);
self.n -= 1;
self.sink(1, less);
item
}
fn peek(&self) -> T {
if self.is_empty() {
panic!("Heap is empty")
}
self.pq[1]
}
fn exch(&mut self, i: usize, j: usize) {
self.pq.swap(i, j);
}
fn swim(&mut self, mut k: usize, less: fn(T, T) -> bool) {
while k > 1 && less(self.pq[k / 2], self.pq[k]) {
self.pq.swap((k / 2) as usize, k as usize);
k = k / 2;
}
}
fn sink(&mut self, mut k: usize, less: fn(T, T) -> bool) {
while 2 * k <= self.n {
let mut j = 2 * k;
if j < self.n && less(self.pq[j], self.pq[j + 1]) {
j += 1;
}
if !less(self.pq[k], self.pq[j]) {
break;
}
self.pq.swap(k as usize, j as usize);
k = j
}
}
fn iter(&mut self) -> Vec<T> {
self.pq[1..].to_vec()
}
}
/// MaxHeap implementation.
impl<T: Ord + Copy> MaxHeap<T> {
/// Creates a new `MaxHeap`` instance.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MaxHeap;
///
/// let heap = MaxHeap::<i32>::new();
///
/// assert_eq!(heap.is_empty(), true);
/// assert_eq!(heap.size(), 0);
/// ```
pub fn new() -> MaxHeap<T> {
MaxHeap { heap: Heap::new() }
}
/// Inserts a new key into the `MaxHeap`.
///
/// # Arguments:
///
/// * `key` - The key to be inserted into the `MaxHeap`.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MaxHeap;
///
/// let mut heap = MaxHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// assert_eq!(heap.is_empty(), false);
/// assert_eq!(heap.size(), 5);
/// ```
pub fn insert(&mut self, key: T) {
self.heap.insert(key, less_max);
}
/// Checks if the `MaxHeap` is empty.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MaxHeap;
///
/// let mut heap = MaxHeap::<i32>::new();
///
/// assert_eq!(heap.is_empty(), true);
///
/// heap.insert(1);
///
/// assert_eq!(heap.is_empty(), false);
/// ```
pub fn is_empty(&self) -> bool {
self.heap.is_empty()
}
/// Returns the size of the `MaxHeap`.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MaxHeap;
///
///
/// let mut heap = MaxHeap::<i32>::new();
///
/// assert_eq!(heap.size(), 0);
///
/// heap.insert(1);
///
/// assert_eq!(heap.size(), 1);
/// ```
pub fn size(&self) -> usize {
self.heap.size()
}
/// Gets the maximum key in the `MaxHeap`.
///
/// # Returns:
///
/// The maximum key in the `MaxHeap`.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MaxHeap;
///
/// let mut heap = MaxHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// assert_eq!(heap.peek(), 5);
/// ```
pub fn peek(&self) -> T {
self.heap.peek()
}
/// Deletes the maximum key in the `MaxHeap`.
///
/// # Returns:
///
/// The maximum key in the `MaxHeap`.
///
/// # Panics:
///
/// If the heap is empty.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MaxHeap;
///
/// let mut heap = MaxHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// assert_eq!(heap.del_max(), 5);
/// assert_eq!(heap.del_max(), 4);
/// assert_eq!(heap.del_max(), 3);
/// assert_eq!(heap.del_max(), 2);
/// assert_eq!(heap.del_max(), 1);
/// ```
pub fn del_max(&mut self) -> T {
self.heap.del(less_max)
}
/// Returns an iterator over the MaxHeap.
///
/// # Returns:
///
/// An iterator over the heap. The iterator will yield the keys in an arbitrary order.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MaxHeap;
///
/// let mut heap = MaxHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// let mut heap_iter = heap.iter();
///
/// heap_iter.sort();
/// let mut counter = 1;
///
/// for i in heap_iter.iter() {
/// assert_eq!(*i, counter);
/// counter += 1;
/// }
pub fn iter(&mut self) -> Vec<T> {
self.heap.iter()
}
}
/// MinHeap implementation.
impl<T: Ord + Copy> MinHeap<T> {
/// Creates a new `MinHeap`` instance.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MinHeap;
///
/// let heap = MinHeap::<i32>::new();
///
/// assert_eq!(heap.is_empty(), true);
/// assert_eq!(heap.size(), 0);
/// ```
pub fn new() -> MinHeap<T> {
MinHeap { heap: Heap::new() }
}
/// Inserts a new key into the `MinHeap`.
///
/// # Arguments:
///
/// * `key` - The key to be inserted into the `MinHeap`.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MinHeap;
///
/// let mut heap = MinHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// assert_eq!(heap.is_empty(), false);
/// assert_eq!(heap.size(), 5);
/// ```
pub fn insert(&mut self, key: T) {
self.heap.insert(key, less_min);
}
/// Checks if the `MinHeap` is empty.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MinHeap;
///
/// let mut heap = MinHeap::<i32>::new();
///
/// assert_eq!(heap.is_empty(), true);
///
/// heap.insert(1);
///
/// assert_eq!(heap.is_empty(), false);
/// ```
pub fn is_empty(&self) -> bool {
self.heap.is_empty()
}
/// Returns the size of the `MinHeap`.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MinHeap;
///
/// let mut heap = MinHeap::<i32>::new();
///
/// assert_eq!(heap.size(), 0);
///
/// heap.insert(1);
///
/// assert_eq!(heap.size(), 1);
/// ```
pub fn size(&self) -> usize {
self.heap.size()
}
/// Gets the minimum key in the `MinHeap`.
///
/// # Returns:
///
/// The minimum key in the `MinHeap`.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MinHeap;
///
/// let mut heap = MinHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// assert_eq!(heap.peek(), 1);
///
/// ```
pub fn peek(&self) -> T {
self.heap.peek()
}
/// Deletes the minimum key in the `MinHeap`.
///
/// # Returns:
///
/// The minimum key in the `MinHeap`.
///
/// # Panics:
///
/// If the heap is empty.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MinHeap;
///
/// let mut heap = MinHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// assert_eq!(heap.del_min(), 1);
/// assert_eq!(heap.del_min(), 2);
/// assert_eq!(heap.del_min(), 3);
/// assert_eq!(heap.del_min(), 4);
/// assert_eq!(heap.del_min(), 5);
/// ```
pub fn del_min(&mut self) -> T {
self.heap.del(less_min)
}
/// Returns an iterator over the MinHeap.
///
/// # Returns:
///
/// An iterator over the heap. The iterator will yield the keys in an arbitrary order.
///
/// # Examples:
///
/// ```rust
/// use rust_algorithms::data_structures::MinHeap;
///
/// let mut heap = MinHeap::<i32>::new();
/// heap.insert(1);
/// heap.insert(2);
/// heap.insert(3);
/// heap.insert(4);
/// heap.insert(5);
///
/// let mut heap_iter = heap.iter();
///
/// heap_iter.sort();
/// let mut counter = 1;
///
/// for i in heap_iter.iter() {
/// assert_eq!(*i, counter);
/// counter += 1;
/// }
pub fn iter(&mut self) -> Vec<T> {
self.heap.iter()
}
}
fn less_max<T: Ord + Copy>(i: T, j: T) -> bool {
i.lt(&j)
}
fn less_min<T: Ord + Copy>(i: T, j: T) -> bool {
!i.lt(&j)
}