{"id":13507327,"url":"https://github.com/tyre/red_black_tree","last_synced_at":"2025-12-12T00:24:43.848Z","repository":{"id":33384943,"uuid":"37029925","full_name":"tyre/red_black_tree","owner":"tyre","description":"Red-black tree implementation for Elixir.","archived":false,"fork":false,"pushed_at":"2018-07-11T13:29:52.000Z","size":21,"stargazers_count":37,"open_issues_count":7,"forks_count":14,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-21T18:53:50.475Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tyre.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-07T19:51:13.000Z","updated_at":"2024-11-26T14:34:10.000Z","dependencies_parsed_at":"2022-08-07T21:00:53.158Z","dependency_job_id":null,"html_url":"https://github.com/tyre/red_black_tree","commit_stats":null,"previous_names":["senecasystems/red_black_tree"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/tyre/red_black_tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tyre%2Fred_black_tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tyre%2Fred_black_tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tyre%2Fred_black_tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tyre%2Fred_black_tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tyre","download_url":"https://codeload.github.com/tyre/red_black_tree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tyre%2Fred_black_tree/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280317282,"owners_count":26309997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T02:00:31.254Z","updated_at":"2025-10-21T18:54:16.898Z","avatar_url":"https://github.com/tyre.png","language":"Elixir","funding_links":[],"categories":["Algorithms and Data structures"],"sub_categories":[],"readme":"# RedBlackTree\n\n[![Hex.pm](https://img.shields.io/hexpm/v/red_black_tree.svg)](https://hex.pm/packages/red_black_tree) [![Travis](https://img.shields.io/travis/SenecaSystems/red_black_tree.svg)](https://travis-ci.org/SenecaSystems/red_black_tree)\n\nRed-black tree implementation for Elixir.\n\n## Install\n\nAdd the following to your mix.exs deps:\n\n`{:red_black_tree, \"~\u003e 1.0\"}`\n\n## About\n\nProvides an ordered key-value store with `O(log(N))` lookup, insert, and delete\nperformance and `O(1)` size performance.\n\nImplements the [Dict](http://elixir-lang.org/docs/stable/elixir/Dict.html)\nbehavior, [Enumerable](http://elixir-lang.org/docs/stable/elixir/Enumerable.html)\nprotocol, and the [Collectable](http://elixir-lang.org/docs/stable/elixir/Collectable.html)\nprotocol.\n\n### Comparison\n\nBy default, keys are compared using strict equality (see note below), allowing for polymorphic keys in the same tree:\n\n```elixir\nRedBlackTree.new()\n|\u003e RedBlackTree.insert(:a, 1)\n|\u003e RedBlackTree.insert({:compound, :key}, 2)\n```\n\nA custom comparator may be provided at initialization via the `:comparator`\noption.\n\nFor example, let's say we want to store maps containing order information,\nsorted by the revenue generated and unique by id. We'll use the\n`RedBlackTree.compare_terms` function for comparisions since it takes care of\nweird cases (see note below.)\n\n\n```elixir\norder_revenue = RedBlackTree.new([], comparator: fn (value1, value2) -\u003e\n  # If the ids are the same, they are the same\n  if value1.id === value2.id do\n    0\n  else\n    case RedBlackTree.compare_terms(value1.revenue, value2.revenue) do\n      # If the revenues are the same but the ids are different, fall back to id comparison for ordering\n      0 -\u003e RedBlackTree.compare_terms(value1.id, value2.id)\n      # otherwise return the comparison\n      revenue_comparison -\u003e revenue_comparison\n    end\n  end\nend)\n\nupdated_tree = order_revenue\n  |\u003e RedBlackTree.insert(%{id: 3, revenue: 40}, 40)\n  |\u003e RedBlackTree.insert(%{id: 50, revenue: 10}, 10)\n  |\u003e RedBlackTree.insert(%{id: 1, revenue: 50}, 50)\n  |\u003e RedBlackTree.insert(%{id: 2, revenue: 40}, 40)\n# =\u003e #RedBlackTree\u003c[{%{id: 50, revenue: 10}, 10}, {%{id: 2, revenue: 40}, 40},\n {%{id: 3, revenue: 40}, 40}, {%{id: 1, revenue: 50}, 50}]\u003e\n\n# Notice how changing the revenue of order 2 bumps it all the way to the end,\n# since its revenue now equals order 1 but it loses the tie-breaker\n\nRedBlackTree.insert(updated_tree, %{id: 2, revenue: 50}, 50)\n# #RedBlackTree\u003c[{%{id: 50, revenue: 10}, 10}, {%{id: 2, revenue: 40}, 40},\n {%{id: 3, revenue: 40}, 40}, {%{id: 1, revenue: 50}, 50},\n {%{id: 2, revenue: 50}, 50}]\u003e\n```\n\n**Note**\n\nDue to the way Erlang, and therefore Elixir, implement comparisons for floats\nand integers, it is possible for a two keys to be equal (`key == other_key`)\nbut not strictly equal (`key !== other_key`).\n\nTo guarantee consistent ordering, the default `:comparator` function must\nfallback to hashing keys that exhibit this property on comparison. In these rare\ncases, there will be a small performance penalty.\n\nExample:\n\n```elixir\ntree = RedBlackTree.new([1 =\u003e :bubbles])\n\n# Hashing necessary since 1 != 1.0 and 1 == 1.0\nupdated = RedBlackTree.insert(tree, 1.0, :walrus)\n\n# No hashing necessary, no performance impact\nRedBlackTree.insert(updated, 0.5, :frank)\n|\u003e RedBlackTree.insert(1.5, :suzie)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftyre%2Fred_black_tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftyre%2Fred_black_tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftyre%2Fred_black_tree/lists"}