Struct splay_tree::SplayHeap
source · pub struct SplayHeap<T> { /* private fields */ }
Expand description
A priority queue implemented with a splay tree.
This will be a max-heap.
A splay tree based heap is a self-adjusting data structure.
It performs pushing and popping in O(log n)
amortized time.
It is a logic error for a key to be modified in such a way that
the key’s ordering relative to any other key,
as determined by the Ord
trait, changes while it is in the map.
This is normally only possible through Cell
, RefCell
, global state, I/O, or unsafe code.
Examples
use splay_tree::SplayHeap;
let mut heap = SplayHeap::new();
heap.push(0);
heap.push(10);
heap.push(7);
assert_eq!(heap.peek(), Some(&10));
assert_eq!(heap.pop(), Some(10));
assert_eq!(heap.pop(), Some(7));
assert_eq!(heap.pop(), Some(0));
assert_eq!(heap.pop(), None);
Implementations§
source§impl<T> SplayHeap<T>where
T: Ord,
impl<T> SplayHeap<T>where
T: Ord,
sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty SplayHeap
as a max-heap.
Examples
use splay_tree::SplayHeap;
let mut heap = SplayHeap::new();
heap.push(10);
assert_eq!(heap.pop(), Some(10));
sourcepub fn peek(&mut self) -> Option<&T>
pub fn peek(&mut self) -> Option<&T>
Returns the greatest item in the heap, or None
if it is empty.
NOTICE
Because SplayHeap
is a self-adjusting amortized data structure,
this function requires the mut
qualifier.
Examples
use splay_tree::SplayHeap;
let mut heap = SplayHeap::new();
assert_eq!(heap.peek(), None);
heap.push(1);
heap.push(5);
heap.push(2);
assert_eq!(heap.peek(), Some(&5));
sourcepub fn peek_immut(&self) -> Option<&T>
pub fn peek_immut(&self) -> Option<&T>
Immutable version of SplayHeap::peek()
.
Note that this method could be less efficient than the mutable version.
Examples
use splay_tree::SplayHeap;
let mut heap = SplayHeap::new();
assert_eq!(heap.peek_immut(), None);
heap.push(1);
heap.push(5);
heap.push(2);
assert_eq!(heap.peek_immut(), Some(&5));
sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the greatest item from the heap and returns it, or None
if it is empty.
Examples
use splay_tree::SplayHeap;
let mut heap: SplayHeap<_> = vec![1, 3].into_iter().collect();
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), None);
source§impl<T> SplayHeap<T>
impl<T> SplayHeap<T>
sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns an iterator visiting all items in sorted (descending) order.
Examples
use splay_tree::SplayHeap;
let heap: SplayHeap<_> = vec![1, 4, 2, 3].into_iter().collect();
// Print all values in `heap` in sorted order
for x in heap.iter() {
println!("{}", x);
}
Trait Implementations§
source§impl<'a, T> Extend<&'a T> for SplayHeap<T>where
T: Copy + 'a + Ord,
impl<'a, T> Extend<&'a T> for SplayHeap<T>where
T: Copy + 'a + Ord,
source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a T>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<T> Extend<T> for SplayHeap<T>where
T: Ord,
impl<T> Extend<T> for SplayHeap<T>where
T: Ord,
source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)