ds 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.rdoc +490 -0
- data/Rakefile +1 -1
- data/lib/ds.rb +17 -19
- data/lib/ds/ext/array_x.rb +35 -0
- data/lib/ds/graphs/graph.rb +36 -32
- data/lib/ds/graphs/graph_as_matrix.rb +22 -23
- data/lib/ds/graphs/graph_as_tri_matrix.rb +1 -2
- data/lib/ds/lists/list.rb +18 -21
- data/lib/ds/matrixes/array_2d.rb +4 -0
- data/lib/ds/pair.rb +14 -0
- data/lib/ds/queues/priority_queue.rb +36 -0
- data/lib/ds/queues/queue.rb +1 -1
- data/lib/ds/sets/ordered_set.rb +32 -0
- data/lib/ds/trees/binary_heap.rb +45 -14
- data/lib/ds/trees/binary_search_tree.rb +3 -1
- data/lib/ds/trees/binary_tree.rb +11 -5
- data/lib/ds/trees/complete_binary_tree.rb +24 -16
- data/lib/ds/trees/tree.rb +3 -3
- data/lib/ds/trees/tree_walker.rb +7 -6
- data/lib/ds/trees/trie.rb +14 -20
- data/lib/ds/version.rb +1 -1
- data/test/help.rb +12 -3
- data/test/test_array2d.rb +14 -4
- data/test/test_array_x.rb +27 -7
- data/test/test_binary_heap.rb +19 -1
- data/test/test_binary_search_tree.rb +8 -1
- data/test/test_binary_tree.rb +7 -1
- data/test/test_complete_binary_tree.rb +34 -6
- data/test/test_digraph.rb +1 -1
- data/test/test_graph.rb +10 -1
- data/test/test_list.rb +59 -2
- data/test/test_ordered_set.rb +28 -0
- data/test/test_pair.rb +26 -0
- data/test/test_priority_queue.rb +134 -0
- data/test/test_queue.rb +3 -3
- data/test/test_stack.rb +0 -2
- data/test/test_tree.rb +32 -9
- data/test/test_tree_walker.rb +22 -0
- data/test/test_trie.rb +20 -2
- metadata +13 -81
- data/doc/Array.html +0 -264
- data/doc/DS.html +0 -292
- data/doc/DS/Array2D.html +0 -345
- data/doc/DS/BinaryHeap.html +0 -493
- data/doc/DS/BinarySearchTree.html +0 -313
- data/doc/DS/BinaryTree.html +0 -433
- data/doc/DS/CompleteBinaryTree.html +0 -550
- data/doc/DS/CyclicList.html +0 -234
- data/doc/DS/Digraph.html +0 -299
- data/doc/DS/Edge.html +0 -283
- data/doc/DS/ExpandableArray.html +0 -316
- data/doc/DS/Graph.html +0 -739
- data/doc/DS/GraphAsList.html +0 -361
- data/doc/DS/GraphAsMatrix.html +0 -633
- data/doc/DS/GraphAsTriMatrix.html +0 -274
- data/doc/DS/List.html +0 -1263
- data/doc/DS/ListElement.html +0 -344
- data/doc/DS/Queue.html +0 -517
- data/doc/DS/Ring.html +0 -323
- data/doc/DS/Stack.html +0 -407
- data/doc/DS/Tree.html +0 -770
- data/doc/DS/TreeWalker.html +0 -561
- data/doc/DS/TriMatrix.html +0 -338
- data/doc/created.rid +0 -25
- data/doc/ds/graphs/digraph_rb.html +0 -52
- data/doc/ds/graphs/edge_rb.html +0 -52
- data/doc/ds/graphs/graph_as_list_rb.html +0 -52
- data/doc/ds/graphs/graph_as_matrix_rb.html +0 -52
- data/doc/ds/graphs/graph_as_tri_matrix_rb.html +0 -52
- data/doc/ds/graphs/graph_rb.html +0 -52
- data/doc/ds/lists/cyclic_list_rb.html +0 -52
- data/doc/ds/lists/list_element_rb.html +0 -52
- data/doc/ds/lists/list_rb.html +0 -52
- data/doc/ds/lists/ring_rb.html +0 -52
- data/doc/ds/matrixes/array_2d_rb.html +0 -52
- data/doc/ds/matrixes/expandable_array_rb.html +0 -52
- data/doc/ds/matrixes/tri_matrix_rb.html +0 -52
- data/doc/ds/queues/queue_rb.html +0 -52
- data/doc/ds/stacks/stack_rb.html +0 -52
- data/doc/ds/trees/binary_heap_rb.html +0 -52
- data/doc/ds/trees/binary_search_tree_rb.html +0 -52
- data/doc/ds/trees/binary_tree_rb.html +0 -52
- data/doc/ds/trees/complete_binary_tree_rb.html +0 -52
- data/doc/ds/trees/tree_rb.html +0 -52
- data/doc/ds/trees/tree_walker_rb.html +0 -52
- data/doc/ds/version_rb.html +0 -52
- data/doc/ds_rb.html +0 -98
- data/doc/ext/ext_rb.html +0 -52
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/index.html +0 -375
- data/doc/js/darkfish.js +0 -116
- data/doc/js/jquery.js +0 -32
- data/doc/js/quicksearch.js +0 -114
- data/doc/js/thickbox-compressed.js +0 -10
- data/doc/rdoc.css +0 -763
- data/lib/ds/ext/ext.rb +0 -36
- data/lib/ds/graphs/graph_as_list.rb +0 -48
data/lib/ds/queues/queue.rb
CHANGED
@@ -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
|
data/lib/ds/trees/binary_heap.rb
CHANGED
@@ -14,10 +14,18 @@ module DS
|
|
14
14
|
heapify!
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
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
|
-
|
87
|
+
def length
|
88
|
+
@data.size
|
89
|
+
end
|
59
90
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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)
|
data/lib/ds/trees/binary_tree.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
46
|
-
|
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
|
|
data/lib/ds/trees/tree.rb
CHANGED
@@ -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? }
|
82
|
+
find{ |node| node.leaf? }
|
83
83
|
end
|
84
84
|
|
85
85
|
#Mirrors tree.
|
data/lib/ds/trees/tree_walker.rb
CHANGED
@@ -32,7 +32,7 @@ module DS
|
|
32
32
|
walk(tree,:postorder,&block)
|
33
33
|
when :preorder
|
34
34
|
walk(tree,:preorder, &block)
|
35
|
-
when
|
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
|
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
|
|
data/lib/ds/trees/trie.rb
CHANGED
@@ -12,25 +12,24 @@ module DS
|
|
12
12
|
@data = value
|
13
13
|
end
|
14
14
|
|
15
|
-
def insert(s, value)
|
16
|
-
|
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
|
-
|
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
|
51
|
-
@
|
49
|
+
if search.empty?
|
50
|
+
@data
|
52
51
|
else
|
53
|
-
|
54
|
-
|
52
|
+
index = key(search.first)
|
53
|
+
if @children[index]
|
54
|
+
@children[index].priv_find(search[1..-1])
|
55
55
|
else
|
56
|
-
|
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
|
data/lib/ds/version.rb
CHANGED
data/test/help.rb
CHANGED
@@ -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
|
13
|
+
require "minitest/pride"
|
5
14
|
|
6
|
-
require 'ds'
|
7
|
-
include DS
|
8
15
|
|
16
|
+
require './lib/ds.rb'
|
17
|
+
include DS
|
data/test/test_array2d.rb
CHANGED
@@ -13,9 +13,14 @@ describe Array2D do
|
|
13
13
|
@zero_matrix[0,0].must_equal 0
|
14
14
|
end
|
15
15
|
|
16
|
-
it "#
|
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 "#
|
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
|