Struct Heap

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

Heap

Implementations§

Source§

impl<T: Clone + PartialOrd + Default + Display + Debug> Heap<T>

Source

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

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

pub fn len(&self) -> usize

Length of the heap

Source

pub fn is_empty(&self) -> bool

Determine if the heap is empty

Source

pub fn inner_vec(&self) -> &[T]

Get the internal data of the heap

Source

pub fn max_heapify(&mut self, index: usize)

Big root heap adjustment Recursive algorithm implementation

Source

pub fn min_heapify(&mut self, index: usize)

Small root heap adjustment Recursive algorithm implementation

Source

pub fn min_sift_up(&mut self, index: usize)

Small root heap upward adjustment Non-recursive algorithm implementation

Source

pub fn max_sift_up(&mut self, index: usize)

Big root heap upward adjustment Non-recursive algorithm implementation

Source

pub fn min_sift_down(&mut self, heap_len: usize)

Small root heap downward adjustment Non-recursive algorithm implementation

Source

pub fn max_sift_down(&mut self, heap_len: usize)

Big root heap downward adjustment Non-recursive algorithm implementation

Source

pub fn build_max_heap_by_max_heapify(&mut self)

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

Source

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])
Source

pub fn build_min_heap_by_min_heapify(&mut self)

Constructing rootlet heap by recursive adjustment algorithm of rootlet heap

Source

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

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

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

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

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

Trait Implementations§

Source§

impl<T: Debug> Debug for Heap<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone + PartialOrd + Default + Display + Debug> Default for Heap<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Heap<T>

§

impl<T> RefUnwindSafe for Heap<T>
where T: RefUnwindSafe,

§

impl<T> Send for Heap<T>
where T: Send,

§

impl<T> Sync for Heap<T>
where T: Sync,

§

impl<T> Unpin for Heap<T>
where T: Unpin,

§

impl<T> UnwindSafe for Heap<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.