Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/grayvox/ruby-binary-search
π² Binary Search Trees in Ruby
https://github.com/grayvox/ruby-binary-search
computer-science ruby theodinproject
Last synced: about 2 months ago
JSON representation
π² Binary Search Trees in Ruby
- Host: GitHub
- URL: https://github.com/grayvox/ruby-binary-search
- Owner: Grayvox
- Created: 2024-06-27T13:57:17.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-07-07T20:03:50.000Z (7 months ago)
- Last Synced: 2024-07-08T19:51:48.863Z (7 months ago)
- Topics: computer-science, ruby, theodinproject
- Language: Ruby
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Binary Search Tree in Ruby
This includes both depth-first and breadth-first approaches to searching a BST.
The following methods are included in this project:
- [x] `#build_tree(array)` -- takes an array of data and returns a balanced tree of `Node` objects, and returns the level 0 root node.
- [x] `#pretty_print` -- returns a visually pleasing showcase of the tree.
- [x] `#insert(value)` -- inserts the value provided into the tree, keeping it balanced.
- [x] `#delete(value)` -- deletes the value provided if it is in the tree, keeping it balanced afterwards. Returns nil if the value is not found.
- [x] `#find(value)` -- searches through the tree to find the value provided. Returns nil if the value is not found
- [x] `#level_order(&block)` -- traverses the tree in breadth-first level order and yields each node to the provided block. Returns an array of values if no block is given. Also uses an array acting as a queue to keep track of all the child nodes that it has yet to traverse and will add new ones to the list when needed. This version uses recursion, while `#level_order_iteration(&block)` uses iteration.
- [x] `#inorder(&block)` -- traverses the tree in depth-first using inorder traversal and yields each node to the provided block. Returns an array of values if no block is given.
- [x] `#preorder(&block)` -- traverses the tree in depth-first using preorder traversal and yields each node to the provided block. Returns an array of values if no block is given.
- [x] `#postorder(&block)` -- traverses the tree in depth-first using postorder traversal and yields each node to the provided block. Returns an array of values if no block is given.
- [x] `#height(value)` -- accepts the value of a node on the tree and returns its height. Height is defined as the number of edges in longest path from a given node to a leaf node. Returns nil if the value isn't found on the tree.
- [ ] `#depth(value)` -- accepts the value of a node on the tree and returns its depth. Depth is defined as the number of edges in path from a given node to the treeβs root node. Returns nil if the value isn't found on the tree.
- [x] `#balanced?` -- checks if the tree is balanced. A balanced tree is one where the difference between heights of left subtree and right subtree of every node is not more than 1. Returns true or fals.
- [x] `#rebalance` -- rebalances an unbalanced tree.