Skip to content

Commit e41d95f

Browse files
committed
Initial commit
0 parents  commit e41d95f

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

Gemfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gem 'pry'
6+
gem 'pry-byebug'
7+
gem 'rubocop'

Gemfile.lock

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
ast (2.4.2)
5+
byebug (11.1.3)
6+
coderay (1.1.3)
7+
json (2.7.2)
8+
language_server-protocol (3.17.0.3)
9+
method_source (1.1.0)
10+
parallel (1.25.1)
11+
parser (3.3.3.0)
12+
ast (~> 2.4.1)
13+
racc
14+
pry (0.14.2)
15+
coderay (~> 1.1)
16+
method_source (~> 1.0)
17+
pry-byebug (3.10.1)
18+
byebug (~> 11.0)
19+
pry (>= 0.13, < 0.15)
20+
racc (1.8.0)
21+
rainbow (3.1.1)
22+
regexp_parser (2.9.2)
23+
rexml (3.3.0)
24+
strscan
25+
rubocop (1.64.1)
26+
json (~> 2.3)
27+
language_server-protocol (>= 3.17.0)
28+
parallel (~> 1.10)
29+
parser (>= 3.3.0.2)
30+
rainbow (>= 2.2.2, < 4.0)
31+
regexp_parser (>= 1.8, < 3.0)
32+
rexml (>= 3.2.5, < 4.0)
33+
rubocop-ast (>= 1.31.1, < 2.0)
34+
ruby-progressbar (~> 1.7)
35+
unicode-display_width (>= 2.4.0, < 3.0)
36+
rubocop-ast (1.31.3)
37+
parser (>= 3.3.1.0)
38+
ruby-progressbar (1.13.0)
39+
strscan (3.1.0)
40+
unicode-display_width (2.5.0)
41+
42+
PLATFORMS
43+
ruby
44+
x86_64-linux
45+
46+
DEPENDENCIES
47+
pry
48+
pry-byebug
49+
rubocop
50+
51+
BUNDLED WITH
52+
2.5.3

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Binary Search Tree in Ruby
2+
3+
This includes both depth-first and breadth-first approaches to searching a BST.
4+
5+
The following methods are included in this project:
6+
7+
- [ ] `#build_tree(array)` -- takes an array of data and returns a balanced tree of `Node` objects, and returns the level 0 root node.
8+
- [ ] `#pretty_print` -- returns a visually pleasing showcase of the tree.
9+
- [ ] `#insert(value)` -- inserts the value provided into the tree, keeping it balanced.
10+
- [ ] `#delete(value)` -- deletes the value provided if it is in the tree, keeping it balanced afterwards. Returns nil if the value is not found.
11+
- [ ] `#find(value)` -- searches through the tree to find the value provided. Returns nil if the value is not found
12+
- [ ] `#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.
13+
- [ ] `#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.
14+
- [ ] `#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.
15+
- [ ] `#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.
16+
- [ ] `#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.
17+
- [ ] `#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.
18+
- [ ] `#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.
19+
- [ ] `#rebalance` -- rebalances an unbalanced tree.

lib/node.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# frozen_string_literal: true

lib/tree.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# frozen_string_literal: true

main.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# frozen_string_literal: true

0 commit comments

Comments
 (0)