lheap v1.0.0 LHeap
Leftist heaps in Elixir.
For general information about them, see Wikipedia.
Leftist heap elements are represented by a three element tuple of the form:
{{s-value, value}, left, right}
Where:
s-valueis the distance from that node to the nearest leaf of the extended binary tree representationvalue, is the actual value of the nodeleftandrightare other heaps
Summary
Functions
Merges two heaps
Returns the minimum element of a heap
Creates a new, empty heap
Creates a new heap from a given enumerable
Puts a new value in a heap
Removes the minimum element from a heap
Sorts the given heap and returns a list
Functions
Merges two heaps.
Example
iex> heap1 = LHeap.new([2, 4, 6])
iex> heap2 = LHeap.new([1, 3, 5])
iex> LHeap.merge(heap1, heap2)
{{2, 1}, {{2, 2}, {{1, 4}, {}, {}}, {{1, 5}, {{1, 6}, {}, {}}, {}}},
{{1, 3}, {}, {}}}
Creates a new heap from a given enumerable.
Example
iex> LHeap.new([4, 3, 8])
{{2, 3}, {{1, 4}, {}, {}}, {{1, 8}, {}, {}}}
Puts a new value in a heap.
Example
iex> LHeap.new() |> LHeap.put(10) |> LHeap.put(1) |> LHeap.put(5) |> LHeap.put(7)
{{2, 1}, {{1, 10}, {}, {}}, {{1, 5}, {{1, 7}, {}, {}}, {}}}