{"id":17495606,"url":"https://github.com/iraikov/chicken-rbtree","last_synced_at":"2026-01-06T03:52:53.822Z","repository":{"id":36690523,"uuid":"40997014","full_name":"iraikov/chicken-rbtree","owner":"iraikov","description":"Red-black trees in Chicken Scheme","archived":false,"fork":false,"pushed_at":"2019-04-29T04:52:24.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-19T17:42:31.070Z","etag":null,"topics":["chicken-scheme","chicken-scheme-eggs","persistent-collections","persistent-data-structure","red-black-tree","red-black-trees","scheme-language"],"latest_commit_sha":null,"homepage":null,"language":"Scheme","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iraikov.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-08-18T20:53:30.000Z","updated_at":"2022-06-06T04:21:07.000Z","dependencies_parsed_at":"2022-09-05T23:51:25.960Z","dependency_job_id":null,"html_url":"https://github.com/iraikov/chicken-rbtree","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Fchicken-rbtree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Fchicken-rbtree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Fchicken-rbtree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Fchicken-rbtree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iraikov","download_url":"https://codeload.github.com/iraikov/chicken-rbtree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245525821,"owners_count":20629832,"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":["chicken-scheme","chicken-scheme-eggs","persistent-collections","persistent-data-structure","red-black-tree","red-black-trees","scheme-language"],"created_at":"2024-10-19T14:14:04.569Z","updated_at":"2026-01-06T03:52:53.786Z","avatar_url":"https://github.com/iraikov.png","language":"Scheme","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chicken-rbtree\n\nSorted dictionary data structures based on red-black trees.\n\n## Usage\n\n(import rb-tree)\n\n## Documentation\n\nThe `rb-tree` library is based on the SML/NJ library implementation\nof red-black trees, which is in turn based on Chris Okasaki's\nimplementation of red-black trees.  The delete function is based on\nthe description in Cormen, Leiserson, and Rivest.\n\nThe present implementation code defines a persistent map class\nthat implements an ordered dictionary mapping of keys to values.\n\nLooking up an arbitrary or the min/max keys, and deleting the min/max\nkeys require no more key comparisons than the depth of the tree, which\nis `O(log n)` where `n` is the total number of keys in the tree.\n\n### Procedures\n\nThe persistent map instance is created by procedure `rb-tree-map`:\n\n\u003cprocedure\u003erb-tree-map:: KEY-COMPARE-PROC -\u003e persistent-map\u003c/procedure\u003e\n\nwhere KEY-COMPARE-PROC is a user-supplied function that takes two keys\nand returns a negative, positive, or zero number depending on how the\nfirst key compares to the second.\n\n`persistent-map` is a yasos class that contains the following operations:\n\n* `empty? TREE` : returns `#t` if the given tree is empty\n* `get TREE` : returns a procedure of the form `(LAMBDA KEY . DEFAULT-CLAUSE` which searches the given tree for an association with a given `KEY`, and returns a (key . value) pair of the found association. If an association with `KEY` cannot be located in the  tree, the procedure returns the result of evaluating the `DEFAULT-CLAUSE`. If the default clause is omitted, an error is signalled. `KEY` must be comparable to the keys in the  tree by a key-compare predicate (which has been specified when the  tree was created)\n* `get-value TREE` : returns a procedure of the form `(LAMBDA KEY . DEFAULT-CLAUSE` which searches the tree for an association with a given `KEY`, and returns the value of (key . value) pair of the found association. If an association with `KEY` cannot be located in the  tree, the procedure returns the result of evaluating the `DEFAULT-CLAUSE`. If the default clause is omitted, an error is signalled. `KEY` must be comparable to the keys in the  tree by a key-compare predicate (which has been specified when the  tree was created)\n* `get-min TREE` : returns a (key . value) pair for an association in the tree with the smallest key. If the  tree is empty, an error is signalled.\n* `get-max TREE` : returns a (key . value) pair for an association in the tree with the largest key. If the tree is empty, an error is signalled.\n* `size TREE` : returns the size (the number of associations) in the tree\n* `put TREE KEY VALUE` : returns a new tree object that contains the given association; if the key was already in the tree, the association will be replaced. \n* `update TREE KEY VALUE MERGE-FN` : returns a new  tree object that contains the given association; if the key was already in the tree, procedure `MERGE-FN` is used to merge the old and new values.\n* `delete TREE KEY . DEFAULT-CLAUSE` : if the specified key is found, it returns a new tree object that no longer contains the association specified by that key, while the original tree object is unmodified. If the key is not found, the procedure returns the result of evaluating `DEFAULT-CLAUSE`\n* `for-each-ascending TREE` : returns a procedure `LAMBDA PROC` that will apply the given procedure PROC to each (key . value) association of the tree, from the one with the smallest key all the way to the one with the max key, in an ascending order of keys. \n* `for-each-descending TREE` : returns a procedure `LAMBDA PROC` that will apply the given procedure `PROC`to each (key . value) association of the tree, in the descending order of keys. \n\n\n## Examples\n\n```scheme\n\n (import rb-tree)\n \n (define (++ x) (fx+ 1 x))\n (define (-- x) (fx- x 1))\n \n (let ((m (rb-tree-map (lambda (x y) (- x y)))))\n      \n      (let* ((compute-assoc (lambda (key) (cons key (++ key))))\n             (min-key -1) (max-key 10)\n             (t (let recur  ((m m) (i min-key))\n                (let ((t1 (put m i (cdr (compute-assoc i)))))\n                  (if (\u003c i max-key) (recur t1 (++ i)) t1))))\n             )\n            \n       (print (get m (++ min-key)))\n \n       (print (get m (-- min-key) 'notfound))\n \n       ;; checking traversing in ascending order\n       (let ((expected-key min-key))\n        ((for-each-ascending m)\n         (lambda (association)\n           (print (equal? association (compute-assoc expected-key)))\n          (set! expected-key (++ expected-key)))))\n  )\n )\n```\n\n## License\n\n\u003e\n\u003e Copyright 2007-2018 Ivan Raikov\n\u003e \n\u003e  This program is free software: you can redistribute it and/or modify\n\u003e  it under the terms of the GNU General Public License as published by\n\u003e  the Free Software Foundation, either version 3 of the License, or (at\n\u003e  your option) any later version.\n\u003e  \n\u003e  This program is distributed in the hope that it will be useful, but\n\u003e  WITHOUT ANY WARRANTY; without even the implied warranty of\n\u003e  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n\u003e  General Public License for more details.\n\u003e \n\u003e  A full copy of the GPL license can be found at\n\u003e  \u003chttp://www.gnu.org/licenses/\u003e.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firaikov%2Fchicken-rbtree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Firaikov%2Fchicken-rbtree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firaikov%2Fchicken-rbtree/lists"}