Skip to content

Commit fe03749

Browse files
committed
more examples
1 parent 1c2c91e commit fe03749

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
binary_search (1.0.0)
4+
ruby_binary_search (1.0.0)
55

66
GEM
77
remote: https://rubygems.org/
@@ -106,12 +106,12 @@ PLATFORMS
106106
x86_64-linux
107107

108108
DEPENDENCIES
109-
binary_search!
110109
lefthook (~> 1.7, >= 1.7.4)
111110
pry (~> 0.14.2)
112111
rake (~> 13.0)
113112
rspec (~> 3.0)
114113
rubocop-rails_config (~> 1.16)
114+
ruby_binary_search!
115115

116116
BUNDLED WITH
117117
2.5.14

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,33 @@ puts list.to_a # Output: [2, 3, 3, 4, 5, 5, 5, 6, 7, 9]
5353
puts list.min # Output: 2
5454
puts list.max # Output: 9
5555
```
56+
Custom objects
57+
```ruby
58+
require 'binary_search'
59+
class Person
60+
attr_accessor :name, :age
61+
62+
def initialize(name, age)
63+
@name = name
64+
@age = age
65+
end
66+
67+
def <=>(other)
68+
@age <=> other.age
69+
end
70+
end
71+
72+
73+
list = BinarySearch::List.new([
74+
Person.new('Alice', 25),
75+
Person.new('Bob', 30),
76+
Person.new('Charlie', 20),
77+
Person.new('David', 35)
78+
])
79+
80+
puts list.to_a.map(&:name) # Output: ["Charlie", "Alice", "Bob", "David"]
81+
```
82+
5683
## Why is BinarySearch better than normal search? 🏆
5784

5885
- Speed: For large datasets, binary search is significantly faster than linear search. While a normal array search takes O(n) time, BinarySearch takes only O(log n) time. 🐇

binary_search.gemspec

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
require_relative 'lib/binary_search/version'
44

55
Gem::Specification.new do |spec|
6-
spec.name = 'binary_search'
6+
spec.name = 'ruby_binary_search'
77
spec.version = BinarySearch::VERSION
88
spec.authors = ['sebi']
99
spec.email = ['[email protected]']
1010

11-
spec.summary = 'Binary search list implemented in ruby using red-black tree'
12-
spec.description = 'Binary search list implemented in ruby using red-black tree'
11+
spec.summary = 'Binary search list implemented in ruby using red-black self-balancing tree'
12+
spec.description = 'Binary search list implemented in ruby using red-black self-balancing tree'
1313
spec.homepage = 'https://github.com/sebyx07/binary_search'
1414
spec.license = 'MIT'
1515
spec.required_ruby_version = '>= 3.0.0'

lib/binary_search/red_black_tree.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,20 @@ def insert(key)
4444
parent = nil
4545
while current
4646
parent = current
47-
if key < current.key
47+
comparison = key <=> current.key
48+
case comparison
49+
when -1
4850
current = current.left
49-
elsif key > current.key
51+
when 1
5052
current = current.right
5153
else
5254
# For duplicates, we'll add to the right
5355
current = current.right
5456
end
5557
end
5658
new_node.parent = parent
57-
if key <= parent.key
59+
comparison = key <=> parent.key
60+
if comparison <= 0
5861
parent.left = new_node
5962
else
6063
parent.right = new_node
@@ -106,8 +109,9 @@ def update(old_key, new_key)
106109
def find(key)
107110
current = @root
108111
while current
109-
return current if current.key == key
110-
current = key < current.key ? current.left : current.right
112+
comparison = key <=> current.key
113+
return current if comparison == 0
114+
current = comparison < 0 ? current.left : current.right
111115
end
112116
nil
113117
end

spec/binary_search/example_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'custom classes' do
4+
class Person
5+
attr_accessor :name, :age
6+
7+
def initialize(name, age)
8+
@name = name
9+
@age = age
10+
end
11+
12+
def <=>(other)
13+
@age <=> other.age
14+
end
15+
end
16+
17+
let(:list) { BinarySearch::List.new }
18+
19+
it 'works with custom classes' do
20+
list.push(Person.new('Alice', 20))
21+
list.push(Person.new('Bob', 30))
22+
list.push(Person.new('Charlie', 25))
23+
24+
expect(list.to_a.map(&:name)).to eq(%w[Alice Charlie Bob])
25+
expect(list.to_a.map(&:age)).to eq([20, 25, 30])
26+
end
27+
end

0 commit comments

Comments
 (0)