Heap data structure for Elixir.
Find a file
2021-12-09 17:18:48 +13:00
config chore: set up git_ops and auto-releasing 2020-12-28 20:06:11 +13:00
lib Fixed dialyzer notations 2019-01-18 19:57:19 +01:00
test Modernise Elixir usage and replace espec with exunit. 2017-09-20 14:12:36 +12:00
.formatter.exs Update dependencies and CI config. 2018-09-04 16:52:19 +12:00
.gitignore Modernise Elixir usage and replace espec with exunit. 2017-09-20 14:12:36 +12:00
.gitlab-ci.yml chore: change default branch to main. 2021-12-08 12:14:46 +13:00
.travis.yml Update Inch config and add to inchci. 2017-09-26 13:39:49 +13:00
CHANGELOG.md chore: set up git_ops and auto-releasing 2020-12-28 20:06:11 +13:00
LICENSE Update MIT license file 2019-03-04 15:06:58 +13:00
mix.exs chore(deps): update dependency earmark to v1 2021-12-09 17:18:48 +13:00
mix.lock chore(dev-deps): Update git_ops to 2.4.5. 2021-12-08 12:32:25 +13:00
README.md chore: change default branch to main. 2021-12-08 12:14:46 +13:00
renovate.json chore: update renovate.json 2021-12-09 11:14:53 +13:00

Heap

Build Status Hex.pm Inline docs

A Heap is a very useful data structure, because it sorts, quickly, at insert time.

See also: https://en.wikipedia.org/wiki/Heap_(data_structure)

You can use it for things like:

  • Help with scientific computing
  • Quickly sorting
  • Priority queues

Installation

This package is available in Hex:

  1. Add heap to your list of dependencies in mix.exs:

    def deps do
      [{:heap, "~> 2.0"}]
    end
    
  2. Run mix deps.get

Deprecation warning

If you're upgrading from heap < 2.0 be aware that the direction of the :< and :> atoms passed into Heap.new/1 has changed to make more sense.

Examples

Create a min heap and use it to find the smallest element in a collection:

1..500 |> Enum.shuffle |> Enum.into(Heap.min) |> Heap.root
# => 1

Likewise, for max heaps:

1..500 |> Enum.shuffle |> Enum.into(Heap.max) |> Heap.root
# => 500

A priority queue:

Tuples are compared by their elements in order, so you can push tuples of {priority, term} into a Heap for sorting by priority:

Heap.new
|> Heap.push({4, :jam})
|> Heap.push({1, :milk})
|> Heap.push({2, :eggs})
|> Heap.push({1, :bread})
|> Heap.push({3, :butter})
|> Heap.push({2, :coffee})
|> Enum.map(fn {_, what} -> what end)
# => [:bread, :milk, :coffee, :eggs, :butter, :jam]

The heap can also be constructed with a custom comparator:

Heap.new(&(Date.compare(elem(&1, 0), elem(&2, 0)) == :gt))
|> Heap.push({~D[2017-11-20], :jam})
|> Heap.push({~D[2017-11-21], :milk})
|> Heap.push({~D[2017-10-21], :bread})
|> Heap.push({~D[2017-10-20], :eggs})
|> Enum.map(fn {_, what} -> what end)
# => [:milk, :jam, :bread, :eggs]

To access the root and the rest of the heap in one line use Heap.split/1:

{root, rest} = Heap.split(heap)
{root, rest} == {Heap.root(heap), Heap.pop(heap)}
# => true

Documentation

Full API documentation is available on (hexdocs.pm)[https://hexdocs.pm/heap]

Contributing

  1. Fork it ( https://github.com/jamesotron/heap/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request