tldr
TODO
- Add .into_iter_sorted() method to BinaryHeap
original post
use std::collections::*;
let heap = BinaryHeap::from(vec![1, 8, 5, 6, 3, 4, 0, 9, 7, 2]); // max heap from a vec
assert_eq!(
heap.into_iter().collect::<Vec<_>>(),
vec![9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
);
produces the following error:
Compiling playground v0.0.1 (file:///playground)
Finished dev [unoptimized + debuginfo] target(s) in 0.97 secs
Running `target/debug/playground`
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `[9, 8, 5, 7, 3, 4, 0, 6, 1, 2]`,
right: `[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]`', src/main.rs:7:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.
For me this is surprising but the lib reference says it is correct.
[Edit]
I use BinaryHeap inorder to get the values in max-first order.
It is ridiculous to return values in arbitrary order.
[/Edit]
It's just a thin wrapper of the underlying vector's iterator.
How can I get the values of max heap in the max-first order using iterator?
[Edit]
My opinion is that Iterator
for BinaryHeap
should abstract the following use case (sorry, C++):
while(!q.empty()) {
std::cout << q.top() << " ";
q.pop();
}
std::cout << '\n';
[/Edit]