forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFSUsingQueue.java
87 lines (72 loc) · 2.5 KB
/
BFSUsingQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.rampatra.trees;
import com.rampatra.base.BinaryNode;
import com.rampatra.base.BinaryTree;
import com.rampatra.base.LinkedQueue;
import com.rampatra.base.Queue;
import java.util.NoSuchElementException;
import static java.lang.System.out;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/26/15
* @time: 7:34 PM
*/
public class BFSUsingQueue {
/**
* Breadth first traversal (Level-order traversal using Queue).
*
* @param node a tree node with left and right references and a value of type {@code E}
* @param <E> the type of value that the {@code node} holds
*/
public static <E extends Comparable<E>> void breadthFirstTraversalUsingQueue(BinaryNode<E> node) {
Queue<BinaryNode<E>> queue = new LinkedQueue<>();
breadthFirstTraversalUsingQueue(node, queue);
}
private static <E extends Comparable<E>> void breadthFirstTraversalUsingQueue(BinaryNode<E> node,
Queue<BinaryNode<E>> queue) {
if (node != null) {
printValue(node);
queue.add(node.left);
queue.add(node.right);
}
try {
breadthFirstTraversalUsingQueue(queue.remove(), queue);
} catch (NoSuchElementException e) {
return;
}
}
private static <E extends Comparable<E>> void printValue(BinaryNode<E> node) {
if (node == null) return;
out.print(node.value);
}
/**
* Level order traversal using queue but iteratively.
*
* @param root the root node from where the traversal should start
* @param <E> type of the {@code value} that {@code BinaryNode} holds
*/
public static <E extends Comparable<E>> void breadthFirstTraversalUsingQueueIterative(BinaryNode<E> root) {
if (root == null) return;
Queue<BinaryNode<E>> q = new LinkedQueue<>();
q.add(root);
while (!q.isEmpty()) {
BinaryNode<E> node = q.remove();
out.print(node.value);
if (node.left != null) q.add(node.left);
if (node.right != null) q.add(node.right);
}
}
public static void main(String[] args) {
BinaryTree<Integer> bt = new BinaryTree<>();
bt.put(6);
bt.put(3);
bt.put(5);
bt.put(7);
bt.put(8);
bt.put(9);
breadthFirstTraversalUsingQueue(bt.root);
System.out.println();
breadthFirstTraversalUsingQueueIterative(bt.root);
}
}