Struct algorithms_rs::heap::Heap

source ·
pub struct Heap<T> { /* private fields */ }
Expand description

Heap

Implementations§

Creating a empty heap

use algorithms_rs::Heap;

let empty_heap = Heap::<i32>::new();

assert_eq!(empty_heap.is_empty(), true);

Creating a heap from an array

use algorithms_rs::Heap;

let empty_heap = Heap::<i32>::from_vector(&vec![1]).unwrap();

assert_eq!(empty_heap.is_empty(), true);

Length of the heap

Determine if the heap is empty

Get the internal data of the heap

Big root heap adjustment Recursive algorithm implementation

Small root heap adjustment Recursive algorithm implementation

Small root heap upward adjustment Non-recursive algorithm implementation

Big root heap upward adjustment Non-recursive algorithm implementation

Small root heap downward adjustment Non-recursive algorithm implementation

Big root heap downward adjustment Non-recursive algorithm implementation

Constructing a big root heap by recursive adjustment algorithm of big root heap

Construction of large root heap by non-recursive adjustment algorithm of large root heap

use algorithms_rs::Heap;

let mut max_heap = Heap::from_vector(&vec![3, 2, 1, 4, 5]).unwrap();

max_heap.build_max_heap_by_shift_up();

assert_eq!(max_heap.inner_vec().to_vec(), vec![5, 4, 2, 3, 1])

Constructing rootlet heap by recursive adjustment algorithm of rootlet heap

Construction of rootlet heap by non-recursive adjustment algorithm of rootlet heap

 use algorithms_rs::Heap;

 let mut min_heap = Heap::from_vector(&vec![3, 2, 1, 4, 5]).unwrap();

 min_heap.build_min_heap_by_siftup();

 assert_eq!(min_heap.inner_vec().to_vec(), vec![1, 2, 3, 4, 5]);

Ascending sort implementation based on recursive implementation of the big root heap

use algorithms_rs::Heap;

let mut max_heap = Heap::from_vector(&vec![5, 3, 7, 9, 10, 23, 45, 23, 12, 23, 0, 12, 32]).unwrap();

max_heap.heap_sort_by_max_heap();

assert_eq!(
   max_heap.inner_vec().to_vec(),
   vec![0, 3, 5, 7, 9, 10, 12, 12, 23, 23, 23, 32, 45]
);

Descending sort implementation based on recursive implementation of small root heap

use algorithms_rs::Heap;

let mut min_heap = Heap::from_vector(&vec![3, 2, 1, 0, 23, 34, 56, 11, 230, 12]).unwrap();

min_heap.heap_sort_by_min_heap();

assert_eq!(min_heap.inner_vec().to_vec(), vec![230, 56, 34, 23, 12, 11, 3, 2, 1, 0]);

Descending sort implementation based on non-recursive implementation of small root heap

 use algorithms_rs::Heap;

 let mut min_heap =
 Heap::from_vector(&vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 13, 14]).unwrap();
 min_heap.dec_sort_with_min_sift();
 assert_eq!(
       min_heap.inner_vec().to_vec(),
        vec![14, 13, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
 );

Non-recursive implementation of ascending sort based on large root heap

 use algorithms_rs::Heap;

 let mut max_heap = Heap::from_vector(&vec![9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 0]).unwrap();

 max_heap.asc_sort_with_max_sift();

 assert_eq!(max_heap.inner_vec().to_vec(), vec![0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9]);

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
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.