-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeTravel.java
146 lines (115 loc) · 3.02 KB
/
TreeTravel.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* 1191 : https://www.acmicpc.net/problem/1191
* 참고 사이트 : https://m.blog.naver.com/PostView.nhn?blogId=occidere&logNo=220899936160&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F
*/
import java.util.Scanner;
public class TreeTravel {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int size = Integer.parseInt(scan.nextLine());
Tree tree = new Tree();
for(int i=0; i<size; i++) {
String s = scan.nextLine();
String splited[] = s.split(" ");
tree.add(splited[0].charAt(0), splited[1].charAt(0), splited[2].charAt(0));
}
tree.preorder(tree.getRoot());
System.out.println();
tree.inorder(tree.getRoot());
System.out.println();
tree.postorder(tree.getRoot());
System.out.println();
}
}
class Node {
private char data;
private Node left;
private Node right;
public Node(char data) {
this.data = data;
left = null;
right = null;
}
public char getData() {
return data;
}
public void setData(char data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public Node getInstance() {
return this;
}
@Override
public String toString() {
return "Node [data=" + data + ", left=" + left + ", right=" + right + "]";
}
}
class Tree {
Node root;
public Node getRoot() {
return root;
}
public void add(char data, char left, char right) {
if(root==null) {
root = new Node(data);
if(left!='.') { root.setLeft(new Node(left)); }
if(right!='.') { root.setRight(new Node(right)); }
}
else {
searchPut(data, root, left, right);
}
}
//94번째 줄에서 search만 하고 node를 반환한 뒤 그것에 put을 하도록 구현하고 싶었으나 재귀적 특성때문에 방법을 찾지못함
//혹시나 찾은 분이 계시다면 comment달아주시면 감사하겠습니다.
public Node searchPut(char data, Node node, char left, char right) {
if(node.getData()==data) {
if(left!='.') { node.setLeft(new Node(left)); }
if(right!='.') { node.setRight(new Node(right)); }
}
if(node.getLeft()!=null)
searchPut(data, node.getLeft(), left, right);
if(node.getRight()!=null)
searchPut(data, node.getRight(), left, right);
return null;
}
// 전위
public void preorder(Node node) {
System.out.print(node.getData());
if (node.getLeft() != null)
preorder(node.getLeft());
if (node.getRight() != null)
preorder(node.getRight());
}
// 중위
public void inorder(Node node) {
if (node.getLeft() != null)
inorder(node.getLeft());
System.out.print(node.getData());
if (node.getRight() != null)
inorder(node.getRight());
}
// 후위
public void postorder(Node node) {
if (node.getLeft() != null)
postorder(node.getLeft());
if (node.getRight() != null)
postorder(node.getRight());
System.out.print(node.getData());
}
@Override
public String toString() {
return "Tree [root=" + root + "]";
}
}