grosser-algorithms 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +45 -0
- data/VERSION +1 -0
- data/ext/containers/bst/bst.c +249 -0
- data/ext/containers/bst/extconf.rb +4 -0
- data/grosser-algorithms.gemspec +4 -4
- data/rdoc/classes/Algorithms.html +221 -0
- data/rdoc/classes/Algorithms/Algorithms.html +115 -0
- data/rdoc/classes/Algorithms/Algorithms/Sort.html +569 -0
- data/rdoc/classes/Algorithms/Containers.html +162 -0
- data/rdoc/classes/Algorithms/Containers/Heap.html +690 -0
- data/rdoc/classes/Algorithms/Containers/KDTree.html +195 -0
- data/rdoc/classes/Algorithms/Containers/MaxHeap.html +238 -0
- data/rdoc/classes/Algorithms/Containers/MinHeap.html +238 -0
- data/rdoc/classes/Algorithms/Containers/PriorityQueue.html +456 -0
- data/rdoc/classes/Algorithms/Containers/Queue.html +363 -0
- data/rdoc/classes/Algorithms/Containers/RubyDeque.html +617 -0
- data/rdoc/classes/Algorithms/Containers/RubyRBTreeMap.html +662 -0
- data/rdoc/classes/Algorithms/Containers/RubySplayTreeMap.html +623 -0
- data/rdoc/classes/Algorithms/Containers/Stack.html +363 -0
- data/rdoc/classes/Algorithms/Containers/SuffixArray.html +246 -0
- data/rdoc/classes/Algorithms/Containers/Trie.html +555 -0
- data/rdoc/classes/Algorithms/Search.html +273 -0
- data/rdoc/created.rid +1 -0
- data/rdoc/files/History_txt.html +281 -0
- data/rdoc/files/README_markdown.html +248 -0
- data/rdoc/files/lib/algorithms/search_rb.html +108 -0
- data/rdoc/files/lib/algorithms/sort_rb.html +108 -0
- data/rdoc/files/lib/algorithms/string_rb.html +115 -0
- data/rdoc/files/lib/algorithms_rb.html +252 -0
- data/rdoc/files/lib/containers/deque_rb.html +119 -0
- data/rdoc/files/lib/containers/heap_rb.html +124 -0
- data/rdoc/files/lib/containers/kd_tree_rb.html +135 -0
- data/rdoc/files/lib/containers/priority_queue_rb.html +108 -0
- data/rdoc/files/lib/containers/queue_rb.html +108 -0
- data/rdoc/files/lib/containers/rb_tree_map_rb.html +109 -0
- data/rdoc/files/lib/containers/splay_tree_map_rb.html +109 -0
- data/rdoc/files/lib/containers/stack_rb.html +108 -0
- data/rdoc/files/lib/containers/suffix_array_rb.html +113 -0
- data/rdoc/files/lib/containers/trie_rb.html +117 -0
- data/rdoc/fr_class_index.html +43 -0
- data/rdoc/fr_file_index.html +42 -0
- data/rdoc/fr_method_index.html +147 -0
- data/rdoc/index.html +24 -0
- data/rdoc/rdoc-style.css +208 -0
- data/spec/bst_gc_mark_spec.rb +25 -0
- data/spec/bst_spec.rb +25 -0
- metadata +50 -3
@@ -0,0 +1,162 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Module: Algorithms::Containers</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Module</strong></td>
|
53
|
+
<td class="class-name-in-header">Algorithms::Containers</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../../files/lib/algorithms_rb.html">
|
59
|
+
lib/algorithms.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
<a href="../../files/lib/containers/kd_tree_rb.html">
|
63
|
+
lib/containers/kd_tree.rb
|
64
|
+
</a>
|
65
|
+
<br />
|
66
|
+
<a href="../../files/lib/containers/splay_tree_map_rb.html">
|
67
|
+
lib/containers/splay_tree_map.rb
|
68
|
+
</a>
|
69
|
+
<br />
|
70
|
+
<a href="../../files/lib/containers/deque_rb.html">
|
71
|
+
lib/containers/deque.rb
|
72
|
+
</a>
|
73
|
+
<br />
|
74
|
+
<a href="../../files/lib/containers/heap_rb.html">
|
75
|
+
lib/containers/heap.rb
|
76
|
+
</a>
|
77
|
+
<br />
|
78
|
+
<a href="../../files/lib/containers/stack_rb.html">
|
79
|
+
lib/containers/stack.rb
|
80
|
+
</a>
|
81
|
+
<br />
|
82
|
+
<a href="../../files/lib/containers/queue_rb.html">
|
83
|
+
lib/containers/queue.rb
|
84
|
+
</a>
|
85
|
+
<br />
|
86
|
+
<a href="../../files/lib/containers/rb_tree_map_rb.html">
|
87
|
+
lib/containers/rb_tree_map.rb
|
88
|
+
</a>
|
89
|
+
<br />
|
90
|
+
<a href="../../files/lib/containers/suffix_array_rb.html">
|
91
|
+
lib/containers/suffix_array.rb
|
92
|
+
</a>
|
93
|
+
<br />
|
94
|
+
<a href="../../files/lib/containers/priority_queue_rb.html">
|
95
|
+
lib/containers/priority_queue.rb
|
96
|
+
</a>
|
97
|
+
<br />
|
98
|
+
<a href="../../files/lib/containers/trie_rb.html">
|
99
|
+
lib/containers/trie.rb
|
100
|
+
</a>
|
101
|
+
<br />
|
102
|
+
</td>
|
103
|
+
</tr>
|
104
|
+
|
105
|
+
</table>
|
106
|
+
</div>
|
107
|
+
<!-- banner header -->
|
108
|
+
|
109
|
+
<div id="bodyContent">
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
<div id="contextContent">
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
</div>
|
118
|
+
|
119
|
+
|
120
|
+
</div>
|
121
|
+
|
122
|
+
|
123
|
+
<!-- if includes -->
|
124
|
+
|
125
|
+
<div id="section">
|
126
|
+
|
127
|
+
<div id="class-list">
|
128
|
+
<h3 class="section-bar">Classes and Modules</h3>
|
129
|
+
|
130
|
+
Class <a href="Containers/Heap.html" class="link">Algorithms::Containers::Heap</a><br />
|
131
|
+
Class <a href="Containers/KDTree.html" class="link">Algorithms::Containers::KDTree</a><br />
|
132
|
+
Class <a href="Containers/MaxHeap.html" class="link">Algorithms::Containers::MaxHeap</a><br />
|
133
|
+
Class <a href="Containers/MinHeap.html" class="link">Algorithms::Containers::MinHeap</a><br />
|
134
|
+
Class <a href="Containers/PriorityQueue.html" class="link">Algorithms::Containers::PriorityQueue</a><br />
|
135
|
+
Class <a href="Containers/Queue.html" class="link">Algorithms::Containers::Queue</a><br />
|
136
|
+
Class <a href="Containers/RubyDeque.html" class="link">Algorithms::Containers::RubyDeque</a><br />
|
137
|
+
Class <a href="Containers/RubyRBTreeMap.html" class="link">Algorithms::Containers::RubyRBTreeMap</a><br />
|
138
|
+
Class <a href="Containers/RubySplayTreeMap.html" class="link">Algorithms::Containers::RubySplayTreeMap</a><br />
|
139
|
+
Class <a href="Containers/Stack.html" class="link">Algorithms::Containers::Stack</a><br />
|
140
|
+
Class <a href="Containers/SuffixArray.html" class="link">Algorithms::Containers::SuffixArray</a><br />
|
141
|
+
Class <a href="Containers/Trie.html" class="link">Algorithms::Containers::Trie</a><br />
|
142
|
+
|
143
|
+
</div>
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
<!-- if method_list -->
|
152
|
+
|
153
|
+
|
154
|
+
</div>
|
155
|
+
|
156
|
+
|
157
|
+
<div id="validator-badges">
|
158
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
159
|
+
</div>
|
160
|
+
|
161
|
+
</body>
|
162
|
+
</html>
|
@@ -0,0 +1,690 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Class: Algorithms::Containers::Heap</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Class</strong></td>
|
53
|
+
<td class="class-name-in-header">Algorithms::Containers::Heap</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../../../files/lib/containers/heap_rb.html">
|
59
|
+
lib/containers/heap.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
</td>
|
63
|
+
</tr>
|
64
|
+
|
65
|
+
<tr class="top-aligned-row">
|
66
|
+
<td><strong>Parent:</strong></td>
|
67
|
+
<td>
|
68
|
+
Object
|
69
|
+
</td>
|
70
|
+
</tr>
|
71
|
+
</table>
|
72
|
+
</div>
|
73
|
+
<!-- banner header -->
|
74
|
+
|
75
|
+
<div id="bodyContent">
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
<div id="contextContent">
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
</div>
|
84
|
+
|
85
|
+
<div id="method-list">
|
86
|
+
<h3 class="section-bar">Methods</h3>
|
87
|
+
|
88
|
+
<div class="name-list">
|
89
|
+
<a href="#M000055"><<</a>
|
90
|
+
<a href="#M000063">change_key</a>
|
91
|
+
<a href="#M000058">clear</a>
|
92
|
+
<a href="#M000064">delete</a>
|
93
|
+
<a href="#M000059">empty?</a>
|
94
|
+
<a href="#M000056">has_key?</a>
|
95
|
+
<a href="#M000052">length</a>
|
96
|
+
<a href="#M000060">merge!</a>
|
97
|
+
<a href="#M000053">new</a>
|
98
|
+
<a href="#M000057">next</a>
|
99
|
+
<a href="#M000062">next!</a>
|
100
|
+
<a href="#M000061">pop</a>
|
101
|
+
<a href="#M000054">push</a>
|
102
|
+
<a href="#M000051">size</a>
|
103
|
+
</div>
|
104
|
+
</div>
|
105
|
+
|
106
|
+
</div>
|
107
|
+
|
108
|
+
|
109
|
+
<!-- if includes -->
|
110
|
+
<div id="includes">
|
111
|
+
<h3 class="section-bar">Included Modules</h3>
|
112
|
+
|
113
|
+
<div id="includes-list">
|
114
|
+
<span class="include-name">Enumerable</span>
|
115
|
+
</div>
|
116
|
+
</div>
|
117
|
+
|
118
|
+
<div id="section">
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
<!-- if method_list -->
|
128
|
+
<div id="methods">
|
129
|
+
<h3 class="section-bar">Public Class methods</h3>
|
130
|
+
|
131
|
+
<div id="method-M000053" class="method-detail">
|
132
|
+
<a name="M000053"></a>
|
133
|
+
|
134
|
+
<div class="method-heading">
|
135
|
+
<a href="#M000053" class="method-signature">
|
136
|
+
<span class="method-name">Heap.new(optional_array) { |x, y| optional_comparison_fn } <br />
|
137
|
+
→ new_heap<br />
|
138
|
+
</span>
|
139
|
+
</a>
|
140
|
+
</div>
|
141
|
+
|
142
|
+
<div class="method-description">
|
143
|
+
<p>
|
144
|
+
If an optional array is passed, the entries in the array are inserted into
|
145
|
+
the heap with equal key and value fields. Also, an optional block can be
|
146
|
+
passed to define the function that maintains heap property. For example, a
|
147
|
+
min-heap can be created with:
|
148
|
+
</p>
|
149
|
+
<pre>
|
150
|
+
minheap = Heap.new { |x, y| (x <=> y) == -1 }
|
151
|
+
minheap.push(6)
|
152
|
+
minheap.push(10)
|
153
|
+
minheap.pop #=> 6
|
154
|
+
</pre>
|
155
|
+
<p>
|
156
|
+
Thus, smaller elements will be parent nodes. The heap defaults to a
|
157
|
+
min-heap if no block is given.
|
158
|
+
</p>
|
159
|
+
<p><a class="source-toggle" href="#"
|
160
|
+
onclick="toggleCode('M000053-source');return false;">[Source]</a></p>
|
161
|
+
<div class="method-source-code" id="M000053-source">
|
162
|
+
<pre>
|
163
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 44</span>
|
164
|
+
44: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">ary</span>=[], <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
165
|
+
45: <span class="ruby-ivar">@compare_fn</span> = <span class="ruby-identifier">block</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">lambda</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">x</span>, <span class="ruby-identifier">y</span><span class="ruby-operator">|</span> (<span class="ruby-identifier">x</span> <span class="ruby-operator"><=></span> <span class="ruby-identifier">y</span>) <span class="ruby-operator">==</span> <span class="ruby-value">-1</span> }
|
166
|
+
46: <span class="ruby-ivar">@next</span> = <span class="ruby-keyword kw">nil</span>
|
167
|
+
47: <span class="ruby-ivar">@size</span> = <span class="ruby-value">0</span>
|
168
|
+
48: <span class="ruby-ivar">@stored</span> = {}
|
169
|
+
49:
|
170
|
+
50: <span class="ruby-identifier">ary</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">n</span><span class="ruby-operator">|</span> <span class="ruby-identifier">push</span>(<span class="ruby-identifier">n</span>) } <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">ary</span>.<span class="ruby-identifier">empty?</span>
|
171
|
+
51: <span class="ruby-keyword kw">end</span>
|
172
|
+
</pre>
|
173
|
+
</div>
|
174
|
+
</div>
|
175
|
+
</div>
|
176
|
+
|
177
|
+
<h3 class="section-bar">Public Instance methods</h3>
|
178
|
+
|
179
|
+
<div id="method-M000055" class="method-detail">
|
180
|
+
<a name="M000055"></a>
|
181
|
+
|
182
|
+
<div class="method-heading">
|
183
|
+
<span class="method-name"><<</span><span class="method-args">(key, value=key)</span>
|
184
|
+
</div>
|
185
|
+
|
186
|
+
<div class="method-description">
|
187
|
+
<p>
|
188
|
+
Alias for <a href="Heap.html#M000054">push</a>
|
189
|
+
</p>
|
190
|
+
</div>
|
191
|
+
</div>
|
192
|
+
|
193
|
+
<div id="method-M000063" class="method-detail">
|
194
|
+
<a name="M000063"></a>
|
195
|
+
|
196
|
+
<div class="method-heading">
|
197
|
+
<a href="#M000063" class="method-signature">
|
198
|
+
<span class="method-name">change_key(key, new_key) → [new_key, value]<br />
|
199
|
+
change_key(key, new_key) → nil<br />
|
200
|
+
</span>
|
201
|
+
</a>
|
202
|
+
</div>
|
203
|
+
|
204
|
+
<div class="method-description">
|
205
|
+
<p>
|
206
|
+
Changes the key from one to another. Doing so must not violate the heap
|
207
|
+
property or an exception will be raised. If the key is found, an array
|
208
|
+
containing the <a href="Heap.html#M000053">new</a> key and value pair is
|
209
|
+
returned, otherwise nil is returned.
|
210
|
+
</p>
|
211
|
+
<p>
|
212
|
+
In the case of duplicate keys, an arbitrary key is changed. This will be
|
213
|
+
investigated more in the future.
|
214
|
+
</p>
|
215
|
+
<p>
|
216
|
+
Complexity: amortized O(1)
|
217
|
+
</p>
|
218
|
+
<pre>
|
219
|
+
minheap = MinHeap.new([1, 2])
|
220
|
+
minheap.change_key(2, 3) #=> raise error since we can't increase the value in a min-heap
|
221
|
+
minheap.change_key(2, 0) #=> [0, 2]
|
222
|
+
minheap.pop #=> 2
|
223
|
+
minheap.pop #=> 1
|
224
|
+
</pre>
|
225
|
+
<p><a class="source-toggle" href="#"
|
226
|
+
onclick="toggleCode('M000063-source');return false;">[Source]</a></p>
|
227
|
+
<div class="method-source-code" id="M000063-source">
|
228
|
+
<pre>
|
229
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 256</span>
|
230
|
+
256: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">change_key</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">new_key</span>, <span class="ruby-identifier">delete</span>=<span class="ruby-keyword kw">false</span>)
|
231
|
+
257: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">key</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">key</span>].<span class="ruby-identifier">empty?</span> <span class="ruby-operator">||</span> (<span class="ruby-identifier">key</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">new_key</span>)
|
232
|
+
258:
|
233
|
+
259: <span class="ruby-comment cmt"># Must maintain heap property</span>
|
234
|
+
260: <span class="ruby-identifier">raise</span> <span class="ruby-value str">"Changing this key would not maintain heap property!"</span> <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">delete</span> <span class="ruby-operator">||</span> <span class="ruby-ivar">@compare_fn</span>[<span class="ruby-identifier">new_key</span>, <span class="ruby-identifier">key</span>])
|
235
|
+
261: <span class="ruby-identifier">node</span> = <span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">key</span>].<span class="ruby-identifier">shift</span>
|
236
|
+
262: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">node</span>
|
237
|
+
263: <span class="ruby-identifier">node</span>.<span class="ruby-identifier">key</span> = <span class="ruby-identifier">new_key</span>
|
238
|
+
264: <span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">new_key</span>] <span class="ruby-operator">||=</span> []
|
239
|
+
265: <span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">new_key</span>] <span class="ruby-operator"><<</span> <span class="ruby-identifier">node</span>
|
240
|
+
266: <span class="ruby-identifier">parent</span> = <span class="ruby-identifier">node</span>.<span class="ruby-identifier">parent</span>
|
241
|
+
267: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">parent</span>
|
242
|
+
268: <span class="ruby-comment cmt"># if heap property is violated</span>
|
243
|
+
269: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">delete</span> <span class="ruby-operator">||</span> <span class="ruby-ivar">@compare_fn</span>[<span class="ruby-identifier">new_key</span>, <span class="ruby-identifier">parent</span>.<span class="ruby-identifier">key</span>]
|
244
|
+
270: <span class="ruby-identifier">cut</span>(<span class="ruby-identifier">node</span>, <span class="ruby-identifier">parent</span>)
|
245
|
+
271: <span class="ruby-identifier">cascading_cut</span>(<span class="ruby-identifier">parent</span>)
|
246
|
+
272: <span class="ruby-keyword kw">end</span>
|
247
|
+
273: <span class="ruby-keyword kw">end</span>
|
248
|
+
274: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">delete</span> <span class="ruby-operator">||</span> <span class="ruby-ivar">@compare_fn</span>[<span class="ruby-identifier">node</span>.<span class="ruby-identifier">key</span>, <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">key</span>]
|
249
|
+
275: <span class="ruby-ivar">@next</span> = <span class="ruby-identifier">node</span>
|
250
|
+
276: <span class="ruby-keyword kw">end</span>
|
251
|
+
277: <span class="ruby-keyword kw">return</span> [<span class="ruby-identifier">node</span>.<span class="ruby-identifier">key</span>, <span class="ruby-identifier">node</span>.<span class="ruby-identifier">value</span>]
|
252
|
+
278: <span class="ruby-keyword kw">end</span>
|
253
|
+
279: <span class="ruby-keyword kw">nil</span>
|
254
|
+
280: <span class="ruby-keyword kw">end</span>
|
255
|
+
</pre>
|
256
|
+
</div>
|
257
|
+
</div>
|
258
|
+
</div>
|
259
|
+
|
260
|
+
<div id="method-M000058" class="method-detail">
|
261
|
+
<a name="M000058"></a>
|
262
|
+
|
263
|
+
<div class="method-heading">
|
264
|
+
<a href="#M000058" class="method-signature">
|
265
|
+
<span class="method-name">clear → nil<br />
|
266
|
+
</span>
|
267
|
+
</a>
|
268
|
+
</div>
|
269
|
+
|
270
|
+
<div class="method-description">
|
271
|
+
<p>
|
272
|
+
Removes all elements from the heap, destructively.
|
273
|
+
</p>
|
274
|
+
<p>
|
275
|
+
Complexity: O(1)
|
276
|
+
</p>
|
277
|
+
<p><a class="source-toggle" href="#"
|
278
|
+
onclick="toggleCode('M000058-source');return false;">[Source]</a></p>
|
279
|
+
<div class="method-source-code" id="M000058-source">
|
280
|
+
<pre>
|
281
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 134</span>
|
282
|
+
134: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">clear</span>
|
283
|
+
135: <span class="ruby-ivar">@next</span> = <span class="ruby-keyword kw">nil</span>
|
284
|
+
136: <span class="ruby-ivar">@size</span> = <span class="ruby-value">0</span>
|
285
|
+
137: <span class="ruby-ivar">@stored</span> = {}
|
286
|
+
138: <span class="ruby-keyword kw">nil</span>
|
287
|
+
139: <span class="ruby-keyword kw">end</span>
|
288
|
+
</pre>
|
289
|
+
</div>
|
290
|
+
</div>
|
291
|
+
</div>
|
292
|
+
|
293
|
+
<div id="method-M000064" class="method-detail">
|
294
|
+
<a name="M000064"></a>
|
295
|
+
|
296
|
+
<div class="method-heading">
|
297
|
+
<a href="#M000064" class="method-signature">
|
298
|
+
<span class="method-name">delete(key) → value<br />
|
299
|
+
delete(key) → nil<br />
|
300
|
+
</span>
|
301
|
+
</a>
|
302
|
+
</div>
|
303
|
+
|
304
|
+
<div class="method-description">
|
305
|
+
<p>
|
306
|
+
Deletes the item with associated key and returns it. nil is returned if the
|
307
|
+
keyis not found. In the case of nodes with duplicate keys, an arbitrary one
|
308
|
+
is deleted.
|
309
|
+
</p>
|
310
|
+
<p>
|
311
|
+
Complexity: amortized O(log n)
|
312
|
+
</p>
|
313
|
+
<pre>
|
314
|
+
minheap = MinHeap.new([1, 2])
|
315
|
+
minheap.delete(1) #=> 1
|
316
|
+
minheap.size #=> 1
|
317
|
+
</pre>
|
318
|
+
<p><a class="source-toggle" href="#"
|
319
|
+
onclick="toggleCode('M000064-source');return false;">[Source]</a></p>
|
320
|
+
<div class="method-source-code" id="M000064-source">
|
321
|
+
<pre>
|
322
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 295</span>
|
323
|
+
295: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">delete</span>(<span class="ruby-identifier">key</span>)
|
324
|
+
296: <span class="ruby-identifier">pop</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">change_key</span>(<span class="ruby-identifier">key</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">true</span>)
|
325
|
+
297: <span class="ruby-keyword kw">end</span>
|
326
|
+
</pre>
|
327
|
+
</div>
|
328
|
+
</div>
|
329
|
+
</div>
|
330
|
+
|
331
|
+
<div id="method-M000059" class="method-detail">
|
332
|
+
<a name="M000059"></a>
|
333
|
+
|
334
|
+
<div class="method-heading">
|
335
|
+
<a href="#M000059" class="method-signature">
|
336
|
+
<span class="method-name">empty? → true or false<br />
|
337
|
+
</span>
|
338
|
+
</a>
|
339
|
+
</div>
|
340
|
+
|
341
|
+
<div class="method-description">
|
342
|
+
<p>
|
343
|
+
Returns true if the heap is empty, false otherwise.
|
344
|
+
</p>
|
345
|
+
<p><a class="source-toggle" href="#"
|
346
|
+
onclick="toggleCode('M000059-source');return false;">[Source]</a></p>
|
347
|
+
<div class="method-source-code" id="M000059-source">
|
348
|
+
<pre>
|
349
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 145</span>
|
350
|
+
145: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">empty?</span>
|
351
|
+
146: <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">nil?</span>
|
352
|
+
147: <span class="ruby-keyword kw">end</span>
|
353
|
+
</pre>
|
354
|
+
</div>
|
355
|
+
</div>
|
356
|
+
</div>
|
357
|
+
|
358
|
+
<div id="method-M000056" class="method-detail">
|
359
|
+
<a name="M000056"></a>
|
360
|
+
|
361
|
+
<div class="method-heading">
|
362
|
+
<a href="#M000056" class="method-signature">
|
363
|
+
<span class="method-name">has_key?(key) → true or false<br />
|
364
|
+
</span>
|
365
|
+
</a>
|
366
|
+
</div>
|
367
|
+
|
368
|
+
<div class="method-description">
|
369
|
+
<p>
|
370
|
+
Returns true if heap contains the key.
|
371
|
+
</p>
|
372
|
+
<p>
|
373
|
+
Complexity: O(1)
|
374
|
+
</p>
|
375
|
+
<pre>
|
376
|
+
minheap = MinHeap.new([1, 2])
|
377
|
+
minheap.has_key?(2) #=> true
|
378
|
+
minheap.has_key?(4) #=> false
|
379
|
+
</pre>
|
380
|
+
<p><a class="source-toggle" href="#"
|
381
|
+
onclick="toggleCode('M000056-source');return false;">[Source]</a></p>
|
382
|
+
<div class="method-source-code" id="M000056-source">
|
383
|
+
<pre>
|
384
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 107</span>
|
385
|
+
107: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">key</span>)
|
386
|
+
108: <span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">key</span>] <span class="ruby-operator">&&</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">key</span>].<span class="ruby-identifier">empty?</span> <span class="ruby-value">? </span><span class="ruby-keyword kw">true</span> <span class="ruby-operator">:</span> <span class="ruby-keyword kw">false</span>
|
387
|
+
109: <span class="ruby-keyword kw">end</span>
|
388
|
+
</pre>
|
389
|
+
</div>
|
390
|
+
</div>
|
391
|
+
</div>
|
392
|
+
|
393
|
+
<div id="method-M000052" class="method-detail">
|
394
|
+
<a name="M000052"></a>
|
395
|
+
|
396
|
+
<div class="method-heading">
|
397
|
+
<span class="method-name">length</span><span class="method-args">()</span>
|
398
|
+
</div>
|
399
|
+
|
400
|
+
<div class="method-description">
|
401
|
+
<p>
|
402
|
+
Alias for <a href="Heap.html#M000051">size</a>
|
403
|
+
</p>
|
404
|
+
</div>
|
405
|
+
</div>
|
406
|
+
|
407
|
+
<div id="method-M000060" class="method-detail">
|
408
|
+
<a name="M000060"></a>
|
409
|
+
|
410
|
+
<div class="method-heading">
|
411
|
+
<a href="#M000060" class="method-signature">
|
412
|
+
<span class="method-name">merge!(otherheap) → merged_heap<br />
|
413
|
+
</span>
|
414
|
+
</a>
|
415
|
+
</div>
|
416
|
+
|
417
|
+
<div class="method-description">
|
418
|
+
<p>
|
419
|
+
Does a shallow merge of all the nodes in the other heap.
|
420
|
+
</p>
|
421
|
+
<p>
|
422
|
+
Complexity: O(1)
|
423
|
+
</p>
|
424
|
+
<pre>
|
425
|
+
heap = MinHeap.new([5, 6, 7, 8])
|
426
|
+
otherheap = MinHeap.new([1, 2, 3, 4])
|
427
|
+
heap.merge!(otherheap)
|
428
|
+
heap.size #=> 8
|
429
|
+
heap.pop #=> 1
|
430
|
+
</pre>
|
431
|
+
<p><a class="source-toggle" href="#"
|
432
|
+
onclick="toggleCode('M000060-source');return false;">[Source]</a></p>
|
433
|
+
<div class="method-source-code" id="M000060-source">
|
434
|
+
<pre>
|
435
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 161</span>
|
436
|
+
161: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">otherheap</span>)
|
437
|
+
162: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">"Trying to merge a heap with something not a heap"</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">otherheap</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Heap</span>
|
438
|
+
163: <span class="ruby-identifier">other_root</span> = <span class="ruby-identifier">otherheap</span>.<span class="ruby-identifier">instance_variable_get</span>(<span class="ruby-value str">"@next"</span>)
|
439
|
+
164: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">other_root</span>
|
440
|
+
165: <span class="ruby-ivar">@stored</span> = <span class="ruby-ivar">@stored</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">otherheap</span>.<span class="ruby-identifier">instance_variable_get</span>(<span class="ruby-value str">"@stored"</span>)) { <span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> (<span class="ruby-identifier">a</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">b</span>).<span class="ruby-identifier">flatten</span> }
|
441
|
+
166: <span class="ruby-comment cmt"># Insert othernode's @next node to the left of current @next</span>
|
442
|
+
167: <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">left</span>.<span class="ruby-identifier">right</span> = <span class="ruby-identifier">other_root</span>
|
443
|
+
168: <span class="ruby-identifier">ol</span> = <span class="ruby-identifier">other_root</span>.<span class="ruby-identifier">left</span>
|
444
|
+
169: <span class="ruby-identifier">other_root</span>.<span class="ruby-identifier">left</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">left</span>
|
445
|
+
170: <span class="ruby-identifier">ol</span>.<span class="ruby-identifier">right</span> = <span class="ruby-ivar">@next</span>
|
446
|
+
171: <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">left</span> = <span class="ruby-identifier">ol</span>
|
447
|
+
172:
|
448
|
+
173: <span class="ruby-ivar">@next</span> = <span class="ruby-identifier">other_root</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@compare_fn</span>[<span class="ruby-identifier">other_root</span>.<span class="ruby-identifier">key</span>, <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">key</span>]
|
449
|
+
174: <span class="ruby-keyword kw">end</span>
|
450
|
+
175: <span class="ruby-ivar">@size</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">otherheap</span>.<span class="ruby-identifier">size</span>
|
451
|
+
176: <span class="ruby-keyword kw">end</span>
|
452
|
+
</pre>
|
453
|
+
</div>
|
454
|
+
</div>
|
455
|
+
</div>
|
456
|
+
|
457
|
+
<div id="method-M000057" class="method-detail">
|
458
|
+
<a name="M000057"></a>
|
459
|
+
|
460
|
+
<div class="method-heading">
|
461
|
+
<a href="#M000057" class="method-signature">
|
462
|
+
<span class="method-name">next → value<br />
|
463
|
+
next → nil<br />
|
464
|
+
</span>
|
465
|
+
</a>
|
466
|
+
</div>
|
467
|
+
|
468
|
+
<div class="method-description">
|
469
|
+
<p>
|
470
|
+
Returns the value of the <a href="Heap.html#M000057">next</a> item in heap
|
471
|
+
order, but does not remove it.
|
472
|
+
</p>
|
473
|
+
<p>
|
474
|
+
Complexity: O(1)
|
475
|
+
</p>
|
476
|
+
<pre>
|
477
|
+
minheap = MinHeap.new([1, 2])
|
478
|
+
minheap.next #=> 1
|
479
|
+
minheap.size #=> 2
|
480
|
+
</pre>
|
481
|
+
<p><a class="source-toggle" href="#"
|
482
|
+
onclick="toggleCode('M000057-source');return false;">[Source]</a></p>
|
483
|
+
<div class="method-source-code" id="M000057-source">
|
484
|
+
<pre>
|
485
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 123</span>
|
486
|
+
123: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">next</span>
|
487
|
+
124: <span class="ruby-ivar">@next</span> <span class="ruby-operator">&&</span> <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">value</span>
|
488
|
+
125: <span class="ruby-keyword kw">end</span>
|
489
|
+
</pre>
|
490
|
+
</div>
|
491
|
+
</div>
|
492
|
+
</div>
|
493
|
+
|
494
|
+
<div id="method-M000062" class="method-detail">
|
495
|
+
<a name="M000062"></a>
|
496
|
+
|
497
|
+
<div class="method-heading">
|
498
|
+
<span class="method-name">next!</span><span class="method-args">()</span>
|
499
|
+
</div>
|
500
|
+
|
501
|
+
<div class="method-description">
|
502
|
+
<p>
|
503
|
+
Alias for <a href="Heap.html#M000061">pop</a>
|
504
|
+
</p>
|
505
|
+
</div>
|
506
|
+
</div>
|
507
|
+
|
508
|
+
<div id="method-M000061" class="method-detail">
|
509
|
+
<a name="M000061"></a>
|
510
|
+
|
511
|
+
<div class="method-heading">
|
512
|
+
<a href="#M000061" class="method-signature">
|
513
|
+
<span class="method-name">pop → value<br />
|
514
|
+
pop → nil<br />
|
515
|
+
</span>
|
516
|
+
</a>
|
517
|
+
</div>
|
518
|
+
|
519
|
+
<div class="method-description">
|
520
|
+
<p>
|
521
|
+
Returns the value of the <a href="Heap.html#M000057">next</a> item in heap
|
522
|
+
order and removes it from the heap.
|
523
|
+
</p>
|
524
|
+
<p>
|
525
|
+
Complexity: O(1)
|
526
|
+
</p>
|
527
|
+
<pre>
|
528
|
+
minheap = MinHeap.new([1, 2])
|
529
|
+
minheap.pop #=> 1
|
530
|
+
minheap.size #=> 1
|
531
|
+
</pre>
|
532
|
+
<p><a class="source-toggle" href="#"
|
533
|
+
onclick="toggleCode('M000061-source');return false;">[Source]</a></p>
|
534
|
+
<div class="method-source-code" id="M000061-source">
|
535
|
+
<pre>
|
536
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 190</span>
|
537
|
+
190: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">pop</span>
|
538
|
+
191: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@next</span>
|
539
|
+
192: <span class="ruby-identifier">popped</span> = <span class="ruby-ivar">@next</span>
|
540
|
+
193: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@size</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
|
541
|
+
194: <span class="ruby-identifier">clear</span>
|
542
|
+
195: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">popped</span>.<span class="ruby-identifier">value</span>
|
543
|
+
196: <span class="ruby-keyword kw">end</span>
|
544
|
+
197: <span class="ruby-comment cmt"># Merge the popped's children into root node</span>
|
545
|
+
198: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">child</span>
|
546
|
+
199: <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">child</span>.<span class="ruby-identifier">parent</span> = <span class="ruby-keyword kw">nil</span>
|
547
|
+
200:
|
548
|
+
201: <span class="ruby-comment cmt"># get rid of parent</span>
|
549
|
+
202: <span class="ruby-identifier">sibling</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">child</span>.<span class="ruby-identifier">right</span>
|
550
|
+
203: <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">sibling</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">child</span>
|
551
|
+
204: <span class="ruby-identifier">sibling</span>.<span class="ruby-identifier">parent</span> = <span class="ruby-keyword kw">nil</span>
|
552
|
+
205: <span class="ruby-identifier">sibling</span> = <span class="ruby-identifier">sibling</span>.<span class="ruby-identifier">right</span>
|
553
|
+
206: <span class="ruby-keyword kw">end</span>
|
554
|
+
207:
|
555
|
+
208: <span class="ruby-comment cmt"># Merge the children into the root. If @next is the only root node, </span>
|
556
|
+
209: <span class="ruby-comment cmt"># make its child the @next node</span>
|
557
|
+
210: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">right</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@next</span>
|
558
|
+
211: <span class="ruby-ivar">@next</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">child</span>
|
559
|
+
212: <span class="ruby-keyword kw">else</span>
|
560
|
+
213: <span class="ruby-identifier">next_left</span>, <span class="ruby-identifier">next_right</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">left</span>, <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">right</span>
|
561
|
+
214: <span class="ruby-identifier">current_child</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">child</span>
|
562
|
+
215: <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">right</span>.<span class="ruby-identifier">left</span> = <span class="ruby-identifier">current_child</span>
|
563
|
+
216: <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">left</span>.<span class="ruby-identifier">right</span> = <span class="ruby-identifier">current_child</span>.<span class="ruby-identifier">right</span>
|
564
|
+
217: <span class="ruby-identifier">current_child</span>.<span class="ruby-identifier">right</span>.<span class="ruby-identifier">left</span> = <span class="ruby-identifier">next_left</span>
|
565
|
+
218: <span class="ruby-identifier">current_child</span>.<span class="ruby-identifier">right</span> = <span class="ruby-identifier">next_right</span>
|
566
|
+
219: <span class="ruby-ivar">@next</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">right</span>
|
567
|
+
220: <span class="ruby-keyword kw">end</span>
|
568
|
+
221: <span class="ruby-keyword kw">else</span>
|
569
|
+
222: <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">left</span>.<span class="ruby-identifier">right</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">right</span>
|
570
|
+
223: <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">right</span>.<span class="ruby-identifier">left</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">left</span>
|
571
|
+
224: <span class="ruby-ivar">@next</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">right</span>
|
572
|
+
225: <span class="ruby-keyword kw">end</span>
|
573
|
+
226: <span class="ruby-identifier">consolidate</span>
|
574
|
+
227:
|
575
|
+
228: <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">popped</span>.<span class="ruby-identifier">key</span>].<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">popped</span>)
|
576
|
+
229: <span class="ruby-identifier">raise</span> <span class="ruby-value str">"Couldn't delete node from stored nodes hash"</span>
|
577
|
+
230: <span class="ruby-keyword kw">end</span>
|
578
|
+
231: <span class="ruby-ivar">@size</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span>
|
579
|
+
232:
|
580
|
+
233: <span class="ruby-identifier">popped</span>.<span class="ruby-identifier">value</span>
|
581
|
+
234: <span class="ruby-keyword kw">end</span>
|
582
|
+
</pre>
|
583
|
+
</div>
|
584
|
+
</div>
|
585
|
+
</div>
|
586
|
+
|
587
|
+
<div id="method-M000054" class="method-detail">
|
588
|
+
<a name="M000054"></a>
|
589
|
+
|
590
|
+
<div class="method-heading">
|
591
|
+
<a href="#M000054" class="method-signature">
|
592
|
+
<span class="method-name">push(key, value) → value<br />
|
593
|
+
push(value) → value<br />
|
594
|
+
</span>
|
595
|
+
</a>
|
596
|
+
</div>
|
597
|
+
|
598
|
+
<div class="method-description">
|
599
|
+
<p>
|
600
|
+
Inserts an item with a given key into the heap. If only one parameter is
|
601
|
+
given, the key is set to the value.
|
602
|
+
</p>
|
603
|
+
<p>
|
604
|
+
Complexity: O(1)
|
605
|
+
</p>
|
606
|
+
<pre>
|
607
|
+
heap = MinHeap.new
|
608
|
+
heap.push(1, "Cat")
|
609
|
+
heap.push(2)
|
610
|
+
heap.pop #=> "Cat"
|
611
|
+
heap.pop #=> 2
|
612
|
+
</pre>
|
613
|
+
<p><a class="source-toggle" href="#"
|
614
|
+
onclick="toggleCode('M000054-source');return false;">[Source]</a></p>
|
615
|
+
<div class="method-source-code" id="M000054-source">
|
616
|
+
<pre>
|
617
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 67</span>
|
618
|
+
67: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">push</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>=<span class="ruby-identifier">key</span>)
|
619
|
+
68: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">"Heap keys must not be nil."</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">key</span>
|
620
|
+
69: <span class="ruby-identifier">node</span> = <span class="ruby-constant">Node</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>)
|
621
|
+
70: <span class="ruby-comment cmt"># Add new node to the left of the @next node</span>
|
622
|
+
71: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@next</span>
|
623
|
+
72: <span class="ruby-identifier">node</span>.<span class="ruby-identifier">right</span> = <span class="ruby-ivar">@next</span>
|
624
|
+
73: <span class="ruby-identifier">node</span>.<span class="ruby-identifier">left</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">left</span>
|
625
|
+
74: <span class="ruby-identifier">node</span>.<span class="ruby-identifier">left</span>.<span class="ruby-identifier">right</span> = <span class="ruby-identifier">node</span>
|
626
|
+
75: <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">left</span> = <span class="ruby-identifier">node</span>
|
627
|
+
76: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@compare_fn</span>[<span class="ruby-identifier">key</span>, <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">key</span>]
|
628
|
+
77: <span class="ruby-ivar">@next</span> = <span class="ruby-identifier">node</span>
|
629
|
+
78: <span class="ruby-keyword kw">end</span>
|
630
|
+
79: <span class="ruby-keyword kw">else</span>
|
631
|
+
80: <span class="ruby-ivar">@next</span> = <span class="ruby-identifier">node</span>
|
632
|
+
81: <span class="ruby-keyword kw">end</span>
|
633
|
+
82: <span class="ruby-ivar">@size</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
634
|
+
83:
|
635
|
+
84: <span class="ruby-identifier">arr</span> = []
|
636
|
+
85: <span class="ruby-identifier">w</span> = <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">right</span>
|
637
|
+
86: <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">w</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@next</span> <span class="ruby-keyword kw">do</span>
|
638
|
+
87: <span class="ruby-identifier">arr</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">w</span>.<span class="ruby-identifier">value</span>
|
639
|
+
88: <span class="ruby-identifier">w</span> = <span class="ruby-identifier">w</span>.<span class="ruby-identifier">right</span>
|
640
|
+
89: <span class="ruby-keyword kw">end</span>
|
641
|
+
90: <span class="ruby-identifier">arr</span> <span class="ruby-operator"><<</span> <span class="ruby-ivar">@next</span>.<span class="ruby-identifier">value</span>
|
642
|
+
91: <span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">key</span>] <span class="ruby-operator">||=</span> []
|
643
|
+
92: <span class="ruby-ivar">@stored</span>[<span class="ruby-identifier">key</span>] <span class="ruby-operator"><<</span> <span class="ruby-identifier">node</span>
|
644
|
+
93: <span class="ruby-identifier">value</span>
|
645
|
+
94: <span class="ruby-keyword kw">end</span>
|
646
|
+
</pre>
|
647
|
+
</div>
|
648
|
+
</div>
|
649
|
+
</div>
|
650
|
+
|
651
|
+
<div id="method-M000051" class="method-detail">
|
652
|
+
<a name="M000051"></a>
|
653
|
+
|
654
|
+
<div class="method-heading">
|
655
|
+
<a href="#M000051" class="method-signature">
|
656
|
+
<span class="method-name">size → int<br />
|
657
|
+
</span>
|
658
|
+
</a>
|
659
|
+
</div>
|
660
|
+
|
661
|
+
<div class="method-description">
|
662
|
+
<p>
|
663
|
+
Return the number of elements in the heap.
|
664
|
+
</p>
|
665
|
+
<p><a class="source-toggle" href="#"
|
666
|
+
onclick="toggleCode('M000051-source');return false;">[Source]</a></p>
|
667
|
+
<div class="method-source-code" id="M000051-source">
|
668
|
+
<pre>
|
669
|
+
<span class="ruby-comment cmt"># File lib/containers/heap.rb, line 23</span>
|
670
|
+
23: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">size</span>
|
671
|
+
24: <span class="ruby-ivar">@size</span>
|
672
|
+
25: <span class="ruby-keyword kw">end</span>
|
673
|
+
</pre>
|
674
|
+
</div>
|
675
|
+
</div>
|
676
|
+
</div>
|
677
|
+
|
678
|
+
|
679
|
+
</div>
|
680
|
+
|
681
|
+
|
682
|
+
</div>
|
683
|
+
|
684
|
+
|
685
|
+
<div id="validator-badges">
|
686
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
687
|
+
</div>
|
688
|
+
|
689
|
+
</body>
|
690
|
+
</html>
|