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
@@ -13,7 +13,7 @@ module DS
13
13
  end
14
14
  end
15
15
 
16
- #Create new queue.Internaly uses list to store elements.
16
+ #Create new queue. Internaly uses list to store elements.
17
17
  def Queue.create
18
18
  new(:list)
19
19
  end
@@ -0,0 +1,32 @@
1
+ module DS
2
+ class OrderedSet
3
+ attr_reader :size
4
+
5
+ #Creates new Ordered Set.
6
+ def initialize
7
+ @size = 0
8
+ @store = {}
9
+ end
10
+
11
+ #Adds new element to set
12
+ def push e
13
+ if index = @store[e]
14
+ index
15
+ else
16
+ @size = @size+1
17
+ @store[e] = @size-1
18
+ end
19
+ end
20
+
21
+ #Returns element index.
22
+ def index e
23
+ @store[e]
24
+ end
25
+
26
+ def to_a
27
+ @store.sort{|a,b| a[1]<=>b[1]}.map{|e| e.first}
28
+ end
29
+
30
+
31
+ end
32
+ end
@@ -14,10 +14,18 @@ module DS
14
14
  heapify!
15
15
  end
16
16
 
17
- def set_relation(&relation)
18
- @relation = relation
17
+ #Create new Maximum Heap from args.
18
+ def BinaryHeap.max(*args)
19
+ new(*args){|parent,child| parent >= child}
19
20
  end
20
21
 
22
+ #Create new Minimum Heap from args.
23
+ def BinaryHeap.min(*args)
24
+ new(*args){|parent,child| parent < child}
25
+ end
26
+
27
+
28
+ #Evaluates Heap relation.
21
29
  def relation(parent,child)
22
30
  @relation.call(@data[parent],@data[child])
23
31
  end
@@ -30,6 +38,34 @@ module DS
30
38
  end
31
39
  end
32
40
 
41
+ #Maintains heap condition for i node.
42
+ #O(log)
43
+ def heapify(i)
44
+ left = left_index(i)
45
+ left = nil if left >= @data.size
46
+
47
+ right = right_index(i)
48
+ right = nil if right >= @data.size
49
+
50
+ largest = [i,left,right].compact.sort{|x,y| relation(x,y)?-1:1}.first
51
+
52
+ if largest != i
53
+ @data[i], @data[largest] = @data[largest], @data[i]
54
+ heapify(largest)
55
+ end
56
+ end
57
+
58
+ #Removes element from heap maintaining heap relation.
59
+ def shift
60
+ if @data.size > 0
61
+ result = @data.shift
62
+ return result if @data.size.zero?
63
+ @data.unshift @data.pop
64
+ heapify(0)
65
+ end
66
+ result
67
+ end
68
+
33
69
  #Inserts new value to heap maintaining the heap relation.
34
70
  #Returns heap itself.
35
71
  #O(log)
@@ -46,21 +82,16 @@ module DS
46
82
  self
47
83
  end
48
84
 
49
- #Maintain heap condition for i node.
50
- #O(log)
51
- def heapify(i)
52
- left = left_index(i)
53
- left = nil if left >= @data.size
54
85
 
55
- right = right_index(i)
56
- right = nil if right >= @data.size
57
86
 
58
- largest = [i,left,right].compact.sort{|x,y| relation(x,y)?-1:1}.first
87
+ def length
88
+ @data.size
89
+ end
59
90
 
60
- if largest != i
61
- @data[i], @data[largest] = @data[largest], @data[i]
62
- heapify(largest)
63
- end
91
+ alias :size :length
92
+
93
+ def empty?
94
+ @data.empty?
64
95
  end
65
96
 
66
97
  def to_a
@@ -1,6 +1,7 @@
1
1
  module DS
2
2
  class BinarySearchTree < BinaryTree
3
3
 
4
+ #Creates new search tree from array.
4
5
  def self.from_array(arr)
5
6
  tree = BinarySearchTree.new(arr.shift)
6
7
  arr.each do |e|
@@ -9,6 +10,7 @@ module DS
9
10
  tree
10
11
  end
11
12
 
13
+ #Inserts new element.
12
14
  def insert x
13
15
  if x > @data and right.nil?
14
16
  self.right = BinarySearchTree.new(x)
@@ -21,7 +23,7 @@ module DS
21
23
  end
22
24
  end
23
25
 
24
-
26
+ #Checks if tree is valid Binary Search Tree.
25
27
  def BinarySearchTree.valid?(other)
26
28
  walker = TreeWalker.new(other)
27
29
  walker.traverse(:inorder)
@@ -1,6 +1,14 @@
1
1
  module DS
2
2
  class BinaryTree < Tree
3
3
 
4
+ #Inserts a new subtree.
5
+ def << (value)
6
+ subtree = BinaryTree.new(value)
7
+ @children << subtree
8
+ return subtree
9
+ end
10
+
11
+ #Returns left subtree
4
12
  def left
5
13
  if @children.empty?
6
14
  nil
@@ -9,10 +17,12 @@ module DS
9
17
  end
10
18
  end
11
19
 
20
+ #Sets left subtree
12
21
  def left=(value)
13
22
  @children[0] = value
14
23
  end
15
24
 
25
+ #Returns right subtree
16
26
  def right
17
27
  if @children.empty?
18
28
  nil
@@ -21,15 +31,11 @@ module DS
21
31
  end
22
32
  end
23
33
 
34
+ #Sets right subtree
24
35
  def right=(value)
25
36
  @children[1] = value
26
37
  end
27
38
 
28
- def << (value)
29
- subtree = BinaryTree.new(value)
30
- @children << subtree
31
- return subtree
32
- end
33
39
 
34
40
  #Inserts new element in BSF order
35
41
  def insert(x)
@@ -1,6 +1,7 @@
1
1
  module DS
2
2
  class CompleteBinaryTree
3
-
3
+
4
+ #Creates new tree (stored in array).
4
5
  def initialize(*args)
5
6
  if args.empty?
6
7
  @data = []
@@ -9,41 +10,48 @@ module DS
9
10
  end
10
11
  end
11
12
 
13
+ #Inserts new element.
12
14
  def << (value)
13
15
  @data.push value
14
16
  end
15
17
 
18
+ #Returns root element.
16
19
  def root
17
20
  @data.first
18
21
  end
19
22
 
20
- def children(value)
21
- i = @data.index(value)
22
- return [@data[2*i+1], @data[2*i+2]]
23
- end
24
-
25
- def right value
26
- children(value)[1]
27
- end
28
-
29
- def left value
30
- children(value).first
31
- end
32
-
23
+ #Returns index of left child.
33
24
  def left_index(i)
34
25
  2*i+1
35
26
  end
36
27
 
28
+ #Returns index of right child.
37
29
  def right_index(i)
38
30
  2*i+2
39
31
  end
40
32
 
33
+ #Returns index of parent.
41
34
  def parent_index(i)
42
35
  (i+1)/2 - 1
43
36
  end
44
37
 
45
- def parent value
46
- i = @data.index(value)
38
+ #Returns children of node i.
39
+ def children(i)
40
+ [@data[left_index(i)], @data[right_index(i)]]
41
+ end
42
+
43
+ #Returns left child of node i.
44
+ def left(i)
45
+ @data[left_index(i)]
46
+ end
47
+
48
+ #Returns left child of node i.
49
+ def right(i)
50
+ @data[right_index(i)]
51
+ end
52
+
53
+ #Returns parent of node i.
54
+ def parent(i)
47
55
  @data[parent_index(i)]
48
56
  end
49
57
 
@@ -64,7 +64,7 @@ module DS
64
64
  end
65
65
 
66
66
  #Returns subtree height.
67
- def h(tree)
67
+ def self.h(tree)
68
68
  unless tree.leaf?
69
69
  tree.children.map{|t| h(t) }.max + 1
70
70
  else
@@ -74,12 +74,12 @@ module DS
74
74
 
75
75
  #Returns tree height.
76
76
  def height
77
- h(self)
77
+ Tree.h(self)
78
78
  end
79
79
 
80
80
  #Returns node which lies closest to the root.
81
81
  def lowest_height
82
- find{ |node| node.leaf? }.data
82
+ find{ |node| node.leaf? }
83
83
  end
84
84
 
85
85
  #Mirrors tree.
@@ -32,7 +32,7 @@ module DS
32
32
  walk(tree,:postorder,&block)
33
33
  when :preorder
34
34
  walk(tree,:preorder, &block)
35
- when (:inorder)
35
+ when :inorder
36
36
  raise ArgumentError unless tree.kind_of? BinaryTree
37
37
  walk(tree,order,&block)
38
38
  end
@@ -65,6 +65,7 @@ module DS
65
65
 
66
66
 
67
67
 
68
+ #Traverses tree with tracking level.
68
69
  def traverse_with_h(tree,height=nil,&block)
69
70
 
70
71
  tree.children.each do |t|
@@ -73,8 +74,6 @@ module DS
73
74
 
74
75
  if block_given?
75
76
  yield tree, height
76
- else
77
- @visited << tree.data
78
77
  end
79
78
 
80
79
  end
@@ -103,9 +102,9 @@ module DS
103
102
 
104
103
  when :inorder
105
104
  raise ArgumentError unless self.tree.is_a? BinaryTree
106
- recalculate!(tree.left,order,&block)
105
+ recalculate!(tree.left,order,memo,&block)
107
106
 
108
- tree.data = yield tree.memo
107
+ tree.data = yield tree, memo
109
108
  memo = tree.data
110
109
 
111
110
  recalculate!(tree.right,order,memo,&block)
@@ -115,7 +114,6 @@ module DS
115
114
  end
116
115
 
117
116
 
118
-
119
117
  #Summarize tree
120
118
  def summarize(direction=:bottomup)
121
119
 
@@ -126,6 +124,9 @@ module DS
126
124
  when :topdown
127
125
  recalculate!(self.tree,:preorder,0){|x,memo| memo = memo+x.data}
128
126
  self.tree
127
+ when :inorder
128
+ recalculate!(self.tree,:inorder,0){|x,memo| memo = memo+x.data if x.data and memo}
129
+ self.tree
129
130
  end
130
131
  end
131
132
 
@@ -12,25 +12,24 @@ module DS
12
12
  @data = value
13
13
  end
14
14
 
15
- def insert(s, value)
16
- priv_insert(s.scan(/./), value)
15
+ def insert(s, value=true)
16
+ letters = s.scan(/./)
17
+ raise ArgumentError, "Not allowed symbol." unless (letters - alphabet).empty?
18
+ priv_insert(letters, value)
17
19
  end
18
20
 
19
21
  def find(s)
20
- priv_find(s.scan(/./))
22
+ letters = s.scan(/./)
23
+ raise ArgumentError, "Not allowed symbol." unless (letters - alphabet).empty?
24
+ priv_find(letters)
21
25
  end
22
26
 
23
27
  protected
24
28
  def key(chr)
25
- raise ArgumentError, "Argument chr is nil" unless chr
26
29
  k=alphabet.index(chr)
27
- if k.nil?
28
- raise ArgumentError, "Character not found"
29
- else
30
- k
31
- end
32
30
  end
33
31
 
32
+
34
33
  def priv_insert(s, value)
35
34
  if s.empty?
36
35
  @data = value
@@ -47,21 +46,16 @@ module DS
47
46
  end
48
47
 
49
48
  def priv_find(search)
50
- if @children[0]
51
- @children[0].value
49
+ if search.empty?
50
+ @data
52
51
  else
53
- if search.empty?
54
- @data
52
+ index = key(search.first)
53
+ if @children[index]
54
+ @children[index].priv_find(search[1..-1])
55
55
  else
56
- index = key(search.first)
57
- if @children[index]
58
- @children[index].priv_find(search[1..-1])
59
- else
60
- nil
61
- end
56
+ nil
62
57
  end
63
58
  end
64
59
  end
65
-
66
60
  end
67
61
  end
@@ -1,3 +1,3 @@
1
1
  module DS
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,8 +1,17 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "/test/"
5
+ end
6
+ end
7
+
8
+
1
9
  require 'rubygems'
10
+ gem 'minitest'
2
11
  require 'minitest/autorun'
3
12
  require "minitest/benchmark" if ENV['BENCH']
4
- require 'redgreen'
13
+ require "minitest/pride"
5
14
 
6
- require 'ds'
7
- include DS
8
15
 
16
+ require './lib/ds.rb'
17
+ include DS
@@ -13,9 +13,14 @@ describe Array2D do
13
13
  @zero_matrix[0,0].must_equal 0
14
14
  end
15
15
 
16
- it "#to_a should return matrix flattened to array." do
16
+ it "#flatten should return matrix flattened to array." do
17
+ @zero_matrix.flatten.must_be_instance_of Array
18
+ @zero_matrix.flatten.must_equal [0,0,0,0]
19
+ end
20
+
21
+ it "#to_a should return internal representation (nested arrays)" do
17
22
  @zero_matrix.to_a.must_be_instance_of Array
18
- @zero_matrix.to_a.must_equal [0,0,0,0]
23
+ @zero_matrix.to_a.must_equal [[0,0],[0,0]]
19
24
  end
20
25
 
21
26
  it "#should extend magically." do
@@ -35,9 +40,14 @@ describe Array2D do
35
40
  refute @discrete_matrix[0,0]
36
41
  end
37
42
 
38
- it "#to_a should return matrix flattened to array." do
43
+ it "#flatten should return matrix flattened to array." do
44
+ @discrete_matrix.flatten.must_be_instance_of Array
45
+ @discrete_matrix.flatten.must_equal [false,false,false,false,false,false,false,false,false]
46
+ end
47
+
48
+ it "#to_a should return internal representation (nested arrays)" do
39
49
  @discrete_matrix.to_a.must_be_instance_of Array
40
- @discrete_matrix.to_a.must_equal [false,false,false,false,false,false,false,false,false]
50
+ @discrete_matrix.to_a.must_equal [[false,false,false],[false,false,false],[false,false,false]]
41
51
  end
42
52
 
43
53
  it "#should extend magically." do