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
@@ -1,561 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
- <head>
6
- <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
7
-
8
- <title>Class: DS::TreeWalker</title>
9
-
10
- <link rel="stylesheet" href="../rdoc.css" type="text/css" media="screen" />
11
-
12
- <script src="../js/jquery.js" type="text/javascript" charset="utf-8"></script>
13
- <script src="../js/thickbox-compressed.js" type="text/javascript" charset="utf-8"></script>
14
- <script src="../js/quicksearch.js" type="text/javascript" charset="utf-8"></script>
15
- <script src="../js/darkfish.js" type="text/javascript" charset="utf-8"></script>
16
-
17
- </head>
18
- <body id="top" class="class">
19
-
20
- <div id="metadata">
21
- <div id="home-metadata">
22
- <div id="home-section" class="section">
23
- <h3 class="section-header">
24
- <a href="../index.html">Home</a>
25
- <a href="../index.html#classes">Classes</a>
26
- <a href="../index.html#methods">Methods</a>
27
- </h3>
28
- </div>
29
- </div>
30
-
31
- <div id="file-metadata">
32
- <div id="file-list-section" class="section">
33
- <h3 class="section-header">In Files</h3>
34
- <div class="section-body">
35
- <ul>
36
-
37
- <li><a href="../ds/trees/tree_walker_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
38
- class="thickbox" title="ds/trees/tree_walker.rb">ds/trees/tree_walker.rb</a></li>
39
-
40
- </ul>
41
- </div>
42
- </div>
43
-
44
-
45
- </div>
46
-
47
- <div id="class-metadata">
48
-
49
- <!-- Parent Class -->
50
- <div id="parent-class-section" class="section">
51
- <h3 class="section-header">Parent</h3>
52
-
53
- <p class="link">Object</p>
54
-
55
- </div>
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
- <!-- Method Quickref -->
64
- <div id="method-list-section" class="section">
65
- <h3 class="section-header">Methods</h3>
66
- <ul class="link-list">
67
-
68
- <li><a href="#method-c-new">::new</a></li>
69
-
70
- <li><a href="#method-i-recalculate-21">#recalculate!</a></li>
71
-
72
- <li><a href="#method-i-reset">#reset</a></li>
73
-
74
- <li><a href="#method-i-summarize">#summarize</a></li>
75
-
76
- <li><a href="#method-i-traverse">#traverse</a></li>
77
-
78
- <li><a href="#method-i-traverse_bfs">#traverse_bfs</a></li>
79
-
80
- <li><a href="#method-i-traverse_with_h">#traverse_with_h</a></li>
81
-
82
- </ul>
83
- </div>
84
-
85
-
86
-
87
- </div>
88
-
89
- <div id="project-metadata">
90
-
91
-
92
-
93
- <div id="classindex-section" class="section project-section">
94
- <h3 class="section-header">Class/Module Index
95
- <span class="search-toggle"><img src="../images/find.png"
96
- height="16" width="16" alt="[+]"
97
- title="show/hide quicksearch" /></span></h3>
98
- <form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
99
- <fieldset>
100
- <legend>Quicksearch</legend>
101
- <input type="text" name="quicksearch" value=""
102
- class="quicksearch-field" />
103
- </fieldset>
104
- </form>
105
-
106
- <ul class="link-list">
107
-
108
- <li><a href="../DS.html">DS</a></li>
109
-
110
- <li><a href="../DS/Array2D.html">DS::Array2D</a></li>
111
-
112
- <li><a href="../DS/BinaryHeap.html">DS::BinaryHeap</a></li>
113
-
114
- <li><a href="../DS/BinarySearchTree.html">DS::BinarySearchTree</a></li>
115
-
116
- <li><a href="../DS/BinaryTree.html">DS::BinaryTree</a></li>
117
-
118
- <li><a href="../DS/CompleteBinaryTree.html">DS::CompleteBinaryTree</a></li>
119
-
120
- <li><a href="../DS/CyclicList.html">DS::CyclicList</a></li>
121
-
122
- <li><a href="../DS/Digraph.html">DS::Digraph</a></li>
123
-
124
- <li><a href="../DS/Edge.html">DS::Edge</a></li>
125
-
126
- <li><a href="../DS/ExpandableArray.html">DS::ExpandableArray</a></li>
127
-
128
- <li><a href="../DS/Graph.html">DS::Graph</a></li>
129
-
130
- <li><a href="../DS/GraphAsList.html">DS::GraphAsList</a></li>
131
-
132
- <li><a href="../DS/GraphAsMatrix.html">DS::GraphAsMatrix</a></li>
133
-
134
- <li><a href="../DS/GraphAsTriMatrix.html">DS::GraphAsTriMatrix</a></li>
135
-
136
- <li><a href="../DS/List.html">DS::List</a></li>
137
-
138
- <li><a href="../DS/ListElement.html">DS::ListElement</a></li>
139
-
140
- <li><a href="../DS/Queue.html">DS::Queue</a></li>
141
-
142
- <li><a href="../DS/Ring.html">DS::Ring</a></li>
143
-
144
- <li><a href="../DS/Stack.html">DS::Stack</a></li>
145
-
146
- <li><a href="../DS/Tree.html">DS::Tree</a></li>
147
-
148
- <li><a href="../DS/TreeWalker.html">DS::TreeWalker</a></li>
149
-
150
- <li><a href="../DS/TriMatrix.html">DS::TriMatrix</a></li>
151
-
152
- <li><a href="../Array.html">Array</a></li>
153
-
154
- </ul>
155
- <div id="no-class-search-results" style="display: none;">No matching classes.</div>
156
- </div>
157
-
158
-
159
- </div>
160
- </div>
161
-
162
- <div id="documentation">
163
- <h1 class="class">DS::TreeWalker</h1>
164
-
165
- <div id="description" class="description">
166
-
167
- </div><!-- description -->
168
-
169
-
170
-
171
-
172
- <div id="5Buntitled-5D" class="documentation-section">
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
- <!-- Attributes -->
181
- <div id="attribute-method-details" class="method-section section">
182
- <h3 class="section-header">Attributes</h3>
183
-
184
-
185
- <div id="tree-attribute-method" class="method-detail">
186
- <a name="tree"></a>
187
-
188
- <a name="tree="></a>
189
-
190
- <div class="method-heading attribute-method-heading">
191
- <span class="method-name">tree</span><span
192
- class="attribute-access-type">[RW]</span>
193
- </div>
194
-
195
- <div class="method-description">
196
-
197
-
198
-
199
- </div>
200
- </div>
201
-
202
- <div id="visited-attribute-method" class="method-detail">
203
- <a name="visited"></a>
204
-
205
- <a name="visited="></a>
206
-
207
- <div class="method-heading attribute-method-heading">
208
- <span class="method-name">visited</span><span
209
- class="attribute-access-type">[RW]</span>
210
- </div>
211
-
212
- <div class="method-description">
213
-
214
-
215
-
216
- </div>
217
- </div>
218
-
219
- </div><!-- attribute-method-details -->
220
-
221
-
222
- <!-- Methods -->
223
-
224
- <div id="public-class-method-details" class="method-section section">
225
- <h3 class="section-header">Public Class Methods</h3>
226
-
227
-
228
- <div id="new-method" class="method-detail ">
229
- <a name="method-c-new"></a>
230
-
231
-
232
- <div class="method-heading">
233
- <span class="method-name">new</span><span
234
- class="method-args">(tree=nil)</span>
235
- <span class="method-click-advice">click to toggle source</span>
236
- </div>
237
-
238
-
239
- <div class="method-description">
240
-
241
- <p>Creates new tree iterator.</p>
242
-
243
-
244
-
245
- <div class="method-source-code" id="new-source">
246
- <pre>
247
- <span class="ruby-comment"># File ds/trees/tree_walker.rb, line 9</span>
248
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">tree</span>=<span class="ruby-keyword">nil</span>)
249
- <span class="ruby-ivar">@visited</span> = []
250
- <span class="ruby-ivar">@tree</span> = <span class="ruby-identifier">tree</span>
251
- <span class="ruby-keyword">end</span></pre>
252
- </div><!-- new-source -->
253
-
254
- </div>
255
-
256
-
257
-
258
-
259
- </div><!-- new-method -->
260
-
261
-
262
- </div><!-- public-class-method-details -->
263
-
264
- <div id="public-instance-method-details" class="method-section section">
265
- <h3 class="section-header">Public Instance Methods</h3>
266
-
267
-
268
- <div id="recalculate-21-method" class="method-detail ">
269
- <a name="method-i-recalculate-21"></a>
270
-
271
-
272
- <div class="method-heading">
273
- <span class="method-name">recalculate!</span><span
274
- class="method-args">(tree,order,memo=nil,&block)</span>
275
- <span class="method-click-advice">click to toggle source</span>
276
- </div>
277
-
278
-
279
- <div class="method-description">
280
-
281
- <p>Recalculates tree by evaluating block on every node.</p>
282
-
283
-
284
-
285
- <div class="method-source-code" id="recalculate-21-source">
286
- <pre>
287
- <span class="ruby-comment"># File ds/trees/tree_walker.rb, line 84</span>
288
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">recalculate!</span>(<span class="ruby-identifier">tree</span>,<span class="ruby-identifier">order</span>,<span class="ruby-identifier">memo</span>=<span class="ruby-keyword">nil</span>,&amp;<span class="ruby-identifier">block</span>)
289
- <span class="ruby-keyword">if</span> <span class="ruby-identifier">tree</span>
290
-
291
- <span class="ruby-keyword">case</span> <span class="ruby-identifier">order</span>
292
- <span class="ruby-keyword">when</span> <span class="ruby-value">:postorder</span>
293
-
294
- <span class="ruby-identifier">arr</span> = <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">map</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">t</span><span class="ruby-operator">|</span> <span class="ruby-identifier">recalculate!</span>(<span class="ruby-identifier">t</span>,<span class="ruby-identifier">order</span>,<span class="ruby-identifier">memo</span>,&amp;<span class="ruby-identifier">block</span>) }
295
- <span class="ruby-identifier">result</span> = <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">arr</span>.<span class="ruby-identifier">push</span> <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">data</span>)
296
- <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">data</span> = <span class="ruby-identifier">result</span>
297
-
298
-
299
- <span class="ruby-keyword">when</span> <span class="ruby-value">:preorder</span>
300
-
301
- <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">data</span> = <span class="ruby-keyword">yield</span> <span class="ruby-identifier">tree</span>, <span class="ruby-identifier">memo</span>
302
- <span class="ruby-identifier">memo</span> = <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">data</span>
303
-
304
- <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
305
- <span class="ruby-identifier">recalculate!</span>(<span class="ruby-identifier">t</span>,<span class="ruby-identifier">order</span>,<span class="ruby-identifier">memo</span>,&amp;<span class="ruby-identifier">block</span>)
306
- <span class="ruby-keyword">end</span>
307
-
308
- <span class="ruby-keyword">when</span> <span class="ruby-value">:inorder</span>
309
- <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span> <span class="ruby-keyword">unless</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">tree</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">BinaryTree</span>
310
- <span class="ruby-identifier">recalculate!</span>(<span class="ruby-identifier">tree</span>.<span class="ruby-identifier">left</span>,<span class="ruby-identifier">order</span>,&amp;<span class="ruby-identifier">block</span>)
311
-
312
- <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">data</span> = <span class="ruby-keyword">yield</span> <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">memo</span>
313
- <span class="ruby-identifier">memo</span> = <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">data</span>
314
-
315
- <span class="ruby-identifier">recalculate!</span>(<span class="ruby-identifier">tree</span>.<span class="ruby-identifier">right</span>,<span class="ruby-identifier">order</span>,<span class="ruby-identifier">memo</span>,&amp;<span class="ruby-identifier">block</span>)
316
-
317
- <span class="ruby-keyword">end</span>
318
- <span class="ruby-keyword">end</span>
319
- <span class="ruby-keyword">end</span></pre>
320
- </div><!-- recalculate-21-source -->
321
-
322
- </div>
323
-
324
-
325
-
326
-
327
- </div><!-- recalculate-21-method -->
328
-
329
-
330
- <div id="reset-method" class="method-detail ">
331
- <a name="method-i-reset"></a>
332
-
333
-
334
- <div class="method-heading">
335
- <span class="method-name">reset</span><span
336
- class="method-args">()</span>
337
- <span class="method-click-advice">click to toggle source</span>
338
- </div>
339
-
340
-
341
- <div class="method-description">
342
-
343
- <p>Resets tree walker.</p>
344
-
345
-
346
-
347
- <div class="method-source-code" id="reset-source">
348
- <pre>
349
- <span class="ruby-comment"># File ds/trees/tree_walker.rb, line 61</span>
350
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">reset</span>
351
- <span class="ruby-ivar">@visited</span>.<span class="ruby-identifier">clear</span>
352
- <span class="ruby-keyword">self</span>
353
- <span class="ruby-keyword">end</span></pre>
354
- </div><!-- reset-source -->
355
-
356
- </div>
357
-
358
-
359
-
360
-
361
- </div><!-- reset-method -->
362
-
363
-
364
- <div id="summarize-method" class="method-detail ">
365
- <a name="method-i-summarize"></a>
366
-
367
-
368
- <div class="method-heading">
369
- <span class="method-name">summarize</span><span
370
- class="method-args">(direction=:bottomup)</span>
371
- <span class="method-click-advice">click to toggle source</span>
372
- </div>
373
-
374
-
375
- <div class="method-description">
376
-
377
- <p>Summarize tree</p>
378
-
379
-
380
-
381
- <div class="method-source-code" id="summarize-source">
382
- <pre>
383
- <span class="ruby-comment"># File ds/trees/tree_walker.rb, line 120</span>
384
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">summarize</span>(<span class="ruby-identifier">direction</span>=<span class="ruby-value">:bottomup</span>)
385
-
386
- <span class="ruby-keyword">case</span> <span class="ruby-identifier">direction</span>
387
- <span class="ruby-keyword">when</span> <span class="ruby-value">:bottomup</span>
388
- <span class="ruby-identifier">recalculate!</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">tree</span>,<span class="ruby-value">:postorder</span>,<span class="ruby-value">0</span>){<span class="ruby-operator">|</span><span class="ruby-identifier">ar</span><span class="ruby-operator">|</span> <span class="ruby-identifier">ar</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-value">0</span>){<span class="ruby-operator">|</span><span class="ruby-identifier">x</span>,<span class="ruby-identifier">memo</span><span class="ruby-operator">|</span> <span class="ruby-identifier">memo</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">x</span>}}
389
- <span class="ruby-keyword">self</span>.<span class="ruby-identifier">tree</span>
390
- <span class="ruby-keyword">when</span> <span class="ruby-value">:topdown</span>
391
- <span class="ruby-identifier">recalculate!</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">tree</span>,<span class="ruby-value">:preorder</span>,<span class="ruby-value">0</span>){<span class="ruby-operator">|</span><span class="ruby-identifier">x</span>,<span class="ruby-identifier">memo</span><span class="ruby-operator">|</span> <span class="ruby-identifier">memo</span> = <span class="ruby-identifier">memo</span><span class="ruby-operator">+</span><span class="ruby-identifier">x</span>.<span class="ruby-identifier">data</span>}
392
- <span class="ruby-keyword">self</span>.<span class="ruby-identifier">tree</span>
393
- <span class="ruby-keyword">end</span>
394
- <span class="ruby-keyword">end</span></pre>
395
- </div><!-- summarize-source -->
396
-
397
- </div>
398
-
399
-
400
-
401
-
402
- </div><!-- summarize-method -->
403
-
404
-
405
- <div id="traverse-method" class="method-detail ">
406
- <a name="method-i-traverse"></a>
407
-
408
-
409
- <div class="method-heading">
410
- <span class="method-name">traverse</span><span
411
- class="method-args">(order=:bfs,&block)</span>
412
- <span class="method-click-advice">click to toggle source</span>
413
- </div>
414
-
415
-
416
- <div class="method-description">
417
-
418
- <p>Traversing tree in given order: bfs - Breadth-first search - default
419
- postorder - postorder search preorder - preorder search inorder - inorder
420
- search - only for Binary Trees</p>
421
-
422
- <p>If block is given, passes each visited subtree to block. Returns values of
423
- nodes in given order</p>
424
-
425
-
426
-
427
- <div class="method-source-code" id="traverse-source">
428
- <pre>
429
- <span class="ruby-comment"># File ds/trees/tree_walker.rb, line 24</span>
430
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">traverse</span>(<span class="ruby-identifier">order</span>=<span class="ruby-value">:bfs</span>,&amp;<span class="ruby-identifier">block</span>)
431
- <span class="ruby-identifier">reset</span>
432
- <span class="ruby-identifier">tree</span> = <span class="ruby-ivar">@tree</span>
433
-
434
- <span class="ruby-keyword">case</span> <span class="ruby-identifier">order</span>
435
- <span class="ruby-keyword">when</span> <span class="ruby-value">:bfs</span>
436
- <span class="ruby-identifier">traverse_bfs</span> &amp;<span class="ruby-identifier">block</span>
437
- <span class="ruby-keyword">when</span> <span class="ruby-value">:postorder</span>
438
- <span class="ruby-identifier">walk</span>(<span class="ruby-identifier">tree</span>,<span class="ruby-value">:postorder</span>,&amp;<span class="ruby-identifier">block</span>)
439
- <span class="ruby-keyword">when</span> <span class="ruby-value">:preorder</span>
440
- <span class="ruby-identifier">walk</span>(<span class="ruby-identifier">tree</span>,<span class="ruby-value">:preorder</span>, &amp;<span class="ruby-identifier">block</span>)
441
- <span class="ruby-keyword">when</span> (<span class="ruby-value">:inorder</span>)
442
- <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">BinaryTree</span>
443
- <span class="ruby-identifier">walk</span>(<span class="ruby-identifier">tree</span>,<span class="ruby-identifier">order</span>,&amp;<span class="ruby-identifier">block</span>)
444
- <span class="ruby-keyword">end</span>
445
-
446
- <span class="ruby-keyword">return</span> <span class="ruby-identifier">visited</span>
447
- <span class="ruby-keyword">end</span></pre>
448
- </div><!-- traverse-source -->
449
-
450
- </div>
451
-
452
-
453
-
454
-
455
- </div><!-- traverse-method -->
456
-
457
-
458
- <div id="traverse_bfs-method" class="method-detail ">
459
- <a name="method-i-traverse_bfs"></a>
460
-
461
-
462
- <div class="method-heading">
463
- <span class="method-name">traverse_bfs</span><span
464
- class="method-args">()</span>
465
- <span class="method-click-advice">click to toggle source</span>
466
- </div>
467
-
468
-
469
- <div class="method-description">
470
-
471
- <p>Traverses tree in BFS order.</p>
472
-
473
-
474
-
475
- <div class="method-source-code" id="traverse_bfs-source">
476
- <pre>
477
- <span class="ruby-comment"># File ds/trees/tree_walker.rb, line 44</span>
478
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">traverse_bfs</span>
479
- <span class="ruby-identifier">q</span> = <span class="ruby-constant">Queue</span>.<span class="ruby-identifier">new</span>
480
- <span class="ruby-identifier">q</span>.<span class="ruby-identifier">push</span> <span class="ruby-ivar">@tree</span>
481
-
482
- <span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>
483
- <span class="ruby-keyword">break</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">q</span>.<span class="ruby-identifier">empty?</span>
484
- <span class="ruby-identifier">node</span> = <span class="ruby-identifier">q</span>.<span class="ruby-identifier">shift</span>
485
- <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
486
- <span class="ruby-keyword">yield</span> <span class="ruby-identifier">node</span>
487
- <span class="ruby-keyword">else</span>
488
- <span class="ruby-ivar">@visited</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">node</span>.<span class="ruby-identifier">data</span>
489
- <span class="ruby-keyword">end</span>
490
- <span class="ruby-identifier">node</span>.<span class="ruby-identifier">children</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">q</span>.<span class="ruby-identifier">push</span> <span class="ruby-identifier">n</span> } <span class="ruby-keyword">if</span> <span class="ruby-identifier">node</span>.<span class="ruby-identifier">children</span>
491
- <span class="ruby-keyword">end</span>
492
- <span class="ruby-keyword">end</span></pre>
493
- </div><!-- traverse_bfs-source -->
494
-
495
- </div>
496
-
497
-
498
-
499
-
500
- </div><!-- traverse_bfs-method -->
501
-
502
-
503
- <div id="traverse_with_h-method" class="method-detail ">
504
- <a name="method-i-traverse_with_h"></a>
505
-
506
-
507
- <div class="method-heading">
508
- <span class="method-name">traverse_with_h</span><span
509
- class="method-args">(tree,height=nil,&block)</span>
510
- <span class="method-click-advice">click to toggle source</span>
511
- </div>
512
-
513
-
514
- <div class="method-description">
515
-
516
-
517
-
518
-
519
-
520
- <div class="method-source-code" id="traverse_with_h-source">
521
- <pre>
522
- <span class="ruby-comment"># File ds/trees/tree_walker.rb, line 68</span>
523
- <span class="ruby-keyword">def</span> <span class="ruby-identifier">traverse_with_h</span>(<span class="ruby-identifier">tree</span>,<span class="ruby-identifier">height</span>=<span class="ruby-keyword">nil</span>,&amp;<span class="ruby-identifier">block</span>)
524
-
525
- <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
526
- <span class="ruby-identifier">traverse_with_h</span>(<span class="ruby-identifier">t</span>,<span class="ruby-identifier">height</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>,&amp;<span class="ruby-identifier">block</span>)
527
- <span class="ruby-keyword">end</span>
528
-
529
- <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
530
- <span class="ruby-keyword">yield</span> <span class="ruby-identifier">tree</span>, <span class="ruby-identifier">height</span>
531
- <span class="ruby-keyword">else</span>
532
- <span class="ruby-ivar">@visited</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">tree</span>.<span class="ruby-identifier">data</span>
533
- <span class="ruby-keyword">end</span>
534
-
535
- <span class="ruby-keyword">end</span></pre>
536
- </div><!-- traverse_with_h-source -->
537
-
538
- </div>
539
-
540
-
541
-
542
-
543
- </div><!-- traverse_with_h-method -->
544
-
545
-
546
- </div><!-- public-instance-method-details -->
547
-
548
- </div><!-- 5Buntitled-5D -->
549
-
550
-
551
- </div><!-- documentation -->
552
-
553
- <div id="validator-badges">
554
- <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
555
- <p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
556
- Rdoc Generator</a> 2</small>.</p>
557
- </div>
558
-
559
- </body>
560
- </html>
561
-