Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rust mergeable heap #204

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"

edition = "2021"

[dependencies]
rand = "0.8.5"

[dev-dependencies]
rstest="0.22.0"

Expand Down
75 changes: 75 additions & 0 deletions rust/structures/mergeable_heap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::mem::swap;
use std::rc::Rc;

struct Heap<V> {
value: V,
left: Option<Rc<Heap<V>>>,
right: Option<Rc<Heap<V>>>,
}

impl<V: PartialOrd + Clone> Heap<V> {
fn new(value: V) -> Option<Rc<Heap<V>>> {
Some(Rc::new(Self {
value,
left: None,
right: None,
}))
}
fn merge(a: &Option<Rc<Heap<V>>>, b: &Option<Rc<Heap<V>>>) -> Option<Rc<Heap<V>>> {
if a.is_none() {
return b.clone();
}
if b.is_none() {
return a.clone();
}
let mut ra = a.clone()?;
let mut rb = b.clone()?;
if ra.value > rb.value {
swap(&mut ra, &mut rb);
}
if rand::random() {
ra = Rc::new(Heap {
value: ra.value.clone(),
left: ra.right.clone(),
right: ra.left.clone(),
});
}
Some(Rc::new(Heap {
value: ra.value.clone(),
left: Self::merge(&ra.left.clone(), &Some(rb)),
right: ra.right.clone(),
}))
}

fn remove_min(heap: &Option<Rc<Heap<V>>>) -> (Option<Rc<Heap<V>>>, V) {
let h = heap.as_ref().unwrap();
(
Self::merge(&h.as_ref().left, &h.as_ref().right),
h.as_ref().value.clone(),
)
}

fn add(heap: &Option<Rc<Heap<V>>>, value: V) -> Option<Rc<Heap<V>>> {
Self::merge(heap, &Heap::new(value))
}
}

#[cfg(test)]
mod tests {
use crate::structures::mergeable_heap::Heap;

#[test]
fn basic_test() {
let mut h = None;
h = Heap::add(&h, 3);
h = Heap::add(&h, 1);
h = Heap::add(&h, 2);
let mut values = Vec::new();
while h.is_some() {
let (heap, min_value) = Heap::remove_min(&h);
values.push(min_value);
h = heap;
}
assert_eq!(values, [1, 2, 3]);
}
}
1 change: 1 addition & 0 deletions rust/structures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod disjoint_sets;
pub mod fenwick_tree;
pub mod mergeable_heap;
pub mod persistent_tree;
Loading