Heap (stella v0.7.0)

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

@type heap() :: [integer()]

Heap - data structure

Link to this section Functions

Link to this function

build_heap(heap, type)

@spec 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

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]
@spec empty?(heap()) :: boolean()

Checks if heap is empty

examples

Examples

iex> Heap.empty?([1, 2, 3])
false
@spec left(non_neg_integer()) :: non_neg_integer()

Returns index of left leaf.

examples

Examples

iex> Heap.left(2)
5

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

max_heapify(heap, index, len \\ nil)

@spec 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

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)

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

Checks if the passed integer is a member of the heap

examples

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)

@spec 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

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]
@spec new() :: []

Create new, empty Heap

examples

Examples

iex> Heap.new()
[]
@spec parent(integer()) :: nil | non_neg_integer()

Returns parent index.

examples

Examples

iex> Heap.parent(7)
3

iex> Heap.parent(0)
nil
@spec right(non_neg_integer()) :: non_neg_integer()

Returns index of right leaf.

examples

Examples

iex> Heap.right(2)
6
@spec root(heap()) :: integer() | nil

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

examples

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
@spec size(heap()) :: non_neg_integer()

Returns current size of heap

examples

Examples

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

iex> Heap.size([])
0
@spec sort(heap()) :: heap()

Method complexity: O(n)

Sorts the heap (from min to max)

examples

Examples

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