-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFSRecurstion.java
97 lines (79 loc) · 2.07 KB
/
DFSRecurstion.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.*;
import java.io.*;
/*
* Node, Graph 등 class는 main Class와 별도로 정의
* pre는 recursion도 pre, post도 마찬가지, in도 마찬가지
* */
class Node {
public String data;
public Node left;
public Node right;
Node(String name) {
data = name;
}
void addLeft(Node N) {
left = N;
}
void addRight(Node N) {
right = N;
}
}
class Graph {
public Node rootNode;
Graph(Node N) {
rootNode = N;
}
void dfsInorder(Node curNode) {
if (curNode != null) {
dfsInorder(curNode.left);
System.out.print(curNode.data + " -> ");
dfsInorder(curNode.right);
}
}
void dfsPreorder(Node curNode) {
if (curNode != null) {
System.out.print(curNode.data + " -> ");
dfsPreorder(curNode.left);
dfsPreorder(curNode.right);
}
}
void dfsPostorder(Node curNode) {
if (curNode != null) {
dfsPostorder(curNode.left);
dfsPostorder(curNode.right);
System.out.print(curNode.data + " -> ");
}
}
}
public class DFS {
public static void main(String[] args) {
Node A = new Node("A");
Node B = new Node("B");
Node C = new Node("C");
Node D = new Node("D");
Node E = new Node("E");
Node F = new Node("F");
Node G = new Node("G");
Node H = new Node("H");
Node I = new Node("I");
Node J = new Node("J");
A.addLeft(B);
A.addRight(C);
B.addLeft(D);
C.addLeft(E);
E.addLeft(F);
F.addLeft(G);
F.addRight(H);
H.addLeft(I);
I.addLeft(J);
Graph graph = new Graph(A);
System.out.println("Inorder");
graph.dfsInorder(graph.rootNode);
System.out.println();
System.out.println("Preorder");
graph.dfsPreorder(graph.rootNode);
System.out.println();
System.out.println("Postorder");
graph.dfsPostorder(graph.rootNode);
}
}