-
Notifications
You must be signed in to change notification settings - Fork 0
/
Depth First Search Traversal.java
58 lines (58 loc) · 1.44 KB
/
Depth First Search Traversal.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
//DFS
class Node{
int data;
Node left,right;
Node(int value){
data=value;
left=right=null;
}
}
class Tree{
Node root;
Tree(){
root=null;
}
void preorder(Node root){
if(root==null){
return;
}
System.out.print(root.data+" ");
preorder(root.left);
preorder(root.right);
}
void inorder(Node root){
if(root==null){
return;
}
inorder(root.left);
System.out.print(root.data+" ");
inorder(root.right);
}
void postorder(Node root){
if(root==null){
return;
}
postorder(root.left);
postorder(root.right);
System.out.print(root.data+" ");
}
}
public class DFS {
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);
System.out.println("========preorder=========");
tree.preorder(tree.root);
System.out.println("\n========Inorder=========");
tree.inorder(tree.root);
System.out.println("\n========postorder=========");
tree.postorder(tree.root);
}
}