Skip to content

Commit

Permalink
Improve mergeable_heap.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 committed Sep 1, 2024
1 parent 20812db commit a1a6754
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions rust/structures/mergeable_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ impl<V: PartialOrd> Heap<V> {
}))
}

fn merge(a: Option<Box<Heap<V>>>, b: Option<Box<Heap<V>>>) -> Option<Box<Heap<V>>> {
fn merge(mut a: Option<Box<Heap<V>>>, mut b: Option<Box<Heap<V>>>) -> Option<Box<Heap<V>>> {
if a.is_none() {
return b;
}
if b.is_none() {
return a;
}
let mut ra = a.unwrap();
let mut rb = b.unwrap();
if ra.value > rb.value {
swap(&mut ra, &mut rb);
if a.as_ref()?.value > b.as_ref()?.value {
swap(&mut a, &mut b);
}
let mut ha = a?;
if rand::random() {
swap(&mut ra.left, &mut ra.right);
swap(&mut ha.left, &mut ha.right);
}
ra.left = Self::merge(ra.left, Some(rb));
Some(ra)
ha.left = Self::merge(ha.left, b);
Some(ha)
}

fn remove_min(heap: Option<Box<Heap<V>>>) -> (Option<Box<Heap<V>>>, V) {
Expand Down

0 comments on commit a1a6754

Please sign in to comment.