Heap (stella v0.7.0)
Documentation for Heap
data structure
Link to this section Summary
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
heap()
@type heap() :: [integer()]
Heap - data structure
Link to this section Functions
build_heap(heap, type)
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]
empty?(heap)
Checks if heap is empty
examples
Examples
iex> Heap.empty?([1, 2, 3])
false
left(index)
@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
max_heapify(heap, index, len \\ nil)
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]
member?(heap, value)
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
min_heapify(heap, index, len \\ nil)
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]
new()
@spec new() :: []
Create new, empty Heap
examples
Examples
iex> Heap.new()
[]
parent(index)
@spec parent(integer()) :: nil | non_neg_integer()
Returns parent index.
examples
Examples
iex> Heap.parent(7)
3
iex> Heap.parent(0)
nil
right(index)
@spec right(non_neg_integer()) :: non_neg_integer()
Returns index of right leaf.
examples
Examples
iex> Heap.right(2)
6
root(heap)
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
size(heap)
@spec size(heap()) :: non_neg_integer()
Returns current size of heap
examples
Examples
iex> Heap.size([1, 2, 3])
3
iex> Heap.size([])
0
sort(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]