Struct splay_tree::SplayHeap [] [src]

pub struct SplayHeap<T> { /* fields omitted */ }

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

Methods

impl<T> SplayHeap<T> where
    T: Ord
[src]

[src]

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

[src]

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

[src]

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

[src]

Pushes an item onto the heap.

Examples

use splay_tree::SplayHeap;
let mut heap = SplayHeap::new();
heap.push(3);
heap.push(5);
heap.push(1);

assert_eq!(heap.len(), 3);
assert_eq!(heap.peek(), Some(&5));

[src]

Drops all items from the heap.

Examples

use splay_tree::SplayHeap;
let mut heap: SplayHeap<_> = vec![1, 3].into_iter().collect();

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

impl<T> SplayHeap<T>
[src]

[src]

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

[src]

Returns the length of the heap.

Examples

use splay_tree::SplayHeap;
let heap: SplayHeap<_> = vec![1, 3].into_iter().collect();

assert_eq!(heap.len(), 2);

[src]

Checkes if the heap is empty.

Examples

use splay_tree::SplayHeap;
let mut heap = SplayHeap::new();

assert!(heap.is_empty());

heap.push(1);
assert!(!heap.is_empty());

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

Trait Implementations

impl<T: Debug> Debug for SplayHeap<T>
[src]

[src]

Formats the value using the given formatter.

impl<T: Clone> Clone for SplayHeap<T>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<T> Default for SplayHeap<T> where
    T: Ord
[src]

[src]

Returns the "default value" for a type. Read more

impl<T> FromIterator<T> for SplayHeap<T> where
    T: Ord
[src]

[src]

Creates a value from an iterator. Read more

impl<T> IntoIterator for SplayHeap<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<'a, T> IntoIterator for &'a SplayHeap<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<T> Extend<T> for SplayHeap<T> where
    T: Ord
[src]

[src]

Extends a collection with the contents of an iterator. Read more

impl<'a, T> Extend<&'a T> for SplayHeap<T> where
    T: Copy + 'a + Ord
[src]

[src]

Extends a collection with the contents of an iterator. Read more