forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree.rb
215 lines (176 loc) · 5.48 KB
/
binary_search_tree.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# frozen_string_literal: true
class BinarySearchTree
class Node
attr_accessor :left, :right, :value
def initialize(value)
@left = nil
@right = nil
@value = value
end
end
def initialize
@root = nil
end
attr_reader :root
def insert(value)
new_node = Node.new(value)
if @root.nil?
@root = new_node
return self
end
current_node = @root
loop do
if value < current_node.value
if current_node.left.nil?
current_node.left = new_node
return self
end
current_node = current_node.left
else
if current_node.right.nil?
current_node.right = new_node
return self
end
current_node = current_node.right
end
end
self
end
def lookup(value)
return nil if @root.nil?
current_node = @root
until current_node.nil?
if value < current_node.value
current_node = current_node.left
elsif value > current_node.value
current_node = current_node.right
else
return current_node
end
end
nil
end
def remove(value)
return false if @root.nil?
current_node = @root
parent_node = nil
while current_node
if value < current_node.value
parent_node = current_node
current_node = current_node.left
elsif value > current_node.value
parent_node = current_node
current_node = current_node.right
elsif current_node.value == value
# We have a match, get to work!
if current_node.right.nil?
update_parent_link(parent_node, current_node, current_node.left)
elsif current_node.right.left.nil?
current_node.right.left = current_node.left
update_parent_link(parent_node, current_node, current_node.right)
else
leftmost, leftmost_parent = find_leftmost(current_node.right)
leftmost_parent.left = leftmost.right
leftmost.left = current_node.left
leftmost.right = current_node.right
update_parent_link(parent_node, current_node, leftmost)
end
return true
end
end
false
end
def traverse(node = @root)
return nil if node.nil?
tree = { value: node.value }
tree[:left] = traverse(node.left)
tree[:right] = traverse(node.right)
tree
end
private
def update_parent_link(parent_node, current_node, new_node)
if parent_node.nil?
@root = new_node
elsif current_node.value < parent_node.value
parent_node.left = new_node
else
parent_node.right = new_node
end
end
def find_leftmost(node)
leftmost = node.left
leftmost_parent = node
while leftmost.left
leftmost_parent = leftmost
leftmost = leftmost.left
end
[leftmost, leftmost_parent]
end
end
RSpec.describe BinarySearchTree do
let(:bst) { BinarySearchTree.new }
before(:each) do
# Construct the following tree:
#
# 9
# 4 20
# 1 6 15 170
#
bst.insert(9)
bst.insert(4)
bst.insert(6)
bst.insert(20)
bst.insert(170)
bst.insert(15)
bst.insert(1)
end
describe "#insert" do
it "adds a value to the tree" do
expect(bst.traverse).to eq({ value: 9,
left: { value: 4,
left: { value: 1, left: nil, right: nil },
right: { value: 6, left: nil, right: nil } },
right: { value: 20, left: { value: 15, left: nil, right: nil },
right: { value: 170, left: nil, right: nil } } })
end
end
describe "#lookup" do
it "finds a value in the tree" do
node = bst.lookup(15)
expect(node.value).to eq(15)
end
it "returns nil for a value not in the tree" do
node = bst.lookup(100)
expect(node).to be_nil
end
end
describe "#remove" do
it "removes a leaf node" do
expect(bst.remove(1)).to be true
expect(bst.traverse).to eq({ value: 9,
left: { value: 4, left: nil,
right: { value: 6, left: nil, right: nil } },
right: { value: 20, left: { value: 15, left: nil, right: nil },
right: { value: 170, left: nil, right: nil } } })
end
it "removes a node with one child" do
expect(bst.remove(20)).to be true
expect(bst.traverse).to eq({ value: 9,
left: { value: 4, left: { value: 1, left: nil, right: nil },
right: { value: 6, left: nil, right: nil } },
right: { value: 170, left: { value: 15, left: nil, right: nil },
right: nil } })
end
it "removes a node with two children" do
expect(bst.remove(4)).to be true
expect(bst.traverse).to eq({ value: 9,
left: { value: 6, left: { value: 1, left: nil, right: nil },
right: nil },
right: { value: 20, left: { value: 15, left: nil, right: nil },
right: { value: 170, left: nil, right: nil } } })
end
it "returns false for a value not in the tree" do
expect(bst.remove(100)).to be false
end
end
end