stella v0.3.0 Heap

Documentation for Heap data structure

Annotations

  • n - number of elements in heap

Link to this section Summary

Functions

Method complexity: O(n) Restores ownership of a whole heap to Heap[parent(i)] >= Heap[i], so build heap max

Return left element of index

Method complexity: O(lgn) Max heapify changes list of elements in max heap where for each elements of list (except root) ownership takes place: Heap[parent(i)] >= Heap[i]

Method complexity: O(lgn) Min heapify changes list of elements in min heap where for each elements of list (except root) ownership takes place: Heap[parent(i)] <= Heap[i]

Create new, empty Heap

Return parent index

Return right element of index

Link to this section Functions

Link to this function

build_heap(heap, type)

Method complexity: O(n) Restores ownership of a whole heap to Heap[parent(i)] >= Heap[i], so build heap max

Examples

iex> Heap.build_heap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7], :max) [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]

iex> Heap.build_heap([16, 4, 9, 14, 7, 10, 3, 2, 8, 1], :max) [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]

iex> Heap.build_heap([16, 4, 9, 14, 7, 10, 3, 2, 8, 1], :min) [1, 2, 3, 8, 4, 10, 9, 14, 16, 7]

Return left element of index

Examples

iex> Heap.left(2) 5

iex> Heap.left(4) 9

Link to this function

max_heapify(heap, index, len \\ nil)

Method complexity: O(lgn) Max heapify changes list of elements in max heap where for each elements of list (except root) ownership takes place: Heap[parent(i)] >= Heap[i]

Examples

iex> Heap.max_heapify([16, 4, 10, 14, 7, 9, 3, 2, 8, 1], 1) [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]

Link to this function

min_heapify(heap, index, len \\ nil)

Method complexity: O(lgn) Min heapify changes list of elements in min heap where for each elements of list (except root) ownership takes place: Heap[parent(i)] <= Heap[i]

Examples

iex> Heap.min_heapify([1, 4, 10, 14, 7, 9, 3, 2, 8, 16], 1) [1, 4, 10, 14, 7, 9, 3, 2, 8, 16]

Create new, empty Heap

Examples

iex> Heap.new() []

Return parent index

Examples

iex> Heap.parent(7) 3

iex> Heap.parent(0) nil

Return right element of index

Examples

iex> Heap.right(2)

6

Link to this function

swap(heap, i, j)