stella v0.6.0 Heap

Documentation for Heap data structure

Link to this section Summary

Types

Heap - data structure

Functions

Method complexity: O(n)

Checks if heap is empty

Returns index of left leaf.

Method complexity: O(lgn)

Checks if the passed integer is a member of the heap

Method complexity: O(lgn)

Create new, empty Heap

Returns parent index.

Returns index of right leaf.

Returns the root value of the heap. Assume the passed heap has the max or min heap properties.

Returns current size of heap

Method complexity: O(n)

Link to this section Types

Specs

heap() :: [integer()]

Heap - data structure

Link to this section Functions

Link to this function

build_heap(heap, type)

Specs

build_heap(heap(), atom()) :: heap()

Method complexity: O(n)

Transforms passed heap (list) into specified heap type

  • min: (Heap[parent(i)] <= Heap[i])
  • max: (Heap[parent(i)] >= Heap[i])

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]

Specs

empty?(heap()) :: boolean()

Checks if heap is empty

Examples

iex> Heap.empty?([1, 2, 3])
false

Specs

Returns index of left leaf.

Examples

iex> Heap.left(2)
5

iex> Heap.left(4)
9
Link to this function

max_heapify(heap, index, len \\ nil)

Specs

max_heapify(heap(), integer(), integer() | nil) :: heap()

Method complexity: O(lgn)

Max heapify changes list of elements in max heap. There is property for every element in heap:

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

member?(heap, value)

Specs

member?(heap(), integer()) :: boolean()

Checks if the passed integer is a member of the heap

Examples

iex> Heap.member?([1, 2, 3], 2)
true

iex> Heap.member?([1, 2, 3], 5)
false
Link to this function

min_heapify(heap, index, len \\ nil)

Specs

min_heapify(heap(), integer(), integer() | nil) :: heap()

Method complexity: O(lgn)

Min heapify changes list of elements in max heap. There is property for every element in heap:

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]

Specs

new() :: []

Create new, empty Heap

Examples

iex> Heap.new()
[]

Specs

parent(integer()) :: nil | non_neg_integer()

Returns parent index.

Examples

iex> Heap.parent(7)
3

iex> Heap.parent(0)
nil

Specs

Returns index of right leaf.

Examples

iex> Heap.right(2)
6

Specs

root(heap()) :: integer() | nil

Returns the root value of the heap. Assume the passed heap has the max or min heap properties.

Examples

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

iex> Heap.root([44, 35, 42, 33, 31, 19, 27, 10, 26, 14])
44

iex> Heap.root([3, 8, 10, 15, 10, 12])
3

Specs

size(heap()) :: non_neg_integer()

Returns current size of heap

Examples

iex> Heap.size([1, 2, 3])
3

iex> Heap.size([])
0

Specs

sort(heap()) :: heap()

Method complexity: O(n)

Sorts the heap (from min to max)

Examples

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