pub struct BinaryHeap<T> { /* private fields */ }Expand description
A priority queue implementation backed by a binary heap.
BinaryHeap::pop removes the smallest item.
§Example
use rust_dsa::BinaryHeap;
// First, we create a new heap.
let mut heap = BinaryHeap::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 = BinaryHeap::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);§Runtime complexity
| Operation | Runtime Complexity |
|---|---|
BinaryHeap::insert | O(log n) |
BinaryHeap::peek | O(1) |
BinaryHeap::pop | O(log n) |
BinaryHeap::from | O(n) |
Implementations§
Source§impl<T> BinaryHeap<T>
impl<T> BinaryHeap<T>
Sourcepub fn insert(&mut self, item: T)where
T: Ord,
pub fn insert(&mut self, item: T)where
T: Ord,
Inserts an item into the binary heap.
§Example
use rust_dsa::BinaryHeap;
let mut heap = BinaryHeap::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 binary heap, or None if the heap is empty.
§Example
use rust_dsa::BinaryHeap;
let mut heap = BinaryHeap::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 binary heap, or returns None
if the heap is empty.
§Example
use rust_dsa::BinaryHeap;
let mut heap = BinaryHeap::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 len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the binary heap.
§Example
use rust_dsa::BinaryHeap;
let mut heap = BinaryHeap::from([1, 2, 3]);
assert_eq!(heap.len(), 3);
heap.clear();
assert_eq!(heap.len(), 0);Trait Implementations§
Source§impl<T: Clone> Clone for BinaryHeap<T>
impl<T: Clone> Clone for BinaryHeap<T>
Source§fn clone(&self) -> BinaryHeap<T>
fn clone(&self) -> BinaryHeap<T>
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T> Default for BinaryHeap<T>
impl<T> Default for BinaryHeap<T>
Source§fn default() -> BinaryHeap<T>
fn default() -> BinaryHeap<T>
Returns the “default value” for a type. Read more
Source§impl<T, const N: usize> From<[T; N]> for BinaryHeap<T>where
T: Ord,
impl<T, const N: usize> From<[T; N]> for BinaryHeap<T>where
T: Ord,
Source§fn from(array: [T; N]) -> BinaryHeap<T>where
T: Ord,
fn from(array: [T; N]) -> BinaryHeap<T>where
T: Ord,
Uses the heapify algorithm to create a BinaryHeap in O(n) time.
Source§impl<T> FromIterator<T> for BinaryHeap<T>where
T: Ord,
impl<T> FromIterator<T> for BinaryHeap<T>where
T: Ord,
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> BinaryHeap<T>
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> BinaryHeap<T>
Uses the heapify algorithm to create a BinaryHeap in O(n) time.
Source§impl<T> IntoIterator for BinaryHeap<T>where
T: Ord,
impl<T> IntoIterator for BinaryHeap<T>where
T: Ord,
Auto Trait Implementations§
impl<T> Freeze for BinaryHeap<T>
impl<T> RefUnwindSafe for BinaryHeap<T>where
T: RefUnwindSafe,
impl<T> Send for BinaryHeap<T>where
T: Send,
impl<T> Sync for BinaryHeap<T>where
T: Sync,
impl<T> Unpin for BinaryHeap<T>where
T: Unpin,
impl<T> UnwindSafe for BinaryHeap<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