{"id":15966673,"url":"https://github.com/libbum/elm-redblacktrees","last_synced_at":"2026-05-11T12:38:00.921Z","repository":{"id":57674626,"uuid":"148495887","full_name":"Libbum/elm-redblacktrees","owner":"Libbum","description":"Red Black self-balancing binary search trees","archived":false,"fork":false,"pushed_at":"2019-05-05T11:44:45.000Z","size":222,"stargazers_count":0,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-08T23:05:03.374Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://package.elm-lang.org/packages/Libbum/elm-redblacktrees/latest/","language":"Elm","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Libbum.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":"2018-09-12T14:45:50.000Z","updated_at":"2023-07-25T14:19:51.000Z","dependencies_parsed_at":"2022-09-19T07:30:16.883Z","dependency_job_id":null,"html_url":"https://github.com/Libbum/elm-redblacktrees","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Libbum%2Felm-redblacktrees","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Libbum%2Felm-redblacktrees/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Libbum%2Felm-redblacktrees/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Libbum%2Felm-redblacktrees/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Libbum","download_url":"https://codeload.github.com/Libbum/elm-redblacktrees/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240822610,"owners_count":19863302,"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","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-10-07T18:03:06.422Z","updated_at":"2026-05-11T12:38:00.884Z","avatar_url":"https://github.com/Libbum.png","language":"Elm","funding_links":[],"categories":[],"sub_categories":[],"readme":"![elm-redblacktrees](https://raw.githubusercontent.com/Libbum/elm-redblacktrees/master/logo/logo.png)\n\n[![Elm Package](https://img.shields.io/elm-package/v/Libbum/elm-redblacktrees.svg)](https://package.elm-lang.org/packages/Libbum/elm-redblacktrees/latest/) │ [![Travis-ci](https://travis-ci.org/Libbum/elm-redblacktrees.svg?branch=master)](https://travis-ci.org/Libbum/elm-redblacktrees) │ [![Fossa Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FLibbum%2Felm-redblacktrees.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FLibbum%2Felm-redblacktrees?ref=badge_shield)\n\n---\n\nRed Black Trees are self-balancing binary search trees (BST) which add one bit of memory to the standard implementation (usually denoted by the colours red and black).\nWith this additional tracking information; the worst case search, insert and delete time complexity drops to O(log N) [from O(N) in the BST case].\n\n\u003e Sounds great and all, but what the hell does that mean?\n\nGenerally speaking, a BST stores your information in a structured (sorted) manner, such that on average an operation can ignore about half of the tree before identifying the correct node to work with.\nThe time taken for this operation is proportional to the logarithm of the number (N) of items in the tree, i.e. log N \u0026mdash; a good deal better than the linear time lookup O(N) for an unsorted list.\nA hashmap on the other hand does even better: on average it only takes O(1) time, meaning it usually can just go to the correct node directly.\n\nNotice though, that these are the *average* cases.\nWhat about the worst case?\nConsider you have a sorted list `[ 1, 2, 3, 4, 5, 6 ]` that you convert to a BST.\nAs each number is inserted, the algorithm will find that it must go to the right child branch of the tree, since it is larger than the previous one.\nIn the end, you get a lopsided tree:\n\n![Wost case BST](https://raw.githubusercontent.com/Libbum/elm-redblacktrees/master/images/sortedbst.png)\n\nNow, if you wish to insert a `7`, you must traverse all of the values in the tree before arriving at the place you need to go.\nThus, your operation is going to take O(N) here, not the average O(log N).\nA hashmap has the same sticking point, so for the most part, they are fast but can get into big trouble sometimes.\n\n\u003e I thought this library was for red black trees. What's with all this binary search and hashmap malarkey?\n\nRight. So red black trees fix the worst case scenario problem with some fancy trickery.\n\n![A red black tree from a sorted list](https://raw.githubusercontent.com/Libbum/elm-redblacktrees/master/images/redblack1to6.png)\n\nThe colours allow the tree to arrange itself such that the height of the tree is minimised, or in other words, the tree remains balanced.\nIt becomes even better once we successfully insert the `7`:\n\n![A balanced red black tree](https://raw.githubusercontent.com/Libbum/elm-redblacktrees/master/images/redblack1to7.png)\n\nAll operations work in O(log N) time, meaning consistent performance across all cases.\nTherefore you should be using a red black tree over a standard binary search tree whenever possible, and whenever your hashmap is giving you grief.\n\n\u003e Hold up, why does this package exist? Isn't this just the poor-man's version of Elm's `Dict` implementation?\n\nWell\u0026hellip; Yes. Would you believe that I only realised this after I'd written the entire codebase and was just finishing off this documentation?\nUsually dictionaries are hashmaps under the hood.\nSeems that Elm's design principles are in line with the 'consistent performance' ideals mentioned above.\n\nSo let me be clear: **you probably want to be using [`Dict`](https://package.elm-lang.org/packages/elm/core/latest/Dict) in your app**.\n\nThe implementation herein is closer to the Elm 0.18 Dict than the 0.19 one, but both of these are a bit difficult to read and understand.\nThis codebase is well documented and as simple as a red black tree can be.\nTherefore, its purpose is perhaps more educational than anything else.\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FLibbum%2Felm-redblacktrees.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FLibbum%2Felm-redblacktrees?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibbum%2Felm-redblacktrees","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flibbum%2Felm-redblacktrees","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibbum%2Felm-redblacktrees/lists"}