Collections.Heap (collections v0.1.0) View Source
Leftist heap implemention in Elixir
See also: Leftist Tree
Time complexity
&peek/2
: O(1)&push/2
: O(logn)&pop/2
: O(logn)&size/1
: O(1)&member?/2
: O(n)&empty?/1
: O(1)
Link to this section Summary
Functions
Test if the heap
is empty
Create an empty max heap
with default comparator &>/2
.
Test if the heap
contains the value
.
Create an empty min heap
with default comparator &</2
.
Create an empty heap with the default comparator &</2
.
Create an empty heap with a specific comparator.
Returns the element at the top of heap
.
Removes the element at the top of the heap
and returns the element and the updated heap.
Push a new element into heap
.
Returns the number of elements in heap
.
Link to this section Types
Specs
data() :: {non_neg_integer(), any(), data(), data()} | nil
Specs
t() :: %Collections.Heap{ comparator: (any(), any() -> boolean()), data: data(), size: non_neg_integer() }
Link to this section Functions
Specs
Test if the heap
is empty
Examples
iex> Collections.Heap.new() |> Collections.Heap.empty?()
true
iex> Collections.Heap.new() |> Collections.Heap.push(10) |> Collections.Heap.empty?()
false
Specs
max() :: t()
Create an empty max heap
with default comparator &>/2
.
A max heap is a heap tree which always has the largest value at the top.
Examples
iex> 1..10
...> |> Enum.shuffle()
...> |> Enum.into(Collections.Heap.max())
...> |> Collections.Heap.peek()
10
Specs
Test if the heap
contains the value
.
Examples
iex> heap = 1..10
...> |> Enum.into(Collections.Heap.new())
...> Collections.Heap.member?(heap, 5)
true
...> Collections.Heap.member?(heap, 20)
false
Specs
min() :: t()
Create an empty min heap
with default comparator &</2
.
A min heap is a heap tree which always has the smallest value at the top.
Examples
iex> 1..10
...> |> Enum.shuffle()
...> |> Enum.into(Collections.Heap.min())
...> |> Collections.Heap.peek()
1
Specs
new() :: t()
Create an empty heap with the default comparator &</2
.
Behaves the same as &Heap.min
Specs
Create an empty heap with a specific comparator.
Examples
iex> 1..10
...> |> Enum.shuffle()
...> |> Enum.into(Collections.Heap.new(&(&1 > &2)))
...> |> Enum.to_list()
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
The given function should compare two arguments, and return true if the first argument precedes the second one.
Specs
Returns the element at the top of heap
.
If the heap
is empty, default
is returned
If default
is not provided, nil is used
Examples
iex> Collections.Heap.new()
...> |> Collections.Heap.peek()
nil
iex> Collections.Heap.new()
...> |> Collections.Heap.peek(10)
10
iex> 1..10
...> |> Enum.shuffle()
...> |> Enum.into(Collections.Heap.new())
...> |> Collections.Heap.peek()
1
Specs
Removes the element at the top of the heap
and returns the element and the updated heap.
If the heap
is empty, default
is returned
If default
is not provided, nil is used
Examples
iex> {nil, _} = Collections.Heap.new()
...> |> Collections.Heap.pop()
iex> {10, _} = Collections.Heap.new()
...> |> Collections.Heap.pop(10)
iex> {1, rest_heap} = 1..10
...> |> Enum.shuffle()
...> |> Enum.into(Collections.Heap.new())
...> |> Collections.Heap.pop()
...> {2, _} = Collections.Heap.pop(rest_heap)
...> Collections.Heap.size(rest_heap)
9
Specs
Push a new element into heap
.
Examples
iex> Collections.Heap.new()
...> |> Collections.Heap.push(10)
...> |> Collections.Heap.peek()
10
Specs
size(t()) :: non_neg_integer()
Returns the number of elements in heap
.
Examples
iex> 1..10
...> |> Enum.into(Collections.Heap.new())
...> |> Collections.Heap.size()
10