ds 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. data/.gitignore +2 -0
  2. data/README.rdoc +490 -0
  3. data/Rakefile +1 -1
  4. data/lib/ds.rb +17 -19
  5. data/lib/ds/ext/array_x.rb +35 -0
  6. data/lib/ds/graphs/graph.rb +36 -32
  7. data/lib/ds/graphs/graph_as_matrix.rb +22 -23
  8. data/lib/ds/graphs/graph_as_tri_matrix.rb +1 -2
  9. data/lib/ds/lists/list.rb +18 -21
  10. data/lib/ds/matrixes/array_2d.rb +4 -0
  11. data/lib/ds/pair.rb +14 -0
  12. data/lib/ds/queues/priority_queue.rb +36 -0
  13. data/lib/ds/queues/queue.rb +1 -1
  14. data/lib/ds/sets/ordered_set.rb +32 -0
  15. data/lib/ds/trees/binary_heap.rb +45 -14
  16. data/lib/ds/trees/binary_search_tree.rb +3 -1
  17. data/lib/ds/trees/binary_tree.rb +11 -5
  18. data/lib/ds/trees/complete_binary_tree.rb +24 -16
  19. data/lib/ds/trees/tree.rb +3 -3
  20. data/lib/ds/trees/tree_walker.rb +7 -6
  21. data/lib/ds/trees/trie.rb +14 -20
  22. data/lib/ds/version.rb +1 -1
  23. data/test/help.rb +12 -3
  24. data/test/test_array2d.rb +14 -4
  25. data/test/test_array_x.rb +27 -7
  26. data/test/test_binary_heap.rb +19 -1
  27. data/test/test_binary_search_tree.rb +8 -1
  28. data/test/test_binary_tree.rb +7 -1
  29. data/test/test_complete_binary_tree.rb +34 -6
  30. data/test/test_digraph.rb +1 -1
  31. data/test/test_graph.rb +10 -1
  32. data/test/test_list.rb +59 -2
  33. data/test/test_ordered_set.rb +28 -0
  34. data/test/test_pair.rb +26 -0
  35. data/test/test_priority_queue.rb +134 -0
  36. data/test/test_queue.rb +3 -3
  37. data/test/test_stack.rb +0 -2
  38. data/test/test_tree.rb +32 -9
  39. data/test/test_tree_walker.rb +22 -0
  40. data/test/test_trie.rb +20 -2
  41. metadata +13 -81
  42. data/doc/Array.html +0 -264
  43. data/doc/DS.html +0 -292
  44. data/doc/DS/Array2D.html +0 -345
  45. data/doc/DS/BinaryHeap.html +0 -493
  46. data/doc/DS/BinarySearchTree.html +0 -313
  47. data/doc/DS/BinaryTree.html +0 -433
  48. data/doc/DS/CompleteBinaryTree.html +0 -550
  49. data/doc/DS/CyclicList.html +0 -234
  50. data/doc/DS/Digraph.html +0 -299
  51. data/doc/DS/Edge.html +0 -283
  52. data/doc/DS/ExpandableArray.html +0 -316
  53. data/doc/DS/Graph.html +0 -739
  54. data/doc/DS/GraphAsList.html +0 -361
  55. data/doc/DS/GraphAsMatrix.html +0 -633
  56. data/doc/DS/GraphAsTriMatrix.html +0 -274
  57. data/doc/DS/List.html +0 -1263
  58. data/doc/DS/ListElement.html +0 -344
  59. data/doc/DS/Queue.html +0 -517
  60. data/doc/DS/Ring.html +0 -323
  61. data/doc/DS/Stack.html +0 -407
  62. data/doc/DS/Tree.html +0 -770
  63. data/doc/DS/TreeWalker.html +0 -561
  64. data/doc/DS/TriMatrix.html +0 -338
  65. data/doc/created.rid +0 -25
  66. data/doc/ds/graphs/digraph_rb.html +0 -52
  67. data/doc/ds/graphs/edge_rb.html +0 -52
  68. data/doc/ds/graphs/graph_as_list_rb.html +0 -52
  69. data/doc/ds/graphs/graph_as_matrix_rb.html +0 -52
  70. data/doc/ds/graphs/graph_as_tri_matrix_rb.html +0 -52
  71. data/doc/ds/graphs/graph_rb.html +0 -52
  72. data/doc/ds/lists/cyclic_list_rb.html +0 -52
  73. data/doc/ds/lists/list_element_rb.html +0 -52
  74. data/doc/ds/lists/list_rb.html +0 -52
  75. data/doc/ds/lists/ring_rb.html +0 -52
  76. data/doc/ds/matrixes/array_2d_rb.html +0 -52
  77. data/doc/ds/matrixes/expandable_array_rb.html +0 -52
  78. data/doc/ds/matrixes/tri_matrix_rb.html +0 -52
  79. data/doc/ds/queues/queue_rb.html +0 -52
  80. data/doc/ds/stacks/stack_rb.html +0 -52
  81. data/doc/ds/trees/binary_heap_rb.html +0 -52
  82. data/doc/ds/trees/binary_search_tree_rb.html +0 -52
  83. data/doc/ds/trees/binary_tree_rb.html +0 -52
  84. data/doc/ds/trees/complete_binary_tree_rb.html +0 -52
  85. data/doc/ds/trees/tree_rb.html +0 -52
  86. data/doc/ds/trees/tree_walker_rb.html +0 -52
  87. data/doc/ds/version_rb.html +0 -52
  88. data/doc/ds_rb.html +0 -98
  89. data/doc/ext/ext_rb.html +0 -52
  90. data/doc/images/brick.png +0 -0
  91. data/doc/images/brick_link.png +0 -0
  92. data/doc/images/bug.png +0 -0
  93. data/doc/images/bullet_black.png +0 -0
  94. data/doc/images/bullet_toggle_minus.png +0 -0
  95. data/doc/images/bullet_toggle_plus.png +0 -0
  96. data/doc/images/date.png +0 -0
  97. data/doc/images/find.png +0 -0
  98. data/doc/images/loadingAnimation.gif +0 -0
  99. data/doc/images/macFFBgHack.png +0 -0
  100. data/doc/images/package.png +0 -0
  101. data/doc/images/page_green.png +0 -0
  102. data/doc/images/page_white_text.png +0 -0
  103. data/doc/images/page_white_width.png +0 -0
  104. data/doc/images/plugin.png +0 -0
  105. data/doc/images/ruby.png +0 -0
  106. data/doc/images/tag_green.png +0 -0
  107. data/doc/images/wrench.png +0 -0
  108. data/doc/images/wrench_orange.png +0 -0
  109. data/doc/images/zoom.png +0 -0
  110. data/doc/index.html +0 -375
  111. data/doc/js/darkfish.js +0 -116
  112. data/doc/js/jquery.js +0 -32
  113. data/doc/js/quicksearch.js +0 -114
  114. data/doc/js/thickbox-compressed.js +0 -10
  115. data/doc/rdoc.css +0 -763
  116. data/lib/ds/ext/ext.rb +0 -36
  117. data/lib/ds/graphs/graph_as_list.rb +0 -48
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ doc/*
6
+ coverage/*
@@ -0,0 +1,490 @@
1
+ = DS - Data Structures
2
+
3
+ DS provides some common data structures not implement in Ruby natively.
4
+
5
+ The DS gem supports the folowing data structures:
6
+
7
+ * Pair
8
+ * Stacks
9
+ * Stack
10
+ * Queues
11
+ * Queue
12
+ * PriorityQueue
13
+ * Lists
14
+ * List
15
+ * CyclicList
16
+ * Ring
17
+ * Trees
18
+ * Tree
19
+ * BinaryTree
20
+ * CompleteBinaryTree
21
+ * BinaryHeap
22
+ * BinarySearchTree
23
+ * Trie
24
+ * Graphs
25
+ * Graph
26
+ * Digraph
27
+ * Matrixes
28
+ * Array2D
29
+ * ExpandableArray
30
+ * TriMatrix
31
+ * Sets
32
+ * OrderedSet
33
+
34
+
35
+ == Instalation
36
+
37
+ gem install ds
38
+
39
+ == Usage
40
+
41
+ require 'ds'
42
+ stack = DS::Stack.new
43
+
44
+ # To not have to type "DS::" before each class, use:
45
+ include DS
46
+ stack = Stack.new
47
+
48
+ === Pair
49
+
50
+ Pair is simple key-value data structure.
51
+
52
+ Creating new Pair
53
+ p = Pair.new(3,9)
54
+
55
+ Accessors defined on Pair object:
56
+
57
+ p.key #=> 3
58
+ p.first #=> 3
59
+ p.value #=> 9
60
+ p.second #=> 9
61
+ p.second = 27
62
+
63
+ === Stack
64
+
65
+ Stack is very simple data structure which allows access only to the top element.
66
+ More: {Stack}[http://en.wikipedia.org/wiki/Stack_(abstract_data_type)]
67
+
68
+ Creating new Stack (implemented as Array).
69
+ stack = Stack.new
70
+
71
+ The following methods are available on a Stack:
72
+
73
+ * push
74
+ * pop
75
+ * size
76
+ * empty?
77
+ * size
78
+
79
+ Examples:
80
+ stack.empty? #=> true
81
+ stack.push :first
82
+ stack.push :second
83
+ stack.size #=> 2
84
+ stack.peek #=> :second
85
+ stack.empty? #=> false
86
+ stack.pop #=> :second
87
+ stack.size #=> 1
88
+
89
+
90
+
91
+ === Queues
92
+
93
+ Queues is First-In-First-Out (FIFO) data structure.
94
+ Which means that first element added to the queue will be the first one to be removed.
95
+ More: {Queue}[http://en.wikipedia.org/wiki/Queue_(data_structure)]
96
+
97
+
98
+ ==== Queue
99
+
100
+ Creating new Queue (implemented as Array).
101
+ q = Queue.new
102
+
103
+ Creating new Queue (implemented as List)
104
+ q1 = Queue.create
105
+ q1 = Queue.new(:list)
106
+
107
+ The following methods are available on a Queue:
108
+
109
+ * enqueue(push)
110
+ * dequeue(shift)
111
+ * peek
112
+ * length
113
+ * empty?
114
+
115
+ Examples:
116
+ q.enqueue :first
117
+ q.push :second
118
+ q.peek #=> :first
119
+ q.length #=> 2
120
+ q.empty? #=> false
121
+ q.dequeue #=> :first
122
+ q.shift #=> :second
123
+ q.empty? #=> true
124
+
125
+
126
+ ==== Priority Queue
127
+
128
+ PriorityQueue is special form of Queue (PriorityQueue inherits from Queue).
129
+ In opposite to simple Queue, in PriorityQueue each element is associated with a "priority".
130
+ More: {Priority Queue}[http://en.wikipedia.org/wiki/Priority_queue]
131
+
132
+ Creating new Priority Queue (implemented as BinaryHeap)
133
+
134
+ q = PriorityQueue.new
135
+
136
+ Examples:
137
+ q.push(:important, 3)
138
+ q.push(:very_important, 5)
139
+ q.push(:nevermind, 1)
140
+
141
+ q.shift #=> :very_important
142
+ q.peek #=> :important
143
+ q.length #=> 2
144
+ q.shift #=> :important
145
+ q.peek #=> :nevermind
146
+
147
+
148
+
149
+ === Lists
150
+
151
+ === List
152
+
153
+ List is an ordered collection of values. Each element of list has pointer to the next
154
+ element (last element points to nil).
155
+ More: {List}[http://en.wikipedia.org/wiki/List_(data_structure)]
156
+
157
+ Creating new List
158
+ l = List.new(1)
159
+ l.append(2)
160
+ or
161
+ arr = [1,2,3,4]
162
+ list = List.from_array(arr)
163
+
164
+ Examples:
165
+
166
+ Simple operation on lists
167
+ list.length #=> 4
168
+ list.append(5).to_a #=> [1,2,3,4,5]
169
+ list.prepend(0).to_a #=> [0,1,2,3,4,5]
170
+ list.remove(list.head).to_a #=> [1,2,3,4,5]
171
+ list.shift #=> 1
172
+
173
+ Accessing first and last element
174
+ list.head.data #=> 2
175
+ list.tail.data #=> 5
176
+
177
+ list.first #=> 2
178
+ list.last #=> 5
179
+
180
+ Reversing
181
+ list.reverse!.to_a #=> [5,4,3,1,0]
182
+
183
+ Enumerable methods are also available
184
+
185
+ list.map{ |e| e.data } #=> [1,2,3,4]
186
+ list.inject(0){ |mem, var| mem = mem + var.data } #=> 10
187
+
188
+ Other operations
189
+ * insert_before
190
+ * insert_after
191
+ * zip?
192
+ * merge
193
+
194
+
195
+ ==== Ring
196
+
197
+ Ring is a Cyclic List where the last element(head) points to the first element(tail).
198
+ More: {Ring}[http://en.wikipedia.org/wiki/Ring_(data_structure)]
199
+
200
+ Creating new Ring
201
+ ring = Ring.from_array([1,2,3,4,5,6,7])
202
+
203
+ Examples:
204
+ ring.looped? #=> true
205
+ ring.cycle_size #=> 7
206
+ ring.eliminate_by(2) #=> 1
207
+
208
+
209
+
210
+ === Trees
211
+
212
+ ==== Tree
213
+
214
+ A tree is a data structure consisting of nodes organised as a hierarchy.
215
+ More: {Tree}[http://en.wikipedia.org/wiki/Tree_(data_structure)]
216
+
217
+ Building Tree
218
+
219
+ t = Tree.new(2)
220
+ c1 = t << 5
221
+ c2 = t << 8
222
+ t << 9
223
+
224
+ c1 << 4
225
+ c1 << 10
226
+ c3 = c2 << 3
227
+
228
+ Examples:
229
+ t.leaf? #=> false
230
+ c3.leaf? #=> true
231
+
232
+ t.height #=> 3
233
+ t.width #=> 3
234
+ t.leaf_count #=> 4
235
+
236
+ t.levels #=> {1=>1,2=>3, 3=>3}
237
+
238
+ Other methods
239
+ * get_leaves
240
+ * isometric?
241
+ * mirror!
242
+
243
+ Enumerable Module is included.
244
+ t.map{ |node| node.data } #=> [2,5,8,9,4,10,3]
245
+
246
+
247
+ ==== Binary Tree
248
+
249
+ BinaryTree is sublass of Tree class. In BinaryTree each node can have at most two children.
250
+ More: {BinaryTree}[http://en.wikipedia.org/wiki/Binary_tree]
251
+
252
+ Building tree
253
+ bin_tree = BinaryTree.new
254
+ [2,5,8,9,11,12,14].each{|x| bin_tree.insert(x)} #build complete binary Tree
255
+
256
+ Accessors defined on BinaryTree object:
257
+ bin_tree.left #=> 5
258
+ bin_tree.right #=> 8
259
+
260
+
261
+ ==== BinarySearchTree
262
+
263
+ BST is Binary Tree which has the following properties:
264
+ * The left subtree of a node contains only nodes with keys less than the node's key.
265
+ * The right subtree of a node contains only nodes with keys greater than the node's key.
266
+ * Both the left and right subtrees must also be binary search trees.
267
+
268
+ Creating
269
+ bst = BinarySearchTree.from_array([8,1,5,2,7,6,3])
270
+
271
+ Examples
272
+ walker = TreeWalker.new(b)
273
+ walker.traverse(:inorder).must_equal [1,2,3,5,6,7,8]
274
+
275
+
276
+ ==== CompleteBinaryTree
277
+
278
+ A complete binary tree is a binary tree in which every level, except possibly
279
+ the last, is completely filled, and all nodes are as far left as possible.
280
+ CompleteBinaryTree is binary tree but does not inherit from Tree and BinaryTree class! Nodes are stored
281
+ internally in array.
282
+ More: {Complete Binary Tree}[http://en.wikipedia.org/wiki/Complete_binary_tree]
283
+
284
+ Creating
285
+ cbt = CompleteBinaryTree.new(1,2,3,4,5,6,7)
286
+
287
+ Examples
288
+ cbt.root #=> 1
289
+ cbt.left(0) #=> 2
290
+ cbt.right(0) #=> 3
291
+ cbt.parent(1) #=> 0
292
+
293
+ cbt.left_index(0) #=> 1
294
+ cbt.right_index(1) #=> 4
295
+ cpt.parent_index(1) #=> 0
296
+
297
+
298
+ ==== Binary Heap
299
+
300
+ BinaryHeap is Complete Binary Tree in which every node satisfies heap property. Binary Heap allows very fast
301
+ access to maximum or minimum element of the tree.
302
+ More: {Binary Heap}[http://en.wikipedia.org/wiki/Binary_heap]
303
+
304
+ Creating
305
+
306
+ Maximum Binary Heap
307
+ max_heap = BinaryHeap.new(9,8,4,5,11,6)
308
+ or
309
+ max_heap = BinaryHeap.max(9,8,4,5,11,6)
310
+
311
+ Minimum Binary Heap
312
+ min_heap = BinaryHeap.min(9,8,4,5,11,6)
313
+ or
314
+ BinaryHeap.new(9,8,4,5,11,6){|parent,child| parent < child}
315
+
316
+ You can set heap relation by passing block to BinaryHeap constructor.
317
+
318
+ Examples
319
+ max_heap.shift #returns max element (11)
320
+ max_heap.to_a #=> [9,8,6,5,4]
321
+ max_heap.insert 15
322
+ max_heap.shift #=> 15
323
+
324
+ min_heap.shift #returns min element (4)
325
+
326
+
327
+ ==== Trie
328
+
329
+ Trie is an ordered tree data structure which allows very quick search: O(k), where k is word length.
330
+ More: {Trie}[http://en.wikipedia.org/wiki/Trie]
331
+
332
+ Creating
333
+ trie = Trie.new
334
+
335
+ Examples
336
+ trie.insert("thing",true);
337
+ trie.find("thing") # => true
338
+
339
+
340
+ ==== Traversing Tree
341
+
342
+ b= BinaryTree.new
343
+ [2,5,8,9,11,12,14].each{|x| b.insert(x)}
344
+
345
+ walker = TreeWalker.new(b)
346
+
347
+ Iterating in postorder
348
+ walker.traverse(:postorder) #=> [9,11,5,12,14,8,2]
349
+ Iterating in inorder
350
+ walker.traverse(:inorder) #=> [9,5,11,2,12,8,14]
351
+ Iterating in preorder
352
+ walker.traverse(:preorder) #=> [2,5,9,11,8,12,14]
353
+
354
+ Iterating in BFS order
355
+ walker.each{ |x| x } #=> [2,5,8,9,11,12,14]
356
+
357
+
358
+ You can also pass block to traverse method
359
+ walker.traverse(:inorder){|n| p n.data**2}
360
+
361
+ If you want to change value of tree nodes, use recalculate! method
362
+ walker.recalculate!(b,:preorder,0){|x,memo| memo = memo+x.data}
363
+
364
+
365
+
366
+ === Graphs
367
+
368
+ A graph data structure consists of a finite (and possibly mutable) set of
369
+ ordered pairs, called edges or arcs, of certain entities called nodes or
370
+ vertices.
371
+ More: {Graph}[http://en.wikipedia.org/wiki/Graph_(data_structure)]
372
+
373
+
374
+ ====Graph
375
+
376
+ Creating new Graph
377
+
378
+ edges = []
379
+ edges << Edge.new('Lukas','Marc')
380
+ edges << Edge.new('Lukas','Tom')
381
+ edges << Edge.new('Marc','Jack')
382
+ edges << Edge.new('Tom','Marc')
383
+
384
+ New graph implemented as Triangular Matrix
385
+ graph = Graph.create(edges)
386
+
387
+ New graph implemented as Matrix
388
+ graph = Graph.new(edges,:matrix)
389
+
390
+ New graph implemented as Adjency List
391
+ graph = Graph.new(edges,:list)
392
+
393
+ Examples:
394
+
395
+ graph.vertex_size #=> 4
396
+ graph.degree("Marc") #=> 3
397
+ graph.edge?("Marc","Tom") #=> true
398
+ graph.edge?("Tom","Jack") #=> false
399
+ graph.add_edges([Edge.new("Marc","Kate")])
400
+ graph.remove("Marc","Jack")
401
+ graph.neighbors('Tom') #=> ["Marc","Lucas"]
402
+
403
+ Iterating
404
+ graph.each_edge{|e| p e}
405
+ graph.each_vertex{ |v| p v }
406
+
407
+
408
+ ==== Digraph
409
+
410
+ Creating Directed Weighted Graph is simple like that:
411
+
412
+ edges = []
413
+
414
+ edges << Edge.new(:A,:C,5)
415
+ edges << Edge.new(:A,:D,3)
416
+ edges << Edge.new(:A,:G,14)
417
+ edges << Edge.new(:C,:E,3)
418
+ edges << Edge.new(:C,:F,2)
419
+ edges << Edge.new(:D,:C,11)
420
+ edges << Edge.new(:D,:E,7)
421
+ edges << Edge.new(:D,:G,6)
422
+ edges << Edge.new(:G,:E,7)
423
+ edges << Edge.new(:E,:B,5)
424
+ edges << Edge.new(:G,:B,6)
425
+ edges << Edge.new(:F,:B,7)
426
+
427
+ wdigraph = Digraph.create(edges)
428
+
429
+ Examples
430
+ wdigraph.get_edge(:D,:C).weight #=> 11
431
+ wdigraph.degree(:E) #=> 4
432
+ wdigraph.in_degree(:E) #=> 3
433
+ wdigraph.out_degree(:E) #=> 1
434
+ wdigraph.bfs(:A) #=> [:A, :C, :D, :G, :E, :F, :B]
435
+
436
+
437
+
438
+
439
+ === Matrixes
440
+
441
+ ==== Array2D
442
+
443
+ Simple two dimensional array(matrix).
444
+ Array2D extends automatically like simple Array.
445
+
446
+ Creating
447
+ discrete_matrix = Array2D.new(2,0)
448
+
449
+ Examples
450
+ discrete_matrix.to_a #=> [[0,0],[0,0]]
451
+ discrete_matrix[3,3] #=> 0
452
+
453
+
454
+
455
+ ==== ExpandableArray
456
+
457
+ arr = ExpandableArray.new(0,0)
458
+ arr[4] = 1 #=> [0,0,0,0,4]
459
+
460
+
461
+ ==== TriMatrix
462
+ Triangular matrix is a special kind of matrix where M[x,y] = M[y,x].
463
+ More: {Triangular Matrixe}[http://en.wikipedia.org/wiki/Triangular_matrix]
464
+
465
+ Creating
466
+ tri_matrix = TriMatrix.new
467
+ tri_matrix[0,1] = true
468
+ tri_matrix[0,2] = true
469
+
470
+ Examples
471
+ tri_matrix[0,1] == tri_matrix[1,0] #=> true
472
+
473
+
474
+
475
+ === Sets
476
+
477
+ ==== OrderedSet
478
+ OrderedSet is a set whose elements are ordered. In opposite to Array duplicates
479
+ are not allowed.
480
+
481
+ Creating new Ordered Set
482
+ set = OrderedSet.new
483
+
484
+ Examples
485
+ set.push(:first) #=> 0
486
+ set.push(:second) #=> 1
487
+ set.index(:first) #=> 0
488
+ set.to_a #=> [:first, :second]
489
+
490
+