On this page
  1. BinaryHeap
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

BinaryHeap

Prev: `VecDeque` | Up: collections | Next: smart_pointers
Rel: collections, rust/builtins
Ref: Rust std — BinaryHeap
Tags: #public #prog #rust #stdlib #collections #tier/standard


Signature & Overview

BinaryHeap<T>

Array-backed max-heap priority queue ($O(\log n)$ push/pop, $O(1)$ peek).

Notes & Pitfalls

Example

use std::cmp::Reverse;
use std::collections::BinaryHeap;

fn main() {
    let mut min_heap = BinaryHeap::from([Reverse(30), Reverse(10), Reverse(20)]);
    assert_eq!(min_heap.pop(), Some(Reverse(10)));
    assert_eq!(min_heap.pop(), Some(Reverse(20)));
}

Linked from

Related notes