{"id":16962701,"url":"https://github.com/sgoudham/bst-rs","last_synced_at":"2025-04-11T21:31:12.072Z","repository":{"id":42057120,"uuid":"460237229","full_name":"sgoudham/bst-rs","owner":"sgoudham","description":"Recursive \u0026 Iterative Binary Search Tree Implementations within Rust","archived":false,"fork":false,"pushed_at":"2022-10-01T01:43:36.000Z","size":121,"stargazers_count":8,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T11:09:15.521Z","etag":null,"topics":["binary-search-tree","binary-tree","bst","bst-tree","data-structures","hacktoberfest","rust","rust-crate","rust-library"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/bst-rs","language":"Rust","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/sgoudham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-17T01:22:28.000Z","updated_at":"2024-12-26T03:18:23.000Z","dependencies_parsed_at":"2022-08-12T03:31:57.695Z","dependency_job_id":null,"html_url":"https://github.com/sgoudham/bst-rs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgoudham%2Fbst-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgoudham%2Fbst-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgoudham%2Fbst-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgoudham%2Fbst-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sgoudham","download_url":"https://codeload.github.com/sgoudham/bst-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248482923,"owners_count":21111401,"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":["binary-search-tree","binary-tree","bst","bst-tree","data-structures","hacktoberfest","rust","rust-crate","rust-library"],"created_at":"2024-10-13T23:07:38.056Z","updated_at":"2025-04-11T21:31:11.732Z","avatar_url":"https://github.com/sgoudham.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bst-rs\n\n[![build](https://github.com/sgoudham/bst-rs/actions/workflows/build.yml/badge.svg)](https://github.com/sgoudham/bst-rs/actions/workflows/build.yml)\n[![crate.io](https://img.shields.io/crates/v/bst-rs)](https://crates.io/crates/bst-rs)\n[![downloads](https://img.shields.io/crates/d/bst-rs)](https://crates.io/crates/bst-rs)\n[![license](https://img.shields.io/github/license/sgoudham/bst-rs)](LICENSE)\n\n\u003e Recursive \u0026 Iterative Binary Search Tree Implementations within Rust\n\n## Table of Contents\n\n- [About](#About)\n- [Personal Goals](#Personal-Goals)\n- [Quick Start](#Quick-Start)\n- [License](#License)\n- [Contributing](#Contributing)\n- [Inspiration](#Inspiration)\n\n## About\n\nThis crate contains Recursive \u0026 Iterative Binary Search Tree Implementations. All common operations are included along\nwith common traversal iterators.\n\nAll elements within the Binary Search Trees _must_ implement\nthe [Ord](https://doc.rust-lang.org/core/cmp/trait.Ord.html) trait.\n\nIt is also important to note that [RecursiveBST](src/lib.rs) is more likely to `blow the stack.`\nFor more information on why that is the case, please have a look at\n[The Story of Tail Call Optimizations in Rust.](https://seanchen1991.github.io/posts/tco-story/)\n\n## Personal Goals\n\nI have made this library with the personal goals of learning and solidifying concepts such as `ownership`, `borrowing`\n, `generics` and `lifetimes`. I cannot promise that the implementations are particularly efficient, or if they are, it\nwas not at the forefront of my mind.\n\nThat being said, there are some areas I would love to improve upon which include:\n\n- [ ] Write idiomatic code.\n- [ ] Effectively use **macro_rules!** to reduce large portions of repetitive code.\n- [ ] Implement a **pretty_print()** function to display the binary search trees nicely.\n- [ ] Implementing the Drop trait for iterative node cleanup.\n- [ ] Pre-allocating space on the heap for nodes to reduce inefficiency of inserts.\n\nI'm more than happy to accept (and encourage) contributions if anyone is kind enough to do so. (Please look\nat [CONTRIBUTING!](#Contributing))\n\n## Quick Start\n\n ```rust\nuse bst_rs::{BinarySearchTree, IterativeBST, RecursiveBST};\n\n// Create new empty binary search trees\nlet mut iterative_bst = IterativeBST::new();\nassert!(iterative_bst.is_empty());\n\nlet mut recursive_bst = RecursiveBST::new();\nassert!(recursive_bst.is_empty());\n\n// Insert elements (no duplicates are allowed)\niterative_bst.insert(10);\niterative_bst.insert(10);   // Element is not inserted\niterative_bst.insert(5);\niterative_bst.insert(2);\niterative_bst.insert(15);\niterative_bst.insert(25);\nassert_eq!(iterative_bst.size(), 5);\n\nrecursive_bst.insert(10);\nrecursive_bst.insert(10);   // Element is not inserted\nrecursive_bst.insert(5);\nrecursive_bst.insert(2);\nrecursive_bst.insert(15);\nrecursive_bst.insert(25);\nassert_eq!(recursive_bst.size(), 5);\n\n// Check if element exists\nassert!(iterative_bst.contains(\u00265));    // true\nassert!(!iterative_bst.contains(\u00260));   // false\n\nassert!(recursive_bst.contains(\u00265));    // true\nassert!(!recursive_bst.contains(\u00260));   // false\n\n// Remove elements\niterative_bst.remove(\u002610);\niterative_bst.remove(\u002650); // No change to tree as element does not exist\nassert_eq!(iterative_bst.size(), 4);\n\nrecursive_bst.remove(\u002610);\nrecursive_bst.remove(\u002650); // No change to tree as element does not exist\nassert_eq!(recursive_bst.size(), 4);\n\n// Get height of tree\nassert_eq!(iterative_bst.height(), Some(2));\nassert_eq!(recursive_bst.height(), Some(2));\n\n// Get minimum element of tree\nassert_eq!(iterative_bst.min(), Some(\u00262));\nassert_eq!(recursive_bst.min(), Some(\u00262));\n\n// Get maximum element of tree\nassert_eq!(iterative_bst.max(), Some(\u002625));\nassert_eq!(recursive_bst.max(), Some(\u002625));\n\n// Retrieve reference to element in tree\nassert_eq!(iterative_bst.retrieve(\u00265), Some(\u00265));\nassert_eq!(iterative_bst.retrieve(\u0026100), None); // Element does not exist so None is returned\nassert_eq!(recursive_bst.retrieve(\u00265), Some(\u00265));\nassert_eq!(recursive_bst.retrieve(\u0026100), None); // Element does not exist so None is returned\n\n// View pre-order, in-order, post-order and level-order traversals\nassert_eq!(iterative_bst.pre_order_vec(), vec![\u002615, \u00265, \u00262, \u002625]);\nassert_eq!(iterative_bst.in_order_vec(), vec![\u00262, \u00265, \u002615, \u002625]);\nassert_eq!(iterative_bst.post_order_vec(), vec![\u00262, \u00265, \u002625, \u002615]);\nassert_eq!(iterative_bst.level_order_vec(), vec![\u002615, \u00265, \u002625, \u00262]);\n\nassert_eq!(recursive_bst.pre_order_vec(), vec![\u002615, \u00265, \u00262, \u002625]);\nassert_eq!(recursive_bst.in_order_vec(), vec![\u00262, \u00265, \u002615, \u002625]);\nassert_eq!(recursive_bst.post_order_vec(), vec![\u00262, \u00265, \u002625, \u002615]);\nassert_eq!(recursive_bst.level_order_vec(), vec![\u002615, \u00265, \u002625, \u00262]);\n\n// Compare equality/in-equality of trees\nassert_eq!(iterative_bst.asc_order_vec(), recursive_bst.asc_order_vec());\nassert_ne!(iterative_bst, IterativeBST::new());\nassert_ne!(recursive_bst, RecursiveBST::new());\n ```\n\n## License\n\n[MIT License](LICENSE)\n\n## Contributing\n\nPlease read the [CONTRIBUTING.md](CONTRIBUTING.md) before contributing! (Thank you!)\n\n## Inspiration\n\nThe book [Learn Rust With Entirely Too Many Linked Lists](https://rust-unofficial.github.io/too-many-lists/) inspired me\nto try and implement Binary Search Trees within the language. I had also been wanting to create my first library for\nother crates to use.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgoudham%2Fbst-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsgoudham%2Fbst-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgoudham%2Fbst-rs/lists"}