binary_search_tree 1.21 → 2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +6 -14
- data/lib/binary_search_tree.rb +139 -97
- data/lib/binary_search_tree_hash.rb +7 -5
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
OThlZDIyZThmNDNhNGViMjM2Zjc2M2M5M2ZjMDNlYmI4Y2I3NjkzMzE5MzA0
|
10
|
-
MTViMWM3ODExYWMxNDgwYjI3M2ViYmYxZDAyMTFhMzQ1ZTViYWJhNjRlZjI2
|
11
|
-
ZGE1YjI3NGQxN2VjNWU4MTI3ZGJkZTVhNDY0NmIyMDI2NjY4YTA=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
ZDhhMTNiZGNiZGVkMDAwMTlmNTJiOGU3MDY2NTkxNTRiODEwYjkzNmVjMmJj
|
14
|
-
NmQ3YmZmNmE4YTQ1ODBkMTg1NzA5ODI3ZWFhNGY4OGVkNTk1NzQ0NzEyNmJk
|
15
|
-
ZGJmZTc4YmNkNTI0YTU1MzFkOTczNTFhNjJjNTQ3N2M2MjI1NTQ=
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 03a236331048d1d421274964c9bb1f786a2433f8
|
4
|
+
data.tar.gz: dc1468a43def290f733104a9df3cd23966c80dd5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3809ba134072969efddf39f35284abaf407680821c17eab8932d7b05133ef651eb81ae6ff78793a99fe11c28a703f2b8af892305098a7e499e807f5d21d271e6
|
7
|
+
data.tar.gz: 3edbf53d2331c7629d2aa4dbffac2930ec9aa99a16786cf34a7888f4af1380e7fd53eb947f026da982062cfb6fdb1b378e39490176ad7a1b93e5fca96b40bff5
|
data/lib/binary_search_tree.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
1
2
|
class BinaryNode
|
2
3
|
attr_accessor :height, :parent, :left, :right, :key, :value
|
3
4
|
|
@@ -134,7 +135,9 @@ class BinarySearchTree
|
|
134
135
|
end
|
135
136
|
|
136
137
|
def put element, value, leaf, parent, link_type=nil
|
138
|
+
#once you reach a point where you can place a new node
|
137
139
|
if leaf.nil?
|
140
|
+
#create that new node
|
138
141
|
leaf = BinaryNode.new element, value, parent
|
139
142
|
@size += 1
|
140
143
|
invalidate_cached_values
|
@@ -148,8 +151,11 @@ class BinarySearchTree
|
|
148
151
|
@root = leaf
|
149
152
|
end
|
150
153
|
if parent.present? && parent.height.zero?
|
154
|
+
#if it has a parent but it is balanced, move up
|
151
155
|
node = parent
|
152
156
|
node_to_rebalance = nil
|
157
|
+
|
158
|
+
#continue moving up until you git the root
|
153
159
|
while node.present?
|
154
160
|
node.height = node.max_children_height + 1
|
155
161
|
if node.balance_factor.abs > 1
|
@@ -158,6 +164,7 @@ class BinarySearchTree
|
|
158
164
|
end
|
159
165
|
node = node.parent
|
160
166
|
end
|
167
|
+
#if at any point you reach an unbalanced node, rebalance it
|
161
168
|
rebalance node_to_rebalance if node_to_rebalance.present?
|
162
169
|
end
|
163
170
|
|
@@ -203,64 +210,130 @@ class BinarySearchTree
|
|
203
210
|
raise "assertion failed" unless condition
|
204
211
|
end
|
205
212
|
|
213
|
+
def rrc_rebalance a, f
|
214
|
+
|
215
|
+
#puts "performing rrc rebalance"
|
216
|
+
b = a.right
|
217
|
+
c = b.right
|
218
|
+
assert a.present? && b.present? && c.present?
|
219
|
+
a.right = b.left
|
220
|
+
if a.right.present?
|
221
|
+
a.right.parent = a
|
222
|
+
end
|
223
|
+
b.left = a
|
224
|
+
a.parent = b
|
225
|
+
if f.nil?
|
226
|
+
@root = b
|
227
|
+
@root.parent = nil
|
228
|
+
else
|
229
|
+
if f.right == a
|
230
|
+
f.right = b
|
231
|
+
else
|
232
|
+
f.left = b
|
233
|
+
end
|
234
|
+
b.parent = f
|
235
|
+
end
|
236
|
+
recompute_heights a
|
237
|
+
recompute_heights b.parent
|
238
|
+
end
|
239
|
+
|
240
|
+
def rlc_rebalance a, f
|
241
|
+
|
242
|
+
#puts "performing rlc rebalance"
|
243
|
+
b = a.right
|
244
|
+
c = b.left
|
245
|
+
assert a.present? && b.present? && c.present?
|
246
|
+
b.left = c.right
|
247
|
+
if b.left.present?
|
248
|
+
b.left.parent = b
|
249
|
+
end
|
250
|
+
a.right = c.left
|
251
|
+
if a.right.present?
|
252
|
+
a.right.parent = a
|
253
|
+
end
|
254
|
+
c.right = b
|
255
|
+
b.parent = c
|
256
|
+
c.left = a
|
257
|
+
a.parent = c
|
258
|
+
if f.nil?
|
259
|
+
@root = c
|
260
|
+
@root.parent = nil
|
261
|
+
else
|
262
|
+
if f.right == a
|
263
|
+
f.right = c
|
264
|
+
else
|
265
|
+
f.left = c
|
266
|
+
end
|
267
|
+
c.parent = f
|
268
|
+
end
|
269
|
+
recompute_heights a
|
270
|
+
recompute_heights b
|
271
|
+
end
|
272
|
+
|
273
|
+
def llc_rebalance a, b, c, f
|
274
|
+
#puts "performing llc rebalance"
|
275
|
+
assert a.present? && b.present? && c.present?
|
276
|
+
a.left = b.right
|
277
|
+
if a.left
|
278
|
+
a.left.parent = a
|
279
|
+
end
|
280
|
+
b.right = a
|
281
|
+
a.parent = b
|
282
|
+
if f.nil?
|
283
|
+
@root = b
|
284
|
+
@root.parent = nil
|
285
|
+
else
|
286
|
+
if f.right == a
|
287
|
+
f.right = b
|
288
|
+
else
|
289
|
+
f.left = b
|
290
|
+
end
|
291
|
+
b.parent = f
|
292
|
+
end
|
293
|
+
recompute_heights a
|
294
|
+
recompute_heights b.parent
|
295
|
+
end
|
296
|
+
|
297
|
+
def lrc_rebalance a, b, c, f
|
298
|
+
#puts "performing lrc rebalance"
|
299
|
+
assert a.present? && b.present? && c.present?
|
300
|
+
a.left = c.right
|
301
|
+
if a.left.present?
|
302
|
+
a.left.parent = a
|
303
|
+
end
|
304
|
+
b.right = c.left
|
305
|
+
if b.right.present?
|
306
|
+
b.right.parent = b
|
307
|
+
end
|
308
|
+
c.left = b
|
309
|
+
b.parent = c
|
310
|
+
c.right = a
|
311
|
+
a.parent = c
|
312
|
+
if f.nil?
|
313
|
+
@root = c
|
314
|
+
@root.parent = nil
|
315
|
+
else
|
316
|
+
if f.right == a
|
317
|
+
f.right = c
|
318
|
+
else
|
319
|
+
f.left = c
|
320
|
+
end
|
321
|
+
c.parent = f
|
322
|
+
end
|
323
|
+
recompute_heights a
|
324
|
+
recompute_heights b
|
325
|
+
end
|
326
|
+
|
206
327
|
def rebalance node_to_rebalance
|
207
328
|
a = node_to_rebalance
|
208
329
|
f = a.parent #allowed to be NULL
|
209
330
|
if node_to_rebalance.balance_factor == -2
|
210
331
|
if node_to_rebalance.right.balance_factor <= 0
|
211
332
|
# """Rebalance, case RRC """
|
212
|
-
|
213
|
-
c = b.right
|
214
|
-
assert a.present? && b.present? && c.present?
|
215
|
-
a.right = b.left
|
216
|
-
if a.right.present?
|
217
|
-
a.right.parent = a
|
218
|
-
end
|
219
|
-
b.left = a
|
220
|
-
a.parent = b
|
221
|
-
if f.nil?
|
222
|
-
@root = b
|
223
|
-
@root.parent = nil
|
224
|
-
else
|
225
|
-
if f.right == a
|
226
|
-
f.right = b
|
227
|
-
else
|
228
|
-
f.left = b
|
229
|
-
end
|
230
|
-
b.parent = f
|
231
|
-
end
|
232
|
-
recompute_heights a
|
233
|
-
recompute_heights b.parent
|
333
|
+
rrc_rebalance a, f
|
234
334
|
else
|
335
|
+
rlc_rebalance a, f
|
235
336
|
# """Rebalance, case RLC """
|
236
|
-
b = a.right
|
237
|
-
c = b.left
|
238
|
-
assert a.present? && b.present? && c.present?
|
239
|
-
b.left = c.right
|
240
|
-
if b.left.present?
|
241
|
-
b.left.parent = b
|
242
|
-
end
|
243
|
-
a.right = c.left
|
244
|
-
if a.right.present?
|
245
|
-
a.right.parent = a
|
246
|
-
end
|
247
|
-
c.right = b
|
248
|
-
b.parent = c
|
249
|
-
c.left = a
|
250
|
-
a.parent = c
|
251
|
-
if f.nil?
|
252
|
-
@root = c
|
253
|
-
@root.parent = nil
|
254
|
-
else
|
255
|
-
if f.right == a
|
256
|
-
f.right = c
|
257
|
-
else
|
258
|
-
f.left = c
|
259
|
-
end
|
260
|
-
c.parent = f
|
261
|
-
end
|
262
|
-
recompute_heights a
|
263
|
-
recompute_heights b
|
264
337
|
end
|
265
338
|
else
|
266
339
|
assert node_to_rebalance.balance_factor == 2
|
@@ -268,56 +341,12 @@ class BinarySearchTree
|
|
268
341
|
b = a.left
|
269
342
|
c = b.left
|
270
343
|
# """Rebalance, case LLC """
|
271
|
-
|
272
|
-
a.left = b.right
|
273
|
-
if a.left
|
274
|
-
a.left.parent = a
|
275
|
-
end
|
276
|
-
b.right = a
|
277
|
-
a.parent = b
|
278
|
-
if f.nil?
|
279
|
-
@root = b
|
280
|
-
@root.parent = nil
|
281
|
-
else
|
282
|
-
if f.right == a
|
283
|
-
f.right = b
|
284
|
-
else
|
285
|
-
f.left = b
|
286
|
-
end
|
287
|
-
b.parent = f
|
288
|
-
end
|
289
|
-
recompute_heights a
|
290
|
-
recompute_heights b.parent
|
344
|
+
llc_rebalance a, b, c, f
|
291
345
|
else
|
292
346
|
b = a.left
|
293
347
|
c = b.right
|
294
348
|
# """Rebalance, case LRC """
|
295
|
-
|
296
|
-
a.left = c.right
|
297
|
-
if a.left.present?
|
298
|
-
a.left.parent = a
|
299
|
-
end
|
300
|
-
b.right = c.left
|
301
|
-
if b.right.present?
|
302
|
-
b.right.parent = b
|
303
|
-
end
|
304
|
-
c.left = b
|
305
|
-
b.parent = c
|
306
|
-
c.right = a
|
307
|
-
a.parent = c
|
308
|
-
if f.nil?
|
309
|
-
@root = c
|
310
|
-
@root.parent = nil
|
311
|
-
else
|
312
|
-
if f.right == a
|
313
|
-
f.right = c
|
314
|
-
else
|
315
|
-
f.left = c
|
316
|
-
end
|
317
|
-
c.parent = f
|
318
|
-
end
|
319
|
-
recompute_heights a
|
320
|
-
recompute_heights b
|
349
|
+
lrc_rebalance a, b, c, f
|
321
350
|
end
|
322
351
|
end
|
323
352
|
end
|
@@ -477,4 +506,17 @@ class BinarySearchTree
|
|
477
506
|
|
478
507
|
end
|
479
508
|
|
480
|
-
require 'binary_search_tree_hash.rb'
|
509
|
+
# #require 'binary_search_tree_hash.rb'
|
510
|
+
# tree = BinarySearchTree.new
|
511
|
+
# puts "inserting 9"
|
512
|
+
# tree.insert(9, "nine")
|
513
|
+
# puts "inserting 5"
|
514
|
+
# tree.insert(5, "five")
|
515
|
+
# puts "inserting 10"
|
516
|
+
# tree.insert(10, "ten")
|
517
|
+
# puts "inserting 1"
|
518
|
+
# tree.insert(1, "ten")
|
519
|
+
# puts "inserting 3"
|
520
|
+
# tree.insert(3, "ten")
|
521
|
+
# puts "inserting 7"
|
522
|
+
# tree.insert(7, "ten")
|
@@ -40,7 +40,7 @@ class BinarySearchTreeHash
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def values
|
43
|
-
map{ |node| node.second }
|
43
|
+
map{ |node| node.second }
|
44
44
|
end
|
45
45
|
|
46
46
|
def to_a
|
@@ -56,7 +56,7 @@ class BinarySearchTreeHash
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def max
|
59
|
-
[@bst.max.key, @bst.max.value]
|
59
|
+
[@bst.max.key, @bst.max.value]
|
60
60
|
end
|
61
61
|
|
62
62
|
def each
|
@@ -93,7 +93,7 @@ class BinarySearchTreeHash
|
|
93
93
|
nodes[i] = nil
|
94
94
|
end
|
95
95
|
end
|
96
|
-
changed ? nodes.compact.map{ |node| [node.key, node.value] } : nil
|
96
|
+
changed ? nodes.compact.map{ |node| [node.key, node.value] } : nil
|
97
97
|
end
|
98
98
|
|
99
99
|
def delete_if
|
@@ -129,7 +129,7 @@ class BinarySearchTreeHash
|
|
129
129
|
end
|
130
130
|
|
131
131
|
def has_value? value
|
132
|
-
@bst.find_value(value).present?
|
132
|
+
@bst.find_value(value).present?
|
133
133
|
end
|
134
134
|
|
135
135
|
def value? value
|
@@ -185,4 +185,6 @@ private
|
|
185
185
|
def bst
|
186
186
|
@bst
|
187
187
|
end
|
188
|
-
end
|
188
|
+
end
|
189
|
+
|
190
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: binary_search_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '
|
4
|
+
version: '2.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Misha Conway
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: MishaAConway@gmail.com
|
@@ -28,17 +28,17 @@ require_paths:
|
|
28
28
|
- lib
|
29
29
|
required_ruby_version: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
35
|
requirements:
|
36
|
-
- -
|
36
|
+
- - ">="
|
37
37
|
- !ruby/object:Gem::Version
|
38
38
|
version: '0'
|
39
39
|
requirements: []
|
40
40
|
rubyforge_project: nowarning
|
41
|
-
rubygems_version: 2.
|
41
|
+
rubygems_version: 2.2.2
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
44
|
summary: A self balancing avl binary search tree class. Also includes BinarySearchTreeHash
|