Fibonacci Heap in Rust
A high-performance Fibonacci Heap implementation in Rust with generic type support. The Fibonacci Heap is a heap data structure consisting of a collection of trees that satisfies the minimum-heap property. Compared to binary heaps, it offers superior amortized time complexities for decrease-key and merge, making it particularly useful in graph algorithms like Dijkstra's and Prim's.
Features
Time Complexities
| Operation | Amortized complexity |
|---|---|
| Insert | O(1) |
| Peek Min | O(1) |
| Peek Min Node | O(1) |
| Merge | O(1) |
| Decrease Key | O(1) |
| Extract Min | O(log n) |
| Delete | O(log n) |
| Into Sorted Vec | O(n log n) |
Supported Operations
- Insert — add a new element; returns an
Rc<RefCell<Node<T>>>handle for later updates. - Extract Min — remove and return the smallest element.
- Decrease Key — reduce the key of a node referenced by a previously obtained handle.
- Delete — remove an arbitrary node by handle in O(log n) amortized.
- Merge — merge two heaps in O(1) without ID conflicts.
- Peek Min — read the minimum key without removing it.
- Peek Min Node — return an
Rchandle to the minimum node for use withdecrease_key/delete. - Into Sorted Vec — consume the heap and return all keys in ascending order.
- Validate Node — O(1) check whether a handle still refers to a node in the heap.
- Clear — remove all elements; all previously issued handles are invalidated.
Standard Rust Traits
IntoIterator— consuming iterator yielding keys in ascending order with exactsize_hint.FromIterator<T>— build a heap directly from any iterator:iter.collect::<GenericFibonacciHeap<_>>().Extend<T>— bulk-insert from an iterator:heap.extend(values).
Supported Types
Any type that implements PartialOrd + Clone + Debug + 'static can be used as a key.
Predefined type aliases are provided for convenience:
| Alias | Key type |
|---|---|
FibonacciHeap |
i32 (backward-compat default) |
FibonacciHeapI8 … FibonacciHeapI128 |
signed integers |
FibonacciHeapU8 … FibonacciHeapU128 |
unsigned integers |
FibonacciHeapISize / FibonacciHeapUSize |
isize / usize |
FibonacciHeapF32 / FibonacciHeapF64 |
floats |
FibonacciHeapChar |
char |
Architecture
- Generic implementation — a single
GenericFibonacciHeap<T>covers all key types. NodeReftrait — abstraction over node handles; providesget_key(),get_id(), andvalidate().- Private
keyfield — the key is read-only from outside the heap; mutation is only possible throughdecrease_key, preserving heap invariants. - Per-node validity flag — each node carries a
valid: boolthat is set tofalseon extraction orclear(), allowing O(1) stale-handle detection without a global registry. - Correct
max_degreebound — consolidation uses⌊log₂ n⌋ + ⌊log₂ n⌋/2 + 2 ≈ 1.5·log₂ n, matching the theoretical Fibonacci-heap degree bound and avoiding mid-loop reallocations.
Example Usage
Basic Integer Heap
use ;
Reading a Node's Key
The key field is private to protect heap invariants. Use the NodeRef trait or
the key() method on the borrowed node:
use ;
let mut heap = new;
let node = heap.insert.unwrap;
// Via the NodeRef trait (clones the value)
let k: i32 = node.get_key;
assert_eq!;
// Via a direct borrow (zero-copy reference)
let k_ref: &i32 = &*node.borrow; // borrows Node<i32>, then calls key()
// or equivalently:
assert_eq!;
Using Type Aliases
use ;
Merging Two Heaps
Nodes from both heaps retain correct validity after a merge — there is no ID-space collision between independently created heaps.
use FibonacciHeapI32;
let mut heap1 = new;
heap1.insert.unwrap;
heap1.insert.unwrap;
let mut heap2 = new;
heap2.insert.unwrap;
heap2.insert.unwrap;
// Merge heap2 into heap1; heap2 is consumed
heap1.merge;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Error Handling
use ;
Deleting an Arbitrary Node
use GenericFibonacciHeap;
let mut heap = new;
heap.insert.unwrap;
let mid = heap.insert.unwrap;
heap.insert.unwrap;
// Remove 50 without knowing its position in the tree
heap.delete.unwrap;
assert_eq!;
// The handle is now invalid
assert!;
assert_eq!;
assert_eq!;
Iterator, FromIterator, Extend
use GenericFibonacciHeap;
// Build from an iterator
let heap: = vec!.into_iter.collect;
// Consume as a sorted iterator
let sorted: = heap.into_iter.collect;
assert_eq!;
// Extend an existing heap
let mut heap2 = new;
heap2.insert.unwrap;
heap2.extend;
assert_eq!;
Node Validity and clear()
use GenericFibonacciHeap;
let mut heap = new;
let handle = heap.insert.unwrap;
assert!; // present in heap
heap.clear;
// After clear(), all previously issued handles are invalidated
assert!;
assert!;
Working with Other Types
use ;
License
This project is licensed under the MIT License — see the LICENSE file for details.