collection_utils 0.1.0 → 1.0.0
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 +4 -4
- data/.travis.yml +1 -0
- data/README.md +3 -3
- data/documentation/CollectionUtils/Heap.html +1276 -0
- data/documentation/CollectionUtils/HeapUtils/MaxHeap.html +490 -0
- data/documentation/CollectionUtils/HeapUtils/MinHeap.html +490 -0
- data/documentation/CollectionUtils/HeapUtils.html +117 -0
- data/documentation/CollectionUtils/Queue.html +749 -0
- data/documentation/CollectionUtils/Stack.html +674 -0
- data/documentation/CollectionUtils/TreeUtils/BST.html +515 -0
- data/documentation/CollectionUtils/TreeUtils.html +117 -0
- data/documentation/CollectionUtils.html +134 -0
- data/documentation/_index.html +211 -0
- data/documentation/class_list.html +51 -0
- data/documentation/css/common.css +1 -0
- data/documentation/css/full_list.css +58 -0
- data/documentation/css/style.css +492 -0
- data/documentation/file.README.html +124 -0
- data/documentation/file_list.html +56 -0
- data/documentation/frames.html +17 -0
- data/documentation/index.html +124 -0
- data/documentation/js/app.js +248 -0
- data/documentation/js/full_list.js +216 -0
- data/documentation/js/jquery.js +4 -0
- data/documentation/method_list.html +355 -0
- data/documentation/top-level-namespace.html +110 -0
- data/lib/collection_utils/heap.rb +251 -0
- data/lib/collection_utils/heap_utils/max_heap.rb +12 -12
- data/lib/collection_utils/heap_utils/min_heap.rb +13 -11
- data/lib/collection_utils/queue.rb +21 -0
- data/lib/collection_utils/stack.rb +25 -2
- data/lib/collection_utils/tree_utils/bst.rb +69 -0
- data/lib/collection_utils/tree_utils.rb +5 -0
- data/lib/collection_utils/version.rb +1 -1
- metadata +28 -3
- data/lib/collection_utils/heap_utils/heap.rb +0 -97
@@ -0,0 +1,110 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8">
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
+
<title>
|
7
|
+
Top Level Namespace
|
8
|
+
|
9
|
+
— Documentation by YARD 0.9.9
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
|
16
|
+
|
17
|
+
<script type="text/javascript" charset="utf-8">
|
18
|
+
pathId = "";
|
19
|
+
relpath = '';
|
20
|
+
</script>
|
21
|
+
|
22
|
+
|
23
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
24
|
+
|
25
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
26
|
+
|
27
|
+
|
28
|
+
</head>
|
29
|
+
<body>
|
30
|
+
<div class="nav_wrap">
|
31
|
+
<iframe id="nav" src="class_list.html?1"></iframe>
|
32
|
+
<div id="resizer"></div>
|
33
|
+
</div>
|
34
|
+
|
35
|
+
<div id="main" tabindex="-1">
|
36
|
+
<div id="header">
|
37
|
+
<div id="menu">
|
38
|
+
|
39
|
+
<a href="_index.html">Index</a> »
|
40
|
+
|
41
|
+
|
42
|
+
<span class="title">Top Level Namespace</span>
|
43
|
+
|
44
|
+
</div>
|
45
|
+
|
46
|
+
<div id="search">
|
47
|
+
|
48
|
+
<a class="full_list_link" id="class_list_link"
|
49
|
+
href="class_list.html">
|
50
|
+
|
51
|
+
<svg width="24" height="24">
|
52
|
+
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
53
|
+
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
54
|
+
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
55
|
+
</svg>
|
56
|
+
</a>
|
57
|
+
|
58
|
+
</div>
|
59
|
+
<div class="clear"></div>
|
60
|
+
</div>
|
61
|
+
|
62
|
+
<div id="content"><h1>Top Level Namespace
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
</h1>
|
67
|
+
<div class="box_info">
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
</div>
|
80
|
+
|
81
|
+
<h2>Defined Under Namespace</h2>
|
82
|
+
<p class="children">
|
83
|
+
|
84
|
+
|
85
|
+
<strong class="modules">Modules:</strong> <span class='object_link'><a href="CollectionUtils.html" title="CollectionUtils (module)">CollectionUtils</a></span>
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
</p>
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
</div>
|
101
|
+
|
102
|
+
<div id="footer">
|
103
|
+
Generated on Thu Jul 20 01:32:56 2017 by
|
104
|
+
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
105
|
+
0.9.9 (ruby-2.0.0).
|
106
|
+
</div>
|
107
|
+
|
108
|
+
</div>
|
109
|
+
</body>
|
110
|
+
</html>
|
@@ -0,0 +1,251 @@
|
|
1
|
+
module CollectionUtils
|
2
|
+
class Heap
|
3
|
+
private
|
4
|
+
attr_accessor :heap
|
5
|
+
|
6
|
+
protected
|
7
|
+
|
8
|
+
|
9
|
+
#
|
10
|
+
# @return element which is at the root of the tree
|
11
|
+
def root
|
12
|
+
return @heap.first
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
# Traverse the tree in pre-order manner
|
17
|
+
#
|
18
|
+
# @param node current node being parsed
|
19
|
+
# @param [Array] arr which will contain the nodes of tree in pre-order manner
|
20
|
+
def pre_order(node, arr)
|
21
|
+
return if node.nil?
|
22
|
+
left = left(node[:index])
|
23
|
+
right = right(node[:index])
|
24
|
+
|
25
|
+
arr << node[:element]
|
26
|
+
pre_order(left, arr) unless left.nil?
|
27
|
+
pre_order(right, arr) unless right.nil?
|
28
|
+
|
29
|
+
return
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
# Traverse the tree in post-order manner
|
34
|
+
#
|
35
|
+
# @param node current node being parsed
|
36
|
+
# @param [Array] arr which will contain the nodes of tree in post-order manner
|
37
|
+
def post_order(node, arr)
|
38
|
+
return if node.nil?
|
39
|
+
left = left(node[:index])
|
40
|
+
right = right(node[:index])
|
41
|
+
|
42
|
+
post_order(left, arr) unless left.nil?
|
43
|
+
post_order(right, arr) unless right.nil?
|
44
|
+
arr << node[:element]
|
45
|
+
return
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
# Traverse the tree in in-order manner
|
50
|
+
#
|
51
|
+
# @param node current node being parsed
|
52
|
+
# @param [Array] arr which will contain the nodes of tree in in-order manner
|
53
|
+
def in_order(node, arr)
|
54
|
+
return if node.nil?
|
55
|
+
left = left(node[:index])
|
56
|
+
right = right(node[:index])
|
57
|
+
|
58
|
+
in_order(left, arr) unless left.nil?
|
59
|
+
arr << node[:element]
|
60
|
+
in_order(right, arr) unless right.nil?
|
61
|
+
return
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
# @param [Integer] parent is the index for which left child needs to be founded. Default is 0
|
66
|
+
# @return left_child and index of that left child
|
67
|
+
def left_child(parent = 0)
|
68
|
+
left = (2*parent + 1)
|
69
|
+
return nil, nil if @heap[left].nil?
|
70
|
+
return @heap[left], left
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
# @param [Integer] parent is the index for which right child needs to be founded. Default is 0
|
75
|
+
# @return right_child and index of that right child
|
76
|
+
def right_child(parent = 0)
|
77
|
+
right = (2*parent + 2)
|
78
|
+
return nil, nil if @heap[right].nil?
|
79
|
+
return @heap[right], right
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
# @param parent is the index for which left child is added. Default is 0
|
84
|
+
# @param node is the element which needs to be assigned to left of the parent
|
85
|
+
def assign_left(parent = 0, node)
|
86
|
+
left = (2*parent + 1)
|
87
|
+
node[:index] = left
|
88
|
+
@heap[left] = node
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
# @param parent is the index for which right child is added. Default is 0
|
93
|
+
# @param node is the element which needs to be assigned to right of the parent
|
94
|
+
def assign_right(parent = 0, node)
|
95
|
+
right = (2*parent + 2)
|
96
|
+
node[:index] = right
|
97
|
+
@heap[right] = node
|
98
|
+
end
|
99
|
+
|
100
|
+
# @param parent for which left child index needs to be found
|
101
|
+
# @return [Integer] left child index
|
102
|
+
def left_index(parent = 0)
|
103
|
+
left = (2*parent + 1)
|
104
|
+
end
|
105
|
+
|
106
|
+
# @param parent for which right child index needs to be found
|
107
|
+
# @return [Integer] right child index
|
108
|
+
def right_index(parent = 0)
|
109
|
+
right = (2*parent + 2)
|
110
|
+
end
|
111
|
+
|
112
|
+
# @param child for which parent needs to be found
|
113
|
+
# @return element and parent index
|
114
|
+
def parent(child = 0)
|
115
|
+
par = child/2
|
116
|
+
return @heap[par], par
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
public
|
121
|
+
#Constructors
|
122
|
+
def initialize(array = [])
|
123
|
+
@heap = []
|
124
|
+
array.each do |element|
|
125
|
+
push(element)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
#Public methods
|
130
|
+
|
131
|
+
# @param [Integer] parent index for which left child needs to be returned
|
132
|
+
# @return left child
|
133
|
+
def left(parent = 0)
|
134
|
+
left_child(parent).first
|
135
|
+
end
|
136
|
+
|
137
|
+
# @param [Integer] parent index for which right child needs to be returned
|
138
|
+
# @return right child
|
139
|
+
def right(parent = 0)
|
140
|
+
right_child(parent).first
|
141
|
+
end
|
142
|
+
|
143
|
+
# push element to heap
|
144
|
+
#
|
145
|
+
# @param element object that needs to be added to heap
|
146
|
+
def push(element)
|
147
|
+
@heap << {element: element, index: size}
|
148
|
+
end
|
149
|
+
|
150
|
+
# pop an element from heap
|
151
|
+
#
|
152
|
+
# @return removed element
|
153
|
+
def pop
|
154
|
+
element = @heap.pop
|
155
|
+
return element[:element]
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
# @return [Integer] size of heap
|
160
|
+
def size
|
161
|
+
return @heap.size
|
162
|
+
end
|
163
|
+
|
164
|
+
# @return [Boolean] heap's emptiness
|
165
|
+
def is_empty?
|
166
|
+
return size == 0
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
# bfs stands for breadth first search.
|
171
|
+
# @yield [element] this will take the element returned in bfs manner and execute the passed block.
|
172
|
+
# @example Create Heap and in bfs manner assign it to array
|
173
|
+
# arr = [1,2,3,4,5]
|
174
|
+
# heap = CollectionUtils::Heap.new(arr)
|
175
|
+
# x = []
|
176
|
+
# @heap.bfs do |element|
|
177
|
+
# x << element
|
178
|
+
# end
|
179
|
+
# #x = [1,2,3,4,5]
|
180
|
+
def bfs
|
181
|
+
queue = CollectionUtils::Queue.new
|
182
|
+
queue.enqueue(root)
|
183
|
+
while true do
|
184
|
+
node = queue.dequeue
|
185
|
+
next if node.nil?
|
186
|
+
left = left(node[:index])
|
187
|
+
right = right(node[:index])
|
188
|
+
yield(node[:element]) if block_given?
|
189
|
+
queue.enqueue(left) unless left.nil?
|
190
|
+
queue.enqueue(right) unless right.nil?
|
191
|
+
break if queue.is_empty?
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
# dfs stands for depth first search.
|
197
|
+
# @yield [element] this will take the element returned in dfs manner and execute the passed block.
|
198
|
+
# @example Create Heap and in dfs manner assign it to array
|
199
|
+
# arr = [1,2,3,4,5]
|
200
|
+
# heap = CollectionUtils::Heap.new(arr)
|
201
|
+
# x = []
|
202
|
+
# @heap.dfs do |element|
|
203
|
+
# x << element
|
204
|
+
# end
|
205
|
+
# #x = [1,3,2,5,4]
|
206
|
+
def dfs
|
207
|
+
stack = CollectionUtils::Stack.new
|
208
|
+
stack.push(root)
|
209
|
+
while true do
|
210
|
+
node = stack.pop
|
211
|
+
next if node.nil?
|
212
|
+
left = left(node[:index])
|
213
|
+
right = right(node[:index])
|
214
|
+
yield(node[:element]) if block_given?
|
215
|
+
stack.push(left) unless left.nil?
|
216
|
+
stack.push(right) unless right.nil?
|
217
|
+
break if stack.is_empty?
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
# Pre-Order Traversal of Tree
|
222
|
+
#
|
223
|
+
# @return [Array] elements of heap in pre-ordered manner
|
224
|
+
def preorder
|
225
|
+
arr = []
|
226
|
+
pre_order(root, arr)
|
227
|
+
return arr
|
228
|
+
end
|
229
|
+
|
230
|
+
# Post-Order Traversal of Tree
|
231
|
+
#
|
232
|
+
# @return [Array] elements of heap in post-ordered manner
|
233
|
+
def postorder
|
234
|
+
arr = []
|
235
|
+
post_order(root, arr)
|
236
|
+
return arr
|
237
|
+
end
|
238
|
+
|
239
|
+
# In-Order Traversal of Tree
|
240
|
+
#
|
241
|
+
# @return [Array] elements of heap in in-ordered manner
|
242
|
+
def inorder
|
243
|
+
arr = []
|
244
|
+
in_order(root, arr)
|
245
|
+
return arr
|
246
|
+
end
|
247
|
+
|
248
|
+
|
249
|
+
end
|
250
|
+
Tree = Heap
|
251
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative '../heap'
|
2
2
|
module CollectionUtils
|
3
3
|
module HeapUtils
|
4
|
-
class MaxHeap < Heap
|
4
|
+
class MaxHeap < CollectionUtils::Heap
|
5
5
|
|
6
6
|
private
|
7
7
|
attr_accessor :heap
|
@@ -14,8 +14,8 @@ module CollectionUtils
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def heapify(node)
|
17
|
-
left =
|
18
|
-
right =
|
17
|
+
left = left(node[:index])
|
18
|
+
right = right(node[:index])
|
19
19
|
largest = node
|
20
20
|
if !left.nil? && left[:element] >= node[:element]
|
21
21
|
largest = left
|
@@ -32,10 +32,6 @@ module CollectionUtils
|
|
32
32
|
|
33
33
|
end
|
34
34
|
|
35
|
-
def max
|
36
|
-
@heap.first
|
37
|
-
end
|
38
|
-
|
39
35
|
public
|
40
36
|
def initialize(array = [])
|
41
37
|
@heap = []
|
@@ -44,7 +40,7 @@ module CollectionUtils
|
|
44
40
|
end
|
45
41
|
end
|
46
42
|
|
47
|
-
def insert(
|
43
|
+
def insert(element)
|
48
44
|
value = {element: element, index: size}
|
49
45
|
@heap << value
|
50
46
|
i = @heap.size - 1
|
@@ -56,20 +52,24 @@ module CollectionUtils
|
|
56
52
|
end
|
57
53
|
end
|
58
54
|
|
55
|
+
# @return element which has maximum value in heap
|
59
56
|
def get_max
|
60
57
|
return if is_empty?
|
61
|
-
return
|
58
|
+
return root[:element]
|
62
59
|
end
|
63
60
|
|
61
|
+
# Removes the maximum value element from heap and
|
62
|
+
# corrects the whole heap again
|
63
|
+
# @return maximum value element
|
64
64
|
def extract_max
|
65
65
|
return if is_empty?
|
66
|
-
maximum =
|
66
|
+
maximum = root
|
67
67
|
if @heap.size > 1
|
68
68
|
last_value = @heap.last
|
69
69
|
last_value[:index] = 0
|
70
70
|
@heap = @heap.slice(0..@heap.size-2)
|
71
71
|
@heap[0] = last_value
|
72
|
-
heapify(
|
72
|
+
heapify(root)
|
73
73
|
else
|
74
74
|
@heap =[ ]
|
75
75
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative '../heap'
|
2
2
|
module CollectionUtils
|
3
3
|
module HeapUtils
|
4
|
-
class MinHeap < Heap
|
4
|
+
class MinHeap < CollectionUtils::Heap
|
5
5
|
|
6
6
|
private
|
7
7
|
attr_accessor :heap
|
@@ -14,8 +14,8 @@ module CollectionUtils
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def heapify(node)
|
17
|
-
left =
|
18
|
-
right =
|
17
|
+
left = left(node[:index])
|
18
|
+
right = right(node[:index])
|
19
19
|
smallest = node
|
20
20
|
if !left.nil? && left[:element] < node[:element]
|
21
21
|
smallest = left
|
@@ -32,9 +32,6 @@ module CollectionUtils
|
|
32
32
|
|
33
33
|
end
|
34
34
|
|
35
|
-
def min
|
36
|
-
@heap.first
|
37
|
-
end
|
38
35
|
|
39
36
|
public
|
40
37
|
def initialize(array = [])
|
@@ -56,22 +53,27 @@ module CollectionUtils
|
|
56
53
|
end
|
57
54
|
end
|
58
55
|
|
56
|
+
|
57
|
+
# @return element which has minimum value in heap
|
59
58
|
def get_min
|
60
59
|
return if is_empty?
|
61
|
-
return
|
60
|
+
return root[:element]
|
62
61
|
end
|
63
62
|
|
63
|
+
# Removes the minimum value element from heap and
|
64
|
+
# corrects the whole heap again
|
65
|
+
# @return minimum value element
|
64
66
|
def extract_min
|
65
67
|
return if is_empty?
|
66
|
-
minimum =
|
68
|
+
minimum = root
|
67
69
|
if @heap.size > 1
|
68
70
|
last_value = @heap.last
|
69
71
|
last_value[:index] = 0
|
70
72
|
@heap = @heap.slice(0..@heap.size-2)
|
71
73
|
@heap[0] = last_value
|
72
|
-
heapify(
|
74
|
+
heapify(root)
|
73
75
|
else
|
74
|
-
@heap =[
|
76
|
+
@heap =[]
|
75
77
|
end
|
76
78
|
|
77
79
|
return minimum[:element]
|