Module binary_heap
A priority queue implemented with a binary heap.
Insertion and popping the largest element have O(log(n)) time complexity. Checking the largest element is O(1). Converting a vector to a binary heap can be done in-place, and has O(n) complexity. A binary heap can also be converted to a sorted vector in-place, allowing it to be used for an O(n * log(n)) in-place heapsort.
Examples
This is a larger example that implements Dijkstra's algorithm
to solve the shortest path problem on a directed graph.
It shows how to use BinaryHeap with custom types.
use Ordering;
use BinaryHeap;
// The priority queue depends on `Ord`.
// Explicitly implement the trait so the queue becomes a min-heap
// instead of a max-heap.
// `PartialOrd` needs to be implemented as well.
// Each node is represented as a `usize`, for a shorter implementation.
// Dijkstra's shortest path algorithm.
// Start at `start` and use `dist` to track the current shortest distance
// to each node. This implementation isn't memory-efficient as it may leave duplicate
// nodes in the queue. It also uses `usize::MAX` as a sentinel value,
// for a simpler implementation.
Structs
- BinaryHeap A priority queue implemented with a binary heap.
-
Drain
A draining iterator over the elements of a
BinaryHeap. -
DrainSorted
A draining iterator over the elements of a
BinaryHeap. -
IntoIter
An owning iterator over the elements of a
BinaryHeap. - IntoIterSorted
-
Iter
An iterator over the elements of a
BinaryHeap. -
PeekMut
Structure wrapping a mutable reference to the greatest item on a
BinaryHeap.