-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2_tree_4.java
More file actions
82 lines (76 loc) · 2.78 KB
/
day2_tree_4.java
File metadata and controls
82 lines (76 loc) · 2.78 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class day2_tree_4 {
/**
* 날짜 : 2021.01.04
* 문제 유형 : 트리
* 문제 url : https://www.acmicpc.net/problem/1991
* 문제 요약
* - 제목 : 트리 순회
* : 이진 트리를 입력받아 전위 순회(preorder traversal), 중위 순회(inorder traversal), 후위 순회(postorder traversal)
* : 결과를 출력하라.
*/
public void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
Tree tree = new Tree();
int N = Integer.parseInt(bf.readLine()); //노드의 개수
char[] data;
for (int i = 0; i < N; i++) {
data = bf.readLine().replaceAll(" ","").toCharArray();
tree.add(data[0], data[1], data[2]);
}
tree.preorder(tree.root);
System.out.println();
tree.inorder(tree.root);
System.out.println();
tree.postorder(tree.root);
}
class Node{
public Node left, right;
public char data;
public Node(char data){
this.data = data;
}
}
class Tree{
Node root;
public void add(char data, char left, char right){
if (root == null){
//빈 tree에 처음 넣을 때
if(data != '.') root = new Node(data);
if(left != '.') root.left = new Node(left);
if(right != '.') root.right = new Node(right);
}
else search(root, data, left, right);
}
public void search(Node root, char data, char left, char right){
if(root == null) return;
else if(root.data == data){
if(left != '.') root.left = new Node(left);
if(right != '.') root.right = new Node(right);
}else{
search(root.left, data, left, right);
search(root.right, data, left, right);
}
}
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);
}
}
}