DSA 0.0.1
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 +7 -0
- data/.gitignore +19 -0
- data/DSA.gemspec +24 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +23 -0
- data/README.md +127 -0
- data/Rakefile +2 -0
- data/lib/DSA.rb +10 -0
- data/lib/DSA/algorithm.rb +69 -0
- data/lib/DSA/binary_search_tree.rb +710 -0
- data/lib/DSA/list.rb +183 -0
- data/lib/DSA/priority_queue.rb +70 -0
- data/lib/DSA/stack_and_queue.rb +151 -0
- data/lib/DSA/version.rb +3 -0
- data/test/algorithms_test.rb +91 -0
- data/test/binary_search_tree_test.rb +287 -0
- data/test/list_test.rb +84 -0
- data/test/priority_queue_test.rb +49 -0
- data/test/stack_and_queue_test.rb +96 -0
- metadata +96 -0
@@ -0,0 +1,287 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'DSA'
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
class MyTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
# Called before every test method runs. Can be used
|
8
|
+
# to set up fixture information.
|
9
|
+
def setup
|
10
|
+
# Do nothing
|
11
|
+
end
|
12
|
+
|
13
|
+
# Called after every test method runs. Can be used to tear
|
14
|
+
# down fixture information.
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
# Do nothing
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_bst
|
21
|
+
bst = DSA::BasicBinarySearchTree.new
|
22
|
+
bst[50] = 'string: 50'
|
23
|
+
bst[90] = 'string: 90'
|
24
|
+
bst[80] = 'string: 80'
|
25
|
+
bst[70] = 'string: 70'
|
26
|
+
bst[25] = 'string: 25'
|
27
|
+
bst[30] = 'string: 30'
|
28
|
+
bst[95] = 'string: 95'
|
29
|
+
bst.bfs_print
|
30
|
+
assert_equal 'string: 95', bst.max, 'max failed'
|
31
|
+
assert_equal 'string: 25', bst.min, 'min failed'
|
32
|
+
assert_equal 'string: 80', bst[80], 'get value by key failed'
|
33
|
+
assert_equal nil, bst[55], 'get value by key failed'
|
34
|
+
assert_equal 'string: 80', bst.delete(80), 'delete failed'
|
35
|
+
bst.bfs_print
|
36
|
+
bst.delete 70
|
37
|
+
bst.bfs_print
|
38
|
+
bst.delete 50
|
39
|
+
bst.bfs_print
|
40
|
+
bst[50] = 'string: 50'
|
41
|
+
|
42
|
+
10.times do
|
43
|
+
key = Random.rand 200
|
44
|
+
value = 'string: ' + key.to_s
|
45
|
+
bst[key] = value
|
46
|
+
end
|
47
|
+
bst.bfs_print
|
48
|
+
bst.each do |key, value|
|
49
|
+
printf "<#{key}-#{value}>\t"
|
50
|
+
end
|
51
|
+
puts
|
52
|
+
|
53
|
+
enum = bst.each
|
54
|
+
loop do
|
55
|
+
key, value = enum.next
|
56
|
+
printf "<#{key}-#{value}>\t"
|
57
|
+
end
|
58
|
+
puts
|
59
|
+
|
60
|
+
bst.gt(50) do |key, value|
|
61
|
+
printf "<#{key}-#{value}>\t"
|
62
|
+
end
|
63
|
+
puts
|
64
|
+
|
65
|
+
enum = bst.gt(50)
|
66
|
+
loop do
|
67
|
+
key, value = enum.next
|
68
|
+
printf "<#{key}-#{value}>\t"
|
69
|
+
end
|
70
|
+
puts
|
71
|
+
|
72
|
+
bst.ge(50) do |key, value|
|
73
|
+
printf "<#{key}-#{value}>\t"
|
74
|
+
end
|
75
|
+
puts
|
76
|
+
|
77
|
+
enum = bst.ge(50)
|
78
|
+
loop do
|
79
|
+
key, value = enum.next
|
80
|
+
printf "<#{key}-#{value}>\t"
|
81
|
+
end
|
82
|
+
puts
|
83
|
+
|
84
|
+
bst.lt(50) do |key, value|
|
85
|
+
printf "<#{key}-#{value}>\t"
|
86
|
+
end
|
87
|
+
puts
|
88
|
+
|
89
|
+
enum = bst.lt(50)
|
90
|
+
loop do
|
91
|
+
key, value = enum.next
|
92
|
+
printf "<#{key}-#{value}>\t"
|
93
|
+
end
|
94
|
+
puts
|
95
|
+
|
96
|
+
bst.le(50) do |key, value|
|
97
|
+
printf "<#{key}-#{value}>\t"
|
98
|
+
end
|
99
|
+
puts
|
100
|
+
|
101
|
+
enum = bst.le(50)
|
102
|
+
loop do
|
103
|
+
key, value = enum.next
|
104
|
+
printf "<#{key}-#{value}>\t"
|
105
|
+
end
|
106
|
+
puts
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_rb
|
111
|
+
rb = DSA::RedBlackTree.new
|
112
|
+
rb[100] = 'string_100'
|
113
|
+
rb[50] = 'string_50'
|
114
|
+
rb[1] = 'string_1'
|
115
|
+
rb.bfs_print
|
116
|
+
rb[120] = 'string_120'
|
117
|
+
rb.bfs_print
|
118
|
+
rb[150] = 'string_150'
|
119
|
+
rb.bfs_print
|
120
|
+
|
121
|
+
5.times do
|
122
|
+
key = Random.rand 200
|
123
|
+
value = "string_#{key}"
|
124
|
+
puts "inserting #{key}"
|
125
|
+
rb[key] = value
|
126
|
+
rb.bfs_print
|
127
|
+
end
|
128
|
+
|
129
|
+
100.times do
|
130
|
+
key = Random.rand 200
|
131
|
+
value = "string_#{key}"
|
132
|
+
rb[key] = value
|
133
|
+
end
|
134
|
+
rb.bfs_print
|
135
|
+
rb.gt(50) do |key, value|
|
136
|
+
printf "<#{key}-#{value}>\t"
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
def test_rb_deletion
|
142
|
+
rb = DSA::RedBlackTree.new
|
143
|
+
rb[100] = 'string_100'
|
144
|
+
rb[50] = 'string_50'
|
145
|
+
rb[1] = 'string_1'
|
146
|
+
rb[120] = 'string_120'
|
147
|
+
rb[150] = 'string_150'
|
148
|
+
rb.bfs_print
|
149
|
+
rb.delete 100
|
150
|
+
rb.bfs_print
|
151
|
+
rb[20] = 'string_20'
|
152
|
+
rb.bfs_print
|
153
|
+
rb.delete 1
|
154
|
+
rb.bfs_print
|
155
|
+
rb[180] = 'string_180'
|
156
|
+
rb[170] = 'string_170'
|
157
|
+
rb[190] = 'string_190'
|
158
|
+
|
159
|
+
rb.bfs_print
|
160
|
+
assert_equal 'string_120', rb.delete(120), 'deletion failed'
|
161
|
+
rb.bfs_print
|
162
|
+
|
163
|
+
rb[18] = 'string_18'
|
164
|
+
rb[174] = 'string_174'
|
165
|
+
rb[113] = 'string_113'
|
166
|
+
rb[92] = 'string_92'
|
167
|
+
rb[108] = 'string_108'
|
168
|
+
rb[193] = 'string_193'
|
169
|
+
rb[42] = 'string_42'
|
170
|
+
rb[35] = 'string_35'
|
171
|
+
rb[82] = 'string_82'
|
172
|
+
rb[31] = 'string_31'
|
173
|
+
rb[61] = 'string_61'
|
174
|
+
rb[37] = 'string_37'
|
175
|
+
rb[179] = 'string_179'
|
176
|
+
rb[23] = 'string_23'
|
177
|
+
rb[10] = 'string_10'
|
178
|
+
rb[1] = 'string_1'
|
179
|
+
rb[28] = 'string_28'
|
180
|
+
rb[187] = 'string_187'
|
181
|
+
rb[170] = 'string_170'
|
182
|
+
rb[77] = 'string_77'
|
183
|
+
rb.bfs_print
|
184
|
+
|
185
|
+
rb.delete 92
|
186
|
+
|
187
|
+
rb[177] = 'string_177'
|
188
|
+
rb[95] = 'string_95'
|
189
|
+
rb[71] = 'string_71'
|
190
|
+
rb[18] = 'string_18'
|
191
|
+
rb[33] = 'string_33'
|
192
|
+
rb[87] = 'string_87'
|
193
|
+
rb[94] = 'string_94'
|
194
|
+
rb[109] = 'string_109'
|
195
|
+
rb.bfs_print
|
196
|
+
rb[38] = 'string_38'
|
197
|
+
rb[190] = 'string_190'
|
198
|
+
rb[101] = 'string_101'
|
199
|
+
rb[90] = 'string_90'
|
200
|
+
rb[35] = 'string_35'
|
201
|
+
rb[73] = 'string_73'
|
202
|
+
rb[27] = 'string_27'
|
203
|
+
rb[58] = 'string_58'
|
204
|
+
rb.bfs_print
|
205
|
+
rb[101] = 'string_101'
|
206
|
+
rb[135] = 'string_135'
|
207
|
+
rb.bfs_print
|
208
|
+
rb[18] = 'string_18'
|
209
|
+
rb[159] = 'string_159'
|
210
|
+
rb[32] = 'string_32'
|
211
|
+
rb[127] = 'string_127'
|
212
|
+
rb.bfs_print
|
213
|
+
rb[152] = 'string_152'
|
214
|
+
rb[160] = 'string_160'
|
215
|
+
rb.bfs_print
|
216
|
+
rb[11] = 'string_11'
|
217
|
+
rb[5] = 'string_5'
|
218
|
+
rb[97] = 'string_97'
|
219
|
+
rb[79] = 'string_79'
|
220
|
+
rb[11] = 'string_11'
|
221
|
+
rb[148] = 'string_148'
|
222
|
+
rb[132] = 'string_132'
|
223
|
+
rb.bfs_print
|
224
|
+
rb[37] = 'string_37'
|
225
|
+
rb[17] = 'string_17'
|
226
|
+
rb[20] = 'string_20'
|
227
|
+
rb[26] = 'string_26'
|
228
|
+
rb[23] = 'string_23'
|
229
|
+
rb.bfs_print
|
230
|
+
rb[101] = 'string_101'
|
231
|
+
rb.bfs_print
|
232
|
+
rb[172] = 'string_172'
|
233
|
+
rb[192] = 'string_192'
|
234
|
+
rb[193] = 'string_193'
|
235
|
+
rb[131] = 'string_131'
|
236
|
+
rb[150] = 'string_150'
|
237
|
+
rb[151] = 'string_151'
|
238
|
+
rb[130] = 'string_130'
|
239
|
+
rb[119] = 'string_119'
|
240
|
+
rb[110] = 'string_110'
|
241
|
+
rb[26] = 'string_26'
|
242
|
+
rb[31] = 'string_31'
|
243
|
+
rb[110] = 'string_110'
|
244
|
+
rb[63] = 'string_63'
|
245
|
+
|
246
|
+
rb.bfs_print
|
247
|
+
rb.delete 87
|
248
|
+
rb.bfs_print
|
249
|
+
|
250
|
+
200.times do
|
251
|
+
key = Random.rand 200
|
252
|
+
value = "string_#{key}"
|
253
|
+
puts "rb[#{key}] = '#{value}'"
|
254
|
+
rb[key] = value
|
255
|
+
end
|
256
|
+
|
257
|
+
100.times do
|
258
|
+
key = Random.rand 200
|
259
|
+
puts "deleting #{key}"
|
260
|
+
rb.delete key
|
261
|
+
rb.bfs_print
|
262
|
+
end
|
263
|
+
|
264
|
+
rb.each do |key, value|
|
265
|
+
printf "<#{key}-#{value}>\t"
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_performance
|
271
|
+
puts
|
272
|
+
rb = DSA::RedBlackTree.new
|
273
|
+
hash = Hash.new
|
274
|
+
value = 10**5
|
275
|
+
Benchmark.bm(20) do |x|
|
276
|
+
x.report('RedBlack') { value.times { rb[Random.rand(value)] = 'whatever' } }
|
277
|
+
x.report('RedBlack Find') { value.times { rb[Random.rand(value)] } }
|
278
|
+
x.report('RedBlack Find gt') { value.times { rb.gt(Random.rand(value)) } }
|
279
|
+
x.report('RedBlack Deletion') { (value/2).times { rb.delete Random.rand(value) } }
|
280
|
+
x.report('Built-In Hash') { value.times { hash[Random.rand(value)] = 'whatever' } }
|
281
|
+
x.report('Built-In Hash Find') { value.times { hash[Random.rand(value)] } }
|
282
|
+
x.report('Built-In Hash Deletion') { (value/2).times { hash.delete Random.rand(value) } }
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
|
287
|
+
end
|
data/test/list_test.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'benchmark'
|
3
|
+
require 'DSA'
|
4
|
+
|
5
|
+
class MyTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
# Called before every test method runs. Can be used
|
8
|
+
# to set up fixture information.
|
9
|
+
def setup
|
10
|
+
# Do nothing
|
11
|
+
end
|
12
|
+
|
13
|
+
# Called after every test method runs. Can be used to tear
|
14
|
+
# down fixture information.
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
# Do nothing
|
18
|
+
end
|
19
|
+
|
20
|
+
class DSA::List
|
21
|
+
def print_me
|
22
|
+
printf 'Head --> '
|
23
|
+
each do |e|
|
24
|
+
printf e.to_s + ' --> '
|
25
|
+
end
|
26
|
+
puts 'Tail'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_list
|
31
|
+
l = DSA::List.new
|
32
|
+
l.push 1
|
33
|
+
l.push 10
|
34
|
+
l.push 20
|
35
|
+
l.insert_at 1, 30
|
36
|
+
assert_equal 10, l[2], '[] failed'
|
37
|
+
assert_equal 1, l.first, 'first failed'
|
38
|
+
assert_equal 20, l.last, 'last failed'
|
39
|
+
l.unshift 40
|
40
|
+
l.unshift 50
|
41
|
+
l.unshift 60
|
42
|
+
l.print_me
|
43
|
+
assert_equal 20, l.pop, 'pop failed'
|
44
|
+
l.print_me
|
45
|
+
assert_equal 60, l.shift, 'shift failed'
|
46
|
+
l.print_me
|
47
|
+
assert_equal 1, l.remove_at(2), 'remove failed'
|
48
|
+
l.print_me
|
49
|
+
|
50
|
+
li = l.begin_iterator
|
51
|
+
loop do
|
52
|
+
puts li.next
|
53
|
+
end
|
54
|
+
li = l.end_iterator
|
55
|
+
assert_equal 10, li.previous, 'iteration failed'
|
56
|
+
li.insert 60
|
57
|
+
l.print_me
|
58
|
+
li.previous
|
59
|
+
li.previous
|
60
|
+
assert_equal 30, li.remove, 'remove failed'
|
61
|
+
l.print_me
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_performance
|
66
|
+
value = 10**6
|
67
|
+
a = Array.new
|
68
|
+
b = DSA::List.new
|
69
|
+
puts 'test construction'
|
70
|
+
Benchmark.bm(20) do |x|
|
71
|
+
x.report('Array') { value.times{ |i| a.push i } }
|
72
|
+
x.report('List') { value.times{ |i| b.push i } }
|
73
|
+
end
|
74
|
+
|
75
|
+
puts 'test insertion'
|
76
|
+
|
77
|
+
value = 10**3
|
78
|
+
li = b.begin_iterator
|
79
|
+
Benchmark.bm(20) do |x|
|
80
|
+
x.report('Array') { value.times{ |i| a.insert(value, i) } }
|
81
|
+
x.report('List') { value.times{ li.next }; value.times{ |i| li.insert(i) }; }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'DSA'
|
3
|
+
|
4
|
+
class MyTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# Called before every test method runs. Can be used
|
7
|
+
# to set up fixture information.
|
8
|
+
def setup
|
9
|
+
# Do nothing
|
10
|
+
end
|
11
|
+
|
12
|
+
# Called after every test method runs. Can be used to tear
|
13
|
+
# down fixture information.
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
# Do nothing
|
17
|
+
end
|
18
|
+
|
19
|
+
class DSA::PriorityQueue
|
20
|
+
def print_me
|
21
|
+
printf 'Start --> '
|
22
|
+
@data.each do |e|
|
23
|
+
printf e.element.to_s + ' --> '
|
24
|
+
end
|
25
|
+
puts 'End'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_priority_queue
|
30
|
+
pq = DSA::PriorityQueue.new
|
31
|
+
10.times do
|
32
|
+
number = Random.rand(50..100)
|
33
|
+
pq.add( number, 'job:'+number.to_s )
|
34
|
+
end
|
35
|
+
pq.print_me
|
36
|
+
pq.add 10, 'job:10'
|
37
|
+
pq.add 20, 'job:20'
|
38
|
+
assert_equal 'job:10', pq.top
|
39
|
+
pq.print_me
|
40
|
+
assert_equal 'job:10', pq.pop
|
41
|
+
pq.print_me
|
42
|
+
pq.pop
|
43
|
+
pq.print_me
|
44
|
+
pq.pop
|
45
|
+
pq.print_me
|
46
|
+
pq.pop
|
47
|
+
pq.print_me
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'DSA'
|
3
|
+
|
4
|
+
class MyTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# Called before every test method runs. Can be used
|
7
|
+
# to set up fixture information.
|
8
|
+
def setup
|
9
|
+
# Do nothing
|
10
|
+
end
|
11
|
+
|
12
|
+
# Called after every test method runs. Can be used to tear
|
13
|
+
# down fixture information.
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
# Do nothing
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_stack
|
20
|
+
s = DSA::ArrayStack.new
|
21
|
+
s.push 1
|
22
|
+
s.push 2
|
23
|
+
assert_equal 2, s.pop, 'failed pop'
|
24
|
+
assert_equal false, s.empty?, 'failed empty?'
|
25
|
+
assert_equal 1, s.top, 'failed top'
|
26
|
+
assert_equal 1, s.length, 'failed length'
|
27
|
+
|
28
|
+
s = DSA::ListStack.new
|
29
|
+
s.push 1
|
30
|
+
s.push 2
|
31
|
+
assert_equal 2, s.pop, 'failed pop'
|
32
|
+
assert_equal false, s.empty?, 'failed empty?'
|
33
|
+
assert_equal 1, s.top, 'failed top'
|
34
|
+
assert_equal 1, s.length, 'failed length'
|
35
|
+
end
|
36
|
+
|
37
|
+
class DSA::ArrayQueue
|
38
|
+
def print_me
|
39
|
+
print @data.join(' --> ')
|
40
|
+
puts
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class DSA::ListQueue
|
45
|
+
def print_me
|
46
|
+
printf 'Head --> '
|
47
|
+
@data.each do |e|
|
48
|
+
printf e.to_s + ' --> '
|
49
|
+
end
|
50
|
+
puts 'Tail'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_queue
|
55
|
+
puts
|
56
|
+
q = DSA::ArrayQueue.new
|
57
|
+
q.print_me
|
58
|
+
assert_equal true, q.empty?, 'failed empty?'
|
59
|
+
q.enqueue 1
|
60
|
+
q.enqueue 2
|
61
|
+
q.enqueue 3
|
62
|
+
assert_equal 3, q.length, 'failed length'
|
63
|
+
q.print_me
|
64
|
+
q.dequeue
|
65
|
+
q.dequeue
|
66
|
+
assert_equal 3, q.first, 'failed first'
|
67
|
+
q.print_me
|
68
|
+
25.times { |i| q.enqueue i }
|
69
|
+
q.print_me
|
70
|
+
10.times { q.dequeue }
|
71
|
+
q.print_me
|
72
|
+
assert_equal 9, q.first, 'failed first'
|
73
|
+
assert_equal 16, q.length, 'failed length'
|
74
|
+
|
75
|
+
|
76
|
+
puts
|
77
|
+
q = DSA::ListQueue.new
|
78
|
+
q.print_me
|
79
|
+
assert_equal true, q.empty?, 'failed empty?'
|
80
|
+
q.enqueue 1
|
81
|
+
q.enqueue 2
|
82
|
+
q.enqueue 3
|
83
|
+
assert_equal 3, q.length, 'failed length'
|
84
|
+
q.print_me
|
85
|
+
q.dequeue
|
86
|
+
q.dequeue
|
87
|
+
assert_equal 3, q.first, 'failed first'
|
88
|
+
q.print_me
|
89
|
+
25.times { |i| q.enqueue i }
|
90
|
+
q.print_me
|
91
|
+
10.times { q.dequeue }
|
92
|
+
q.print_me
|
93
|
+
assert_equal 9, q.first, 'failed first'
|
94
|
+
assert_equal 16, q.length, 'failed length'
|
95
|
+
end
|
96
|
+
end
|