1
0
Fork 0

day(24): implement min heap

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-07-03 10:57:23 +02:00
parent 0a7aea65e1
commit 113e927a65
Signed by: mfocko
GPG key ID: 7C47D46246790496
3 changed files with 33 additions and 0 deletions

View 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()
}
}

View file

@ -0,0 +1,2 @@
mod min_heap;
pub use min_heap::*;

View file

@ -9,3 +9,6 @@ pub use testing::*;
mod vectors; mod vectors;
pub use vectors::*; pub use vectors::*;
mod data_structures;
pub use data_structures::*;