Skip to content

Commit

Permalink
ruby/binary-search-tree: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Nov 1, 2023
1 parent e8139dc commit f5ec234
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Exercism Workspace
| July | Jurassic | C (5/5), C++ (5/5), Fortran (0/5) |
| August | Apps | Dart (5/5), Java (0/5), Kotlin (0/5) |
| September | Slimline | Awk (5/5), Bash\* (5/5), jq (0/5), Perl (0/5) |
| October | Object-Oriented | Ruby (3/5), Java (0/5) |
| October | Object-Oriented | Ruby (4/5), Java (0/5) |
| November | Nibbly | |
| December | Diversions | |

Expand Down
1 change: 1 addition & 0 deletions ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@
- [simple-cipher](./simple-cipher/README.md)
- [clock](./clock/README.md)
- [circular-buffer](./circular-buffer/README.md)
- [binary-search-tree](./binary-search-tree/README.md)
7 changes: 6 additions & 1 deletion ruby/binary-search-tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ And if we then added 1, 5, and 7, it would look like this

### Based on

Josh Cheek - https://twitter.com/josh_cheek
Josh Cheek - https://twitter.com/josh_cheek

### My Solution

- [my solution](./binary_search_tree.rb)
- [run-tests](./run-tests-ruby.txt)
34 changes: 28 additions & 6 deletions ruby/binary-search-tree/binary_search_tree.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
=begin
Write your code for the 'Binary Search Tree' exercise in this file. Make the tests in
`binary_search_tree_test.rb` pass.
# frozen_string_literal: false

To get started with TDD, see the `README.md` file in your
`ruby/binary-search-tree` directory.
=end
# https://exercism.org/tracks/ruby/exercises/binary-search-tree
# Binary Search Tree exercise
class Bst
attr_reader :data, :left, :right

def initialize(data = nil)
@data = data
@left = nil
@right = nil
end

def insert(data)
if data <= @data
@left ? @left.insert(data) : @left = Bst.new(data)
else
@right ? @right.insert(data) : @right = Bst.new(data)
end
end

def each(&block)
return enum_for(:each) unless block_given?

@left&.each(&block)
block.call(@data)
@right&.each(&block)
end
end
42 changes: 33 additions & 9 deletions ruby/binary-search-tree/binary_search_tree_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# frozen_string_literal: false

# https://github.com/simplecov-ruby/simplecov
require 'simplecov'

# https://about.codecov.io/blog/getting-started-with-code-coverage-for-ruby/
require 'simplecov-cobertura'
SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter

# line coverage
SimpleCov.start if ENV['COVERAGE'] != 'branch'

# branch coverage
if ENV['COVERAGE'] == 'branch'
SimpleCov.start do
enable_coverage :branch
primary_coverage :branch
end
end

# name the test file/group
SimpleCov.command_name 'test:exercism'

# original exercism tests
require 'minitest/autorun'
require_relative 'binary_search_tree'

Expand All @@ -7,31 +31,31 @@ def test_data_is_retained
end

def test_inserting_less
skip
# skip
four = Bst.new 4
four.insert 2
assert_equal 4, four.data
assert_equal 2, four.left.data
end

def test_inserting_same
skip
# skip
four = Bst.new 4
four.insert 4
assert_equal 4, four.data
assert_equal 4, four.left.data
end

def test_inserting_right
skip
# skip
four = Bst.new 4
four.insert 5
assert_equal 4, four.data
assert_equal 5, four.right.data
end

def test_complex_tree
skip
# skip
four = Bst.new 4
four.insert 2
four.insert 6
Expand All @@ -55,26 +79,26 @@ def record_all_data(bst)
end

def test_iterating_one_element
skip
# skip
assert_equal [4], record_all_data(Bst.new(4))
end

def test_iterating_over_smaller_element
skip
# skip
four = Bst.new 4
four.insert 2
assert_equal [2, 4], record_all_data(four)
end

def test_iterating_over_larger_element
skip
# skip
four = Bst.new 4
four.insert 5
assert_equal [4, 5], record_all_data(four)
end

def test_iterating_over_complex_tree
skip
# skip
four = Bst.new 4
four.insert 2
four.insert 1
Expand All @@ -86,7 +110,7 @@ def test_iterating_over_complex_tree
end

def test_each_returns_enumerator_if_no_block
skip
# skip

tree = Bst.new 4
[2, 1, 3, 6, 7, 5].each { |x| tree.insert x }
Expand Down
5 changes: 5 additions & 0 deletions ruby/binary-search-tree/coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"result": {
"line": 100.0
}
}
40 changes: 40 additions & 0 deletions ruby/binary-search-tree/coverage/.resultset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"test:exercism": {
"coverage": {
"/home/vpayno/git_vpayno/exercism-workspace/ruby/binary-search-tree/binary_search_tree.rb": {
"lines": [
null,
null,
null,
null,
1,
1,
null,
1,
33,
33,
33,
null,
null,
1,
35,
18,
null,
17,
null,
null,
null,
1,
20,
null,
19,
19,
19,
null,
null
]
}
},
"timestamp": 1698805764
}
}
Empty file.
34 changes: 34 additions & 0 deletions ruby/binary-search-tree/coverage/coverage.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version='1.0'?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<!-- Generated by simplecov-cobertura version 2.1.0 (https://github.com/dashingrocket/simplecov-cobertura) -->
<coverage line-rate="1.0" lines-covered="15" lines-valid="15" complexity="0" version="0" timestamp="1698805764">
<sources>
<source>/home/vpayno/git_vpayno/exercism-workspace/ruby/binary-search-tree</source>
</sources>
<packages>
<package name="binary-search-tree" line-rate="1.0" complexity="0">
<classes>
<class name="binary_search_tree" filename="binary_search_tree.rb" line-rate="1.0" complexity="0">
<methods/>
<lines>
<line number="5" hits="1"/>
<line number="6" hits="1"/>
<line number="8" hits="1"/>
<line number="9" hits="33"/>
<line number="10" hits="33"/>
<line number="11" hits="33"/>
<line number="14" hits="1"/>
<line number="15" hits="35"/>
<line number="16" hits="18"/>
<line number="18" hits="17"/>
<line number="22" hits="1"/>
<line number="23" hits="20"/>
<line number="25" hits="19"/>
<line number="26" hits="19"/>
<line number="27" hits="19"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
Loading

0 comments on commit f5ec234

Please sign in to comment.