-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreadthFirstSearch.java
54 lines (47 loc) · 1.48 KB
/
BreadthFirstSearch.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
import java.util.LinkedList;
/*
Print the nodes in a binary tree level-wise. For example, the following should print 1, 2, 3, 4, 5.
1
/ \
2 3
/ \
4 5
*/
public class BreadthFirstSearch {
public static void main(String[] args) {
Node rootNode = new Node(
"1",
new Node("2", null, null),
new Node(
"3",
new Node("4", null, null),
new Node("5", null, null)
)
);
printNodeValuesLevelWise(rootNode);
}
private static void printNodeValuesLevelWise(Node rootNode) {
LinkedList<Node> queue = new LinkedList<>();
queue.add(rootNode);
while (!queue.isEmpty()) {
Node nextNodeToBeProcessed = queue.removeFirst();
System.out.println("Node value: " + nextNodeToBeProcessed.value);
if(nextNodeToBeProcessed.leftChild != null) {
queue.add(nextNodeToBeProcessed.leftChild);
}
if(nextNodeToBeProcessed.rightChild != null) {
queue.add(nextNodeToBeProcessed.rightChild);
}
}
}
private static class Node {
private String value;
private Node leftChild;
private Node rightChild;
private Node(String value, Node leftChild, Node rightChild) {
this.value = value;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
}
}