pub struct Heap<T> { /* private fields */ }
Expand description
Heap
Implementations§
Source§impl<T: Clone + PartialOrd + Default + Display + Debug> Heap<T>
impl<T: Clone + PartialOrd + Default + Display + Debug> Heap<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creating a empty heap
use algorithms_rs::Heap;
let empty_heap = Heap::<i32>::new();
assert_eq!(empty_heap.is_empty(), true);
Sourcepub fn from_vector(array: &[T]) -> Result<Self>
pub fn from_vector(array: &[T]) -> Result<Self>
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);
Sourcepub fn max_heapify(&mut self, index: usize)
pub fn max_heapify(&mut self, index: usize)
Big root heap adjustment Recursive algorithm implementation
Sourcepub fn min_heapify(&mut self, index: usize)
pub fn min_heapify(&mut self, index: usize)
Small root heap adjustment Recursive algorithm implementation
Sourcepub fn min_sift_up(&mut self, index: usize)
pub fn min_sift_up(&mut self, index: usize)
Small root heap upward adjustment Non-recursive algorithm implementation
Sourcepub fn max_sift_up(&mut self, index: usize)
pub fn max_sift_up(&mut self, index: usize)
Big root heap upward adjustment Non-recursive algorithm implementation
Sourcepub fn min_sift_down(&mut self, heap_len: usize)
pub fn min_sift_down(&mut self, heap_len: usize)
Small root heap downward adjustment Non-recursive algorithm implementation
Sourcepub fn max_sift_down(&mut self, heap_len: usize)
pub fn max_sift_down(&mut self, heap_len: usize)
Big root heap downward adjustment Non-recursive algorithm implementation
Sourcepub fn build_max_heap_by_max_heapify(&mut self)
pub fn build_max_heap_by_max_heapify(&mut self)
Constructing a big root heap by recursive adjustment algorithm of big root heap
Sourcepub fn build_max_heap_by_shift_up(&mut self)
pub fn build_max_heap_by_shift_up(&mut self)
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])
Sourcepub fn build_min_heap_by_min_heapify(&mut self)
pub fn build_min_heap_by_min_heapify(&mut self)
Constructing rootlet heap by recursive adjustment algorithm of rootlet heap
Sourcepub fn build_min_heap_by_siftup(&mut self)
pub fn build_min_heap_by_siftup(&mut self)
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]);
Sourcepub fn heap_sort_by_max_heap(&mut self)
pub fn heap_sort_by_max_heap(&mut self)
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]
);
Sourcepub fn heap_sort_by_min_heap(&mut self)
pub fn heap_sort_by_min_heap(&mut self)
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]);
Sourcepub fn dec_sort_with_min_sift(&mut self)
pub fn dec_sort_with_min_sift(&mut self)
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]
);
Sourcepub fn asc_sort_with_max_sift(&mut self)
pub fn asc_sort_with_max_sift(&mut self)
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]);