-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.java
51 lines (41 loc) · 1.13 KB
/
BFS.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
import java.util.Arrays;
import java.util.Queue;
import java.util.LinkedList;
class BFS {
public static void main(String[] args) {
Node head = new Node(7);
head.left = new Node(4);
head.left.left = new Node(2);
head.left.right = new Node(3);
head.right = new Node(12);
head.right.left = new Node(8);
System.out.println(Arrays.toString(BreadthFirstSearch(head)));
}
private static int[] BreadthFirstSearch(Node head) {
int[] returnArray = new int[6];
int count = 0;
Queue<Node> q = new LinkedList<Node>();
q.add(head);
while (!q.isEmpty()) {
Node current = q.remove();
returnArray[count] = current.data;
count++;
if(current.left != null) {
q.add(current.left);
}
if(current.right != null) {
q.add(current.right);
}
}
return returnArray;
}
}
class Node {
int data;
Node left, right;
public Node(int n) {
this.data = n;
this.left = null;
this.right = null;
}
}