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