avl_tree 1.1.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/avl_tree.rb +6 -0
- data/lib/red_black_tree.rb +6 -0
- data/test/test_avl_tree.rb +20 -0
- data/test/test_red_black_tree.rb +20 -0
- metadata +9 -3
data/lib/avl_tree.rb
CHANGED
@@ -57,6 +57,8 @@ class AVLTree
|
|
57
57
|
@value = value
|
58
58
|
when 1
|
59
59
|
@right = @right.insert(key, value)
|
60
|
+
else
|
61
|
+
raise TypeError, "cannot compare #{key} and #{@key} with <=>"
|
60
62
|
end
|
61
63
|
rotate
|
62
64
|
end
|
@@ -70,6 +72,8 @@ class AVLTree
|
|
70
72
|
@value
|
71
73
|
when 1
|
72
74
|
@right.retrieve(key)
|
75
|
+
else
|
76
|
+
nil
|
73
77
|
end
|
74
78
|
end
|
75
79
|
|
@@ -84,6 +88,8 @@ class AVLTree
|
|
84
88
|
when 1
|
85
89
|
deleted, @right = @right.delete(key)
|
86
90
|
[deleted, self.rotate]
|
91
|
+
else
|
92
|
+
raise TypeError, "cannot compare #{key} and #{@key} with <=>"
|
87
93
|
end
|
88
94
|
end
|
89
95
|
|
data/lib/red_black_tree.rb
CHANGED
@@ -77,6 +77,8 @@ class RedBlackTree
|
|
77
77
|
if black? and @left.black? and @right.red? and !@right.children_both_black?
|
78
78
|
ret = rebalance_for_right_insert
|
79
79
|
end
|
80
|
+
else
|
81
|
+
raise TypeError, "cannot compare #{key} and #{@key} with <=>"
|
80
82
|
end
|
81
83
|
ret.pullup_red
|
82
84
|
end
|
@@ -90,6 +92,8 @@ class RedBlackTree
|
|
90
92
|
@value
|
91
93
|
when 1
|
92
94
|
@right.retrieve(key)
|
95
|
+
else
|
96
|
+
nil
|
93
97
|
end
|
94
98
|
end
|
95
99
|
|
@@ -110,6 +114,8 @@ class RedBlackTree
|
|
110
114
|
if rebalance
|
111
115
|
ret, rebalance = rebalance_for_right_delete
|
112
116
|
end
|
117
|
+
else
|
118
|
+
raise TypeError, "cannot compare #{key} and #{@key} with <=>"
|
113
119
|
end
|
114
120
|
[deleted, ret, rebalance]
|
115
121
|
end
|
data/test/test_avl_tree.rb
CHANGED
@@ -155,6 +155,15 @@ class TestAVLTree < Test::Unit::TestCase
|
|
155
155
|
assert_equal 6, h.size
|
156
156
|
end
|
157
157
|
|
158
|
+
def test_different_type
|
159
|
+
h = AVLTree.new
|
160
|
+
h['a'] = 1
|
161
|
+
assert_raise(TypeError) do
|
162
|
+
h[3.3] = 2
|
163
|
+
end
|
164
|
+
assert_nil h[3.3]
|
165
|
+
end
|
166
|
+
|
158
167
|
def test_delete_leaf
|
159
168
|
h = AVLTree.new
|
160
169
|
h['b'] = 1
|
@@ -307,6 +316,17 @@ class TestAVLTree < Test::Unit::TestCase
|
|
307
316
|
assert_equal 5, h.dump_tree.split($/).size
|
308
317
|
end
|
309
318
|
|
319
|
+
def test_delete_different_type
|
320
|
+
h = AVLTree.new
|
321
|
+
h['a'] = 1
|
322
|
+
h['abc'] = 2
|
323
|
+
h['bb'] = 3
|
324
|
+
|
325
|
+
assert_raise(TypeError) do
|
326
|
+
h.delete(3.3)
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
310
330
|
def test_each
|
311
331
|
h = AVLTree.new
|
312
332
|
s = { 'aa' => 1, 'ab' => 2, 'bb' => 3, 'bc' => 4, 'a' => 5, 'abc' => 6 }
|
data/test/test_red_black_tree.rb
CHANGED
@@ -169,6 +169,15 @@ class TestRedBlackTree < Test::Unit::TestCase
|
|
169
169
|
assert_equal 6, h.size
|
170
170
|
end
|
171
171
|
|
172
|
+
def test_different_type
|
173
|
+
h = RedBlackTree.new
|
174
|
+
h['a'] = 1
|
175
|
+
assert_raise(TypeError) do
|
176
|
+
h[3.3] = 2
|
177
|
+
end
|
178
|
+
assert_nil h[3.3]
|
179
|
+
end
|
180
|
+
|
172
181
|
def test_delete_leaf
|
173
182
|
h = RedBlackTree.new
|
174
183
|
h['b'] = 1
|
@@ -457,6 +466,17 @@ class TestRedBlackTree < Test::Unit::TestCase
|
|
457
466
|
assert_equal '(f (c (b a) (d - e)) (i (g - h) (k j)))', h.dump_sexp
|
458
467
|
end
|
459
468
|
|
469
|
+
def test_delete_different_type
|
470
|
+
h = RedBlackTree.new
|
471
|
+
h['a'] = 1
|
472
|
+
h['abc'] = 2
|
473
|
+
h['bb'] = 3
|
474
|
+
|
475
|
+
assert_raise(TypeError) do
|
476
|
+
h.delete(3.3)
|
477
|
+
end
|
478
|
+
end
|
479
|
+
|
460
480
|
def test_each
|
461
481
|
h = RedBlackTree.new
|
462
482
|
s = { 'aa' => 1, 'ab' => 2, 'bb' => 3, 'bc' => 4, 'a' => 5, 'abc' => 6 }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: avl_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-09 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email: nahi@ruby-lang.org
|
@@ -38,15 +38,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
38
|
- - ! '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
hash: -4094052252433988634
|
41
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
45
|
none: false
|
43
46
|
requirements:
|
44
47
|
- - ! '>='
|
45
48
|
- !ruby/object:Gem::Version
|
46
49
|
version: '0'
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
hash: -4094052252433988634
|
47
53
|
requirements: []
|
48
54
|
rubyforge_project:
|
49
|
-
rubygems_version: 1.8.
|
55
|
+
rubygems_version: 1.8.23
|
50
56
|
signing_key:
|
51
57
|
specification_version: 3
|
52
58
|
summary: AVL tree and Red-black tree in Ruby
|