-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.java
More file actions
70 lines (61 loc) · 1.61 KB
/
BFS.java
File metadata and controls
70 lines (61 loc) · 1.61 KB
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
package chapter4TreesAndGraphs;
import java.util.LinkedList;
import chapter4TreesAndGraphs.BTree.Node;
public class BFS {
public static void main(String[] args){
BTree bt = new BTree();
Node n5 = bt.new Node(7,null,null);
Node n6 = bt.new Node(6,null,null);
Node n3 = bt.new Node(5,null,null);
Node n4 = bt.new Node(4,null,null);
Node n1 = bt.new Node(2,n3,n4);
Node n2 = bt.new Node(3,n5,n6);
Node root = bt.new Node(1,n1,n2);
bt.root = root;
doBFSUsingTwoQ(bt.root);
doBFSUsingOneQ(bt.root);
}
public static void doBFSUsingTwoQ(Node root){
if(root==null) return;
LinkedList<Node> currentLevel = new LinkedList<Node>();
LinkedList<Node> nextLevel = new LinkedList<Node>();
currentLevel.add(root);
while(!currentLevel.isEmpty()){
Node curNode = currentLevel.poll();
if(curNode!=null){
System.out.print(curNode.data+" ");
nextLevel.add(curNode.left);
nextLevel.add(curNode.right);
}
if(currentLevel.isEmpty()){
System.out.println();
currentLevel.addAll(nextLevel);
nextLevel.clear();
}
}
}
public static void doBFSUsingOneQ (Node root){
LinkedList<Node> level = new LinkedList<Node>();
int curLevelNum =1;
int nextLevelNum =0;
level.add(root);
while(curLevelNum!=0){
Node curNode = level.poll();
curLevelNum--;
System.out.print(curNode.data + " ");
if(curNode.left!=null){
level.add(curNode.left);
nextLevelNum ++;
}
if(curNode.right!=null){
level.add(curNode.right);
nextLevelNum ++;
}
if(curLevelNum==0){
System.out.println();
curLevelNum = nextLevelNum;
nextLevelNum = 0;
}
}
}
}