ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - tyre/red_black_tree: Red-black tree implementation for Elixir.
Red-black tree implementation for Elixir. Contribute to tyre/red_black_tree development by creating an account on GitHub.
Visit Site

GitHub - tyre/red_black_tree: Red-black tree implementation for Elixir.

GitHub - tyre/red_black_tree: Red-black tree implementation for Elixir.

RedBlackTree

Hex.pm Travis

Red-black tree implementation for Elixir.

Install

Add the following to your mix.exs deps:

{:red_black_tree, "~> 1.0"}

About

Provides an ordered key-value store with O(log(N)) lookup, insert, and delete performance and O(1) size performance.

Implements the Dict behavior, Enumerable protocol, and the Collectable protocol.

Comparison

By default, keys are compared using strict equality (see note below), allowing for polymorphic keys in the same tree:

RedBlackTree.new()
|> RedBlackTree.insert(:a, 1)
|> RedBlackTree.insert({:compound, :key}, 2)

A custom comparator may be provided at initialization via the :comparator option.

For example, let's say we want to store maps containing order information, sorted by the revenue generated and unique by id. We'll use the RedBlackTree.compare_terms function for comparisions since it takes care of weird cases (see note below.)

order_revenue = RedBlackTree.new([], comparator: fn (value1, value2) ->
  # If the ids are the same, they are the same
  if value1.id === value2.id do
    0
  else
    case RedBlackTree.compare_terms(value1.revenue, value2.revenue) do
      # If the revenues are the same but the ids are different, fall back to id comparison for ordering
      0 -> RedBlackTree.compare_terms(value1.id, value2.id)
      # otherwise return the comparison
      revenue_comparison -> revenue_comparison
    end
  end
end)

updated_tree = order_revenue
  |> RedBlackTree.insert(%{id: 3, revenue: 40}, 40)
  |> RedBlackTree.insert(%{id: 50, revenue: 10}, 10)
  |> RedBlackTree.insert(%{id: 1, revenue: 50}, 50)
  |> RedBlackTree.insert(%{id: 2, revenue: 40}, 40)
# => #RedBlackTree<[{%{id: 50, revenue: 10}, 10}, {%{id: 2, revenue: 40}, 40},
 {%{id: 3, revenue: 40}, 40}, {%{id: 1, revenue: 50}, 50}]>

# Notice how changing the revenue of order 2 bumps it all the way to the end,
# since its revenue now equals order 1 but it loses the tie-breaker

RedBlackTree.insert(updated_tree, %{id: 2, revenue: 50}, 50)
# #RedBlackTree<[{%{id: 50, revenue: 10}, 10}, {%{id: 2, revenue: 40}, 40},
 {%{id: 3, revenue: 40}, 40}, {%{id: 1, revenue: 50}, 50},
 {%{id: 2, revenue: 50}, 50}]>

Note

Due to the way Erlang, and therefore Elixir, implement comparisons for floats and integers, it is possible for a two keys to be equal (key == other_key) but not strictly equal (key !== other_key).

To guarantee consistent ordering, the default :comparator function must fallback to hashing keys that exhibit this property on comparison. In these rare cases, there will be a small performance penalty.

Example:

tree = RedBlackTree.new([1 => :bubbles])

# Hashing necessary since 1 != 1.0 and 1 == 1.0
updated = RedBlackTree.insert(tree, 1.0, :walrus)

# No hashing necessary, no performance impact
RedBlackTree.insert(updated, 0.5, :frank)
|> RedBlackTree.insert(1.5, :suzie)

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory