rust_dsa

Struct BinaryHeap

Source
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

OperationRuntime Complexity
BinaryHeap::insertO(log n)
BinaryHeap::peekO(1)
BinaryHeap::popO(log n)
BinaryHeap::fromO(n)

Implementations§

Source§

impl<T> BinaryHeap<T>

Source

pub fn new() -> Self

Creates an empty binary heap.

Source

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

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

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

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

pub fn is_empty(&self) -> bool

Returns true if the binary heap is empty.

§Example
use rust_dsa::BinaryHeap;

let mut heap = BinaryHeap::from([1, 2]);
assert!(!heap.is_empty());

heap.clear();
assert!(heap.is_empty());
Source

pub fn clear(&mut self)

Clears the binary heap.

§Example
use rust_dsa::BinaryHeap;

let mut heap = BinaryHeap::from([1, 2]);

heap.clear();
assert!(heap.is_empty());

Trait Implementations§

Source§

impl<T: Clone> Clone for BinaryHeap<T>

Source§

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

Returns a copy 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> Default for BinaryHeap<T>

Source§

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,

Source§

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,

Source§

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,

Source§

fn into_iter(self) -> IntoIter<T>

Creates an iterator that iterates over the heap’s items in ascending order.

§Example
use rust_dsa::BinaryHeap;

let heap = BinaryHeap::from([4, 2, 3, 1]);

for x in heap {
    // prints 1, 2, 3, 4
    println!("{x}");
}
Source§

type IntoIter = IntoIter<T>

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

type Item = T

The type of the elements being iterated over.

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> 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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.