-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadth First Search(Recursive).java
More file actions
59 lines (59 loc) · 1.43 KB
/
Breadth First Search(Recursive).java
File metadata and controls
59 lines (59 loc) · 1.43 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
import java.util.*;
class Node{
int data;
Node left,right;
Node(int value){
data=value;
left=right=null;
}
}
//edit this code
class Tree{
Node root;
Tree(){
root=null;
}
int height(Node root){
if(root==null){
return 0;
}
int leftheight=1+height(root.left);
int rightheight=1+height(root.right);
if(leftheight>rightheight){
return leftheight;
}else {
return rightheight;
}
}
void BFStree(){
int h=height(root);
for(int i=1;i<=h;i++){
levelsisreturn(root,i,1);
}
}
void levelsisreturn(Node root,int level,int currentlevel){
if(root==null){
return;
}
if(level==currentlevel){
System.out.println(root.data+" ");
}else{
levelsisreturn(root.left,level,currentlevel+1);
levelsisreturn(root.right,level,currentlevel+1);
}
}
}
public class BFSRecursive {
public static void main(String[] args){
Tree tree=new Tree();
tree.root=new Node(1);
tree.root.left=new Node(2);
tree.root.right=new Node(3);
tree.root.left.left=new Node(4);
tree.root.left.right=new Node(5);
tree.root.left.left.right=new Node(6);
tree.root.right.left=new Node(7);
tree.root.right.right=new Node(8);
tree.BFStree();
}
}