pub struct FibonacciHeap<T> { /* private fields */ }Expand description
A priority queue implementation backed by a Fibonacci heap.
FibonacciHeap::pop removes the smallest item.
TODO: implement change_value.
§Example
use rust_dsa::FibonacciHeap;
// First, we create a new heap.
let mut heap = FibonacciHeap::new();
// Then we can add items in any order.
heap.insert(4);
heap.insert(1);
heap.insert(3);
// We can peek at the minimum item.
assert_eq!(heap.peek(), Some(&1));
// And pop them off in ascending order.
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(4));
assert_eq!(heap.pop(), None);
// We can also create heaps from arrays.
let mut heap = FibonacciHeap::from([2, 6, 2]);
// And heaps can contain duplicate items.
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.len(), 1);
// We can also join two heaps together.
let mut heap: FibonacciHeap<_> = "xbz".chars().collect();
let other: FibonacciHeap<_> = "ayc".chars().collect();
heap.extend(other);
assert_eq!(heap.len(), 6);
assert_eq!(heap.pop(), Some('a'));
assert_eq!(heap.pop(), Some('b'));
assert_eq!(heap.pop(), Some('c'));
assert_eq!(heap.pop(), Some('x'));
assert_eq!(heap.pop(), Some('y'));
assert_eq!(heap.pop(), Some('z'));
assert_eq!(heap.pop(), None);§Runtime complexity
| Operation | Runtime Complexity |
|---|---|
FibonacciHeap::insert | O(1) |
FibonacciHeap::peek | O(1) |
FibonacciHeap::pop | O(log n) |
FibonacciHeap::extend | O(1) |
FibonacciHeap::from | unclear… |
Implementations§
Source§impl<T> FibonacciHeap<T>
impl<T> FibonacciHeap<T>
Sourcepub fn new() -> FibonacciHeap<T>
pub fn new() -> FibonacciHeap<T>
Creates a new heap.
Sourcepub fn insert(&mut self, value: T)where
T: Ord,
pub fn insert(&mut self, value: T)where
T: Ord,
Inserts a value into the heap.
§Example
use rust_dsa::FibonacciHeap;
let mut heap = FibonacciHeap::new();
heap.insert(4);
heap.insert(1);
heap.insert(3);
assert_eq!(heap.len(), 3);
assert_eq!(heap.peek(), Some(&1));Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Returns the smallest item in the heap, or None if the heap is empty.
§Example
use rust_dsa::FibonacciHeap;
let mut heap = FibonacciHeap::from([2, 1]);
assert_eq!(heap.peek(), Some(&1));
heap.clear();
assert_eq!(heap.peek(), None);Sourcepub fn pop(&mut self) -> Option<T>where
T: Ord,
pub fn pop(&mut self) -> Option<T>where
T: Ord,
Removes and returns the smallest item in the heap, or returns None
if the heap is empty.
§Example
use rust_dsa::FibonacciHeap;
let mut heap = FibonacciHeap::from([4, 1, 3]);
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(4));
assert_eq!(heap.pop(), None);Sourcepub fn extend(&mut self, other: FibonacciHeap<T>)where
T: Ord,
pub fn extend(&mut self, other: FibonacciHeap<T>)where
T: Ord,
Inserts the elements from annother heap into this one.
§Example
use rust_dsa::FibonacciHeap;
let mut heap = FibonacciHeap::from([3, 4, 5]);
let other = FibonacciHeap::from([1, 2]);
heap.extend(other);
assert_eq!(heap.len(), 5);
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(4));
assert_eq!(heap.pop(), Some(5));
assert_eq!(heap.pop(), None);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the heap.
§Example
use rust_dsa::FibonacciHeap;
let mut heap = FibonacciHeap::from([1, 2, 3]);
assert_eq!(heap.len(), 3);
heap.clear();
assert_eq!(heap.len(), 0);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the heap is empty.
§Example
use rust_dsa::FibonacciHeap;
let mut heap = FibonacciHeap::from([1, 2]);
assert!(!heap.is_empty());
heap.clear();
assert!(heap.is_empty());Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the heap.
§Example
use rust_dsa::FibonacciHeap;
let mut heap = FibonacciHeap::from([1, 2]);
heap.clear();
assert!(heap.is_empty());Sourcepub fn consolidate(&mut self)where
T: Ord,
pub fn consolidate(&mut self)where
T: Ord,
Consolidates the heap so no two nodes have the same rank.
Trait Implementations§
Source§impl<T> Default for FibonacciHeap<T>
impl<T> Default for FibonacciHeap<T>
Source§fn default() -> FibonacciHeap<T>
fn default() -> FibonacciHeap<T>
Returns the “default value” for a type. Read more
Source§impl<T> FromIterator<T> for FibonacciHeap<T>where
T: Ord,
impl<T> FromIterator<T> for FibonacciHeap<T>where
T: Ord,
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> FibonacciHeap<T>
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> FibonacciHeap<T>
Creates a heap from an iterator.
use rust_dsa::FibonacciHeap;
let mut heap: FibonacciHeap<i64> = (0..10_001).map(|_| rand::random()).collect();
let mut prev = heap.pop().unwrap();
for _ in 0..10_000 {
assert!(&prev <= heap.peek().unwrap());
prev = heap.pop().unwrap();
}
assert_eq!(heap.pop(), None);Source§impl<T> IntoIterator for FibonacciHeap<T>where
T: Ord,
impl<T> IntoIterator for FibonacciHeap<T>where
T: Ord,
Auto Trait Implementations§
impl<T> Freeze for FibonacciHeap<T>
impl<T> RefUnwindSafe for FibonacciHeap<T>where
T: RefUnwindSafe,
impl<T> Send for FibonacciHeap<T>where
T: Send,
impl<T> Sync for FibonacciHeap<T>where
T: Sync,
impl<T> Unpin for FibonacciHeap<T>where
T: Unpin,
impl<T> UnwindSafe for FibonacciHeap<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more