File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -207,6 +207,30 @@ use super::SpecExtend;
207207/// // The heap should now be empty.
208208/// assert!(heap.is_empty())
209209/// ```
210+ ///
211+ /// ## Min-heap
212+ ///
213+ /// Either `std::cmp::Reverse` or a custom `Ord` implementation can be used to
214+ /// make `BinaryHeap` a min-heap. This makes `heap.pop()` return the smallest
215+ /// value instead of the greatest one.
216+ ///
217+ /// ```
218+ /// use std::collections::BinaryHeap;
219+ /// use std::cmp::Reverse;
220+ ///
221+ /// let mut heap = BinaryHeap::new();
222+ ///
223+ /// // Wrap values in `Reverse`
224+ /// heap.push(Reverse(1));
225+ /// heap.push(Reverse(5));
226+ /// heap.push(Reverse(2));
227+ ///
228+ /// // If we pop these scores now, they should come back in the reverse order.
229+ /// assert_eq!(heap.pop(), Some(Reverse(1)));
230+ /// assert_eq!(heap.pop(), Some(Reverse(2)));
231+ /// assert_eq!(heap.pop(), Some(Reverse(5)));
232+ /// assert_eq!(heap.pop(), None);
233+ /// ```
210234#[ stable( feature = "rust1" , since = "1.0.0" ) ]
211235pub struct BinaryHeap < T > {
212236 data : Vec < T > ,
You can’t perform that action at this time.
0 commit comments