MinHeap

Struct MinHeap 

Source
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,

Source

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());
Source

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,

Source

pub fn is_empty(&self) -> bool

Returns true if the heap contains no elements.

§Examples
use graph_collections::MinHeap;

let mut heap: MinHeap<u32> = MinHeap::new();
assert!(heap.is_empty());
heap.push(1);
assert!(!heap.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of elements in the heap.

§Examples
use graph_collections::MinHeap;

let mut heap: MinHeap<u32> = MinHeap::new();
assert_eq!(heap.len(), 0);
heap.push(1);
heap.push(2);
assert_eq!(heap.len(), 2);
Source§

impl<T> MinHeap<T>
where T: Ord,

Source

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,

Source

pub fn push(&mut self, value: T)

Inserts value into the heap in O(log n).

After each push, peek returns the overall minimum.

§Examples
use graph_collections::MinHeap;

let mut heap: MinHeap<u32> = MinHeap::new();
heap.push(3);
heap.push(1);
heap.push(2);
assert_eq!(heap.peek(), Some(&1));
Source§

impl<T> MinHeap<T>
where T: Ord,

Source

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);
Source

pub fn clear(&mut self)

Removes all elements from the heap.

§Examples
use graph_collections::MinHeap;

let mut heap: MinHeap<u32> = (1..=5).collect();
heap.clear();
assert!(heap.is_empty());

Trait Implementations§

Source§

impl<T> Clone for MinHeap<T>
where T: Clone + Ord,

Source§

fn clone(&self) -> MinHeap<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for MinHeap<T>
where T: Debug + Ord,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Default for MinHeap<T>
where T: Ord,

Source§

fn default() -> MinHeap<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Extend<T> for MinHeap<T>
where T: Ord,

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

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,

Source§

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,

Source§

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]);
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = MinHeapIntoIter<T>

Which kind of iterator are we turning this into?
Source§

impl<T> PartialEq for MinHeap<T>
where T: PartialEq + Ord,

Source§

fn eq(&self, other: &MinHeap<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Eq for MinHeap<T>
where T: Eq + Ord,

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.