-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1991.java
More file actions
95 lines (77 loc) · 1.93 KB
/
1991.java
File metadata and controls
95 lines (77 loc) · 1.93 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
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
import java.io.*;
import java.util.*;
class Node {
char data;
Node left;
Node right;
Node(char data) {
this.data = data;
}
}
class Tree {
Node root;
public void createNode(char data, char leftData, char rightData) {
if(root == null) {
root = new Node(data);
if(leftData != '.') {
root.left = new Node(leftData);
}
if(rightData != '.') {
root.right = new Node(rightData);
}
} else {
searchNode(root, data, leftData, rightData);
}
}
public void searchNode(Node root, char data, char leftData, char rightData) {
if(root == null) {
return;
} else if(root.data == data) {
if(leftData != '.') {
root.left = new Node(leftData);
}
if(rightData != '.') {
root.right = new Node(rightData);
}
} else {
searchNode(root.left, data, leftData, rightData);
searchNode(root.right, data, leftData, rightData);
}
}
// 전위순회
public void preorder(Node root){
System.out.print(root.data);
if(root.left!=null) preorder(root.left);
if(root.right!=null) preorder(root.right);
}
// 중위순회
public void inorder(Node root){
if(root.left!=null) inorder(root.left);
System.out.print(root.data);
if(root.right!=null) inorder(root.right);
}
// 후위순회
public void postorder(Node root){
if(root.left!=null) postorder(root.left);
if(root.right!=null) postorder(root.right);
System.out.print(root.data);
}
}
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Tree tree = new Tree();
for(int i = 0; i < N; i++) {
char[] data;
data = br.readLine().replaceAll(" ", "").toCharArray();
tree.createNode(data[0], data[1], data[2]);
}
tree.preorder(tree.root);
System.out.println();
tree.inorder(tree.root);
System.out.println();
tree.postorder(tree.root);
br.close();
}
}