pub struct MinHeap<T>where
T: Ord,{ /* private fields */ }Expand description
A binary min-heap backed by a Vec.
The smallest element (by Ord) is always at the top. Every push and pop
runs in O(log n); peek is O(1).
§Examples
use graph_collections::MinHeap;
let mut heap = MinHeap::new();
heap.push(5u32);
heap.push(1);
heap.push(3);
assert_eq!(heap.peek(), Some(&1));
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(5));
assert!(heap.is_empty());Implementations§
Source§impl<T> MinHeap<T>where
T: Ord,
impl<T> MinHeap<T>where
T: Ord,
Sourcepub fn new() -> MinHeap<T>
pub fn new() -> MinHeap<T>
Creates a new, empty min-heap.
§Examples
use graph_collections::MinHeap;
let heap: MinHeap<u32> = MinHeap::new();
assert!(heap.is_empty());Sourcepub fn with_capacity(capacity: usize) -> MinHeap<T>
pub fn with_capacity(capacity: usize) -> MinHeap<T>
Creates a new, empty min-heap with at least the given capacity
pre-allocated, avoiding reallocations for the first capacity pushes.
§Examples
use graph_collections::MinHeap;
let heap: MinHeap<u32> = MinHeap::with_capacity(64);
assert!(heap.is_empty());Source§impl<T> MinHeap<T>where
T: Ord,
impl<T> MinHeap<T>where
T: Ord,
Source§impl<T> MinHeap<T>where
T: Ord,
impl<T> MinHeap<T>where
T: Ord,
Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Returns a reference to the minimum element without removing it,
or None if the heap is empty.
§Examples
use graph_collections::MinHeap;
let mut heap: MinHeap<u32> = MinHeap::new();
assert_eq!(heap.peek(), None);
heap.push(5);
heap.push(1);
heap.push(3);
assert_eq!(heap.peek(), Some(&1)); // minimum, not removed
assert_eq!(heap.len(), 3);Source§impl<T> MinHeap<T>where
T: Ord,
impl<T> MinHeap<T>where
T: Ord,
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes and returns the minimum element in O(log n),
or None if the heap is empty.
§Examples
use graph_collections::MinHeap;
let mut heap: MinHeap<u32> = MinHeap::new();
assert_eq!(heap.pop(), None);
heap.push(5);
heap.push(1);
heap.push(3);
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(5));
assert_eq!(heap.pop(), None);Trait Implementations§
Source§impl<T> Extend<T> for MinHeap<T>where
T: Ord,
impl<T> Extend<T> for MinHeap<T>where
T: Ord,
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Pushes all elements produced by the iterator into the heap.
§Examples
use graph_collections::MinHeap;
let mut heap: MinHeap<u32> = MinHeap::new();
heap.push(10);
heap.extend([2, 7, 1]);
assert_eq!(heap.peek(), Some(&1));
assert_eq!(heap.len(), 4);Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl<T> FromIterator<T> for MinHeap<T>where
T: Ord,
impl<T> FromIterator<T> for MinHeap<T>where
T: Ord,
Source§fn from_iter<I>(iter: I) -> MinHeap<T>where
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> MinHeap<T>where
I: IntoIterator<Item = T>,
Builds a MinHeap from any iterator.
Uses the O(n) heapify algorithm (build-heap) rather than O(n log n) successive pushes.
§Examples
use graph_collections::MinHeap;
let heap: MinHeap<u32> = vec![5, 3, 1, 4, 2].into_iter().collect();
assert_eq!(heap.peek(), Some(&1));
assert_eq!(heap.len(), 5);Source§impl<T> IntoIterator for MinHeap<T>where
T: Ord,
impl<T> IntoIterator for MinHeap<T>where
T: Ord,
Source§fn into_iter(self) -> <MinHeap<T> as IntoIterator>::IntoIter
fn into_iter(self) -> <MinHeap<T> as IntoIterator>::IntoIter
Consumes the heap and yields elements in ascending order.
§Examples
use graph_collections::MinHeap;
let heap: MinHeap<u32> = vec![4, 2, 5, 1, 3].into_iter().collect();
let sorted: Vec<u32> = heap.into_iter().collect();
assert_eq!(sorted, [1, 2, 3, 4, 5]);impl<T> Eq for MinHeap<T>
impl<T> StructuralPartialEq for MinHeap<T>where
T: Ord,
Auto Trait Implementations§
impl<T> Freeze for MinHeap<T>
impl<T> RefUnwindSafe for MinHeap<T>where
T: RefUnwindSafe,
impl<T> Send for MinHeap<T>where
T: Send,
impl<T> Sync for MinHeap<T>where
T: Sync,
impl<T> Unpin for MinHeap<T>where
T: Unpin,
impl<T> UnwindSafe for MinHeap<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