Struct go_heap_rs::HeapType[][src]

pub struct HeapType<T, E> where
    T: Heap<E>, 
{ pub data: T, // some fields omitted }
Expand description

A heap structure which holds a type which derives from Heap

Fields

data: T

Implementations

Initialized a new heap from a Heap type

Arguments

  • init: The Heap type to be initialized

returns: HeapType<T, E> which is your heap data structure

Examples

use go_heap_rs::{HeapType, MinHeap};

let my_vec = MinHeap(vec![4, 3, 2, 1]); // see min heap implementation in Heap trait
let mut heap = HeapType::new(my_vec);
assert_eq!(heap.peak(), Some(&1));

Pushes an element into heap

Arguments

  • x: The element to push it into heap

Removes the greatest item from the binary heap and returns it, or None if it is empty.

returns E: The first element in list

Removes an element from heap by it’s index in it’s underlying container

Arguments

  • i: The index to remove

returns E: The element which as been removed

Panics

This method might panic (based on implementation of swap) if i is bigger than len()

Examples

use go_heap_rs::{HeapType, MinHeap};

let my_vec = MinHeap(vec![1, 4, 3]);
let mut heap = HeapType::new(my_vec); // [1, 4, 3]
assert_eq!(heap.remove(1), 4);
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), None);

Fix re-establishes the heap ordering after the element at index i has changed its value. Changing the value of the element at index i and then calling Fix is equivalent to, but less expensive than, calling Remove(h, i) followed by a Push of the new value.

Arguments

  • i: The index to fix

Examples

use go_heap_rs::{HeapType, MinHeap};

let my_vec = MinHeap(vec![10, 4, 3]);
let mut heap = HeapType::new(my_vec); // [3, 4, 10]
heap.data.0[1] = 0;
heap.fix(0);
assert_eq!(heap.pop(), Some(0));
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(10));
assert_eq!(heap.pop(), None);

peak will return the top of heap if available

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.