|
14 | 14 | * successor(T key)
|
15 | 15 | * searchMin()
|
16 | 16 | * searchMax()
|
17 |
| - * printInorder() |
18 |
| - * printPreorder() |
19 |
| - * printPostorder() |
20 |
| - * printLevelorder() |
| 17 | + * getInorder() |
| 18 | + * getPreorder() |
| 19 | + * getPostorder() |
| 20 | + * getLevelorder() |
21 | 21 | */
|
22 | 22 | public class BinarySearchTree<T extends Comparable<T>, V> {
|
23 | 23 |
|
@@ -170,83 +170,89 @@ private T successor(Node<T, V> node) {
|
170 | 170 | }
|
171 | 171 |
|
172 | 172 | /**
|
173 |
| - * Prints out in-order traversal of tree rooted at node |
| 173 | + * Stores in-order traversal of tree rooted at node into a list |
| 174 | + * |
174 | 175 | * @param node node which the tree is rooted at
|
175 | 176 | */
|
176 |
| - private void printInorder(Node<T, V> node) { |
| 177 | + private void getInorder(Node<T, V> node, List<String> result) { |
177 | 178 | if (node == null) {
|
178 | 179 | return;
|
179 | 180 | }
|
180 | 181 |
|
181 | 182 | if (node.left != null) {
|
182 |
| - printInorder(node.left); |
| 183 | + getInorder(node.left, result); |
183 | 184 | }
|
184 | 185 |
|
185 |
| - System.out.print(node.toString() + " "); |
| 186 | + result.add(node.toString()); |
186 | 187 |
|
187 | 188 | if (node.right != null) {
|
188 |
| - printInorder(node.right); |
| 189 | + getInorder(node.right, result); |
189 | 190 | }
|
190 | 191 | }
|
191 | 192 |
|
192 | 193 | /**
|
193 |
| - * Prints out pre-order traversal of tree rooted at node |
| 194 | + * Stores in-order traversal of tree rooted at node into a list |
| 195 | + * |
194 | 196 | * @param node node which the tree is rooted at
|
195 | 197 | */
|
196 |
| - private void printPreorder(Node<T, V> node) { |
| 198 | + private void getPreorder(Node<T, V> node, List<String> result) { |
197 | 199 | if (node == null) {
|
198 | 200 | return;
|
199 | 201 | }
|
200 | 202 |
|
201 |
| - System.out.print(node.toString() + " "); |
| 203 | + result.add(node.toString()); |
202 | 204 |
|
203 | 205 | if (node.left != null) {
|
204 |
| - printPreorder(node.left); |
| 206 | + getPreorder(node.left, result); |
205 | 207 | }
|
206 | 208 |
|
207 | 209 | if (node.right != null) {
|
208 |
| - printPreorder(node.right); |
| 210 | + getPreorder(node.right, result); |
209 | 211 | }
|
210 | 212 | }
|
211 | 213 |
|
212 | 214 | /**
|
213 |
| - * Prints out post-order traversal of tree rooted at node |
| 215 | + * Stores post-order traversal of tree rooted at node into a list |
| 216 | + * |
214 | 217 | * @param node node which the tree is rooted at
|
215 | 218 | */
|
216 |
| - private void printPostorder(Node<T, V> node) { |
| 219 | + private void getPostorder(Node<T, V> node, List<String> result) { |
217 | 220 | if (node == null) {
|
218 | 221 | return;
|
219 | 222 | }
|
220 | 223 |
|
221 | 224 | if (node.left != null) {
|
222 |
| - printPostorder(node.left); |
| 225 | + getPostorder(node.left, result); |
223 | 226 | }
|
224 | 227 |
|
225 | 228 | if (node.right != null) {
|
226 |
| - printPostorder(node.right); |
| 229 | + getPostorder(node.right, result); |
227 | 230 | }
|
228 | 231 |
|
229 |
| - System.out.print(node.toString() + " "); |
| 232 | + result.add(node.toString()); |
230 | 233 | }
|
231 | 234 |
|
232 | 235 | /**
|
233 |
| - * Prints out level-order traversal of tree rooted at node |
| 236 | + * Stores level-order traversal of tree rooted at node into a list |
| 237 | + * |
234 | 238 | * @param node node which the tree is rooted at
|
235 | 239 | */
|
236 |
| - private void printLevelorder(Node<T, V> node) { |
| 240 | + private void getLevelorder(Node<T, V> node, List<String> result) { |
237 | 241 | if (node == null) {
|
238 | 242 | return;
|
239 | 243 | }
|
240 |
| - Queue<Node<T, V>> q = new LinkedList<>(); |
241 |
| - q.add(node); |
242 |
| - while (!q.isEmpty()) { |
243 |
| - Node<T, V> curr = q.poll(); |
244 |
| - System.out.print(curr.toString() + " "); |
245 |
| - if (curr.left != null) { |
246 |
| - q.add(curr.left); |
| 244 | + |
| 245 | + Queue<Node<T, V>> queue = new LinkedList<>(); |
| 246 | + queue.add(node); |
| 247 | + while (!queue.isEmpty()) { |
| 248 | + Node<T, V> current = queue.poll(); |
| 249 | + result.add(current.toString()); |
| 250 | + |
| 251 | + if (current.left != null) { |
| 252 | + queue.add(current.left); |
247 | 253 | }
|
248 |
| - if (curr.right != null) { |
249 |
| - q.add(curr.right); |
| 254 | + if (current.right != null) { |
| 255 | + queue.add(current.right); |
250 | 256 | }
|
251 | 257 | }
|
252 | 258 | }
|
@@ -364,27 +370,27 @@ public Node<T, V> searchMax() {
|
364 | 370 | }
|
365 | 371 | }
|
366 | 372 |
|
367 |
| - public void printInorder() { |
368 |
| - System.out.print("In-order: "); |
369 |
| - printInorder(root); |
370 |
| - System.out.println(); |
| 373 | + public List<String> getInorder() { |
| 374 | + List<String> result = new ArrayList<>(); |
| 375 | + getInorder(root, result); |
| 376 | + return result; |
371 | 377 | }
|
372 | 378 |
|
373 |
| - public void printPreorder() { |
374 |
| - System.out.print("Pre-order: "); |
375 |
| - printPreorder(root); |
376 |
| - System.out.println(); |
| 379 | + public List<String> getPreorder() { |
| 380 | + List<String> result = new ArrayList<>(); |
| 381 | + getPreorder(root, result); |
| 382 | + return result; |
377 | 383 | }
|
378 | 384 |
|
379 |
| - public void printPostorder() { |
380 |
| - System.out.print("Post-order: "); |
381 |
| - printPostorder(root); |
382 |
| - System.out.println(); |
| 385 | + public List<String> getPostorder() { |
| 386 | + List<String> result = new ArrayList<>(); |
| 387 | + getPostorder(root, result); |
| 388 | + return result; |
383 | 389 | }
|
384 | 390 |
|
385 |
| - public void printLevelorder() { |
386 |
| - System.out.print("Level-order: "); |
387 |
| - printLevelorder(root); |
388 |
| - System.out.println(); |
| 391 | + public List<String> getLevelorder() { |
| 392 | + List<String> result = new ArrayList<>(); |
| 393 | + getLevelorder(root, result); |
| 394 | + return result; |
389 | 395 | }
|
390 | 396 | }
|
0 commit comments