
노드와 링크로 구성된 자료구조(그래프의 일종, Cycle 없음)
계층적 구조를 나타낼 때 사용
트리의 구조

특징
왼쪽 자식 노드의 키는 부모 노드의 키보다 작음
오른쪽 자식 노드의 키는 부모 노드의 키보다 큼
각각의 서브 트리도 이진 탐색 트리를 유지
중복된 키를 허용하지 않음

특징
탐색
삽입
삭제


import java.io.*;
import java.util.HashMap;
public class Main {
static class Node{
char data;
Node left, right;
Node(char data){
this.data = data;
left = right = null;
}
}
static HashMap<Character, Node> tree = new HashMap<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
for(int i=0; i<N; i++){
String[] str = br.readLine().split(" ");
char parent = str[0].charAt(0);
char left = str[1].charAt(0);
char right = str[2].charAt(0);
tree.putIfAbsent(parent, new Node(parent));
Node parentNode = tree.get(parent);
if(left != '.'){
tree.putIfAbsent(left, new Node(left));
parentNode.left = tree.get(left);
}
if(right != '.'){
tree.putIfAbsent(right, new Node(right));
parentNode.right = tree.get(right);
}
}
Node start = tree.get('A');
preOrder(start);
System.out.println();
inOrder(start);
System.out.println();
postOrder(start);
}
public static void preOrder(Node start){
if(start == null){
return;
}
System.out.print(start.data);
preOrder(start.left);
preOrder(start.right);
}
public static void inOrder(Node start){
if(start == null){
return;
}
inOrder(start.left);
System.out.print(start.data);
inOrder(start.right);
}
public static void postOrder(Node start){
if(start == null){
return;
}
postOrder(start.left);
postOrder(start.right);
System.out.print(start.data);
}
}