day(24): implement min heap
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
0a7aea65e1
commit
113e927a65
3 changed files with 33 additions and 0 deletions
28
src/data_structures/min_heap.rs
Normal file
28
src/data_structures/min_heap.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
use std::cmp::{Ord, Reverse};
|
||||||
|
use std::collections::BinaryHeap;
|
||||||
|
|
||||||
|
pub struct MinHeap<T> {
|
||||||
|
heap: BinaryHeap<Reverse<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Ord> MinHeap<T> {
|
||||||
|
pub fn new() -> MinHeap<T> {
|
||||||
|
MinHeap {
|
||||||
|
heap: BinaryHeap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push(&mut self, item: T) {
|
||||||
|
self.heap.push(Reverse(item))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pop(&mut self) -> Option<T> {
|
||||||
|
self.heap.pop().map(|Reverse(x)| x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Ord> Default for MinHeap<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
2
src/data_structures/mod.rs
Normal file
2
src/data_structures/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
mod min_heap;
|
||||||
|
pub use min_heap::*;
|
|
@ -9,3 +9,6 @@ pub use testing::*;
|
||||||
|
|
||||||
mod vectors;
|
mod vectors;
|
||||||
pub use vectors::*;
|
pub use vectors::*;
|
||||||
|
|
||||||
|
mod data_structures;
|
||||||
|
pub use data_structures::*;
|
||||||
|
|
Loading…
Reference in a new issue