Computer Science/알고리즘

백준 Java | BOJ 1991 트리 순회

토마토. 2023. 2. 1. 14:40
package DAY03.P1991;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;

class Tree {
    public Node root;
}
class Node {
    public char name;
    public Node left_node;
    public Node right_node;
    Node(char name){
        this.name = name;
        left_node = null;
        right_node = null;
    }
}
public class Main {
    public static int N;
    public static Tree tree;
    public static ArrayList<Node> node_list;
    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/DAY03/P1991/input.txt"));
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(bf.readLine());
        tree = new Tree();
        node_list = new ArrayList<>();

        for (int i=0;i<N;i++){
            node_list.add(new Node((char)(i+'A')));
        }
        tree.root = node_list.get(0);

        for (int i=0;i<N;i++){
            String information = bf.readLine();
            String[] info = information.split(" ");
            char name = info[0].charAt(0);
            Node tmp_node = node_list.get(name-'A');
            String left_child = info[1];
            String right_child = info[2];
            if (!left_child.equals(".")){
                tmp_node.left_node = node_list.get(left_child.charAt(0)-'A');
            }
            if (!right_child.equals(".")){
                tmp_node.right_node = node_list.get(right_child.charAt(0)-'A');
            }
            char left;
            char right;
            if (tmp_node.left_node == null){
                left = '.';
            } else {
                left = tmp_node.left_node.name;
            }
            if (tmp_node.right_node == null){
                right = '.';
            } else {
                right = tmp_node.right_node.name;
            }
            // System.out.println(tmp_node.name + " -> " + left + ", " + right);

        }
        pre_order_traverse(tree.root);
        System.out.println();
        in_order_traverse(tree.root);
        System.out.println();
        post_order_traverse(tree.root);
    }
    public static void pre_order_traverse(Node node){
        // 현재 노드
        if (node==null){
            return;
        }
        System.out.print(node.name);
        // 왼쪽 노드
        pre_order_traverse(node.left_node);
        // 오른쪽 노드
        pre_order_traverse(node.right_node);
    }
    public static void in_order_traverse(Node node){
        if (node==null){
            return;
        }
        // 왼쪽 노드
        in_order_traverse(node.left_node);
        // 현재 노드
        System.out.print(node.name);
        // 오른쪽 노드
        in_order_traverse(node.right_node);
    }
    public static void post_order_traverse(Node node){
        if (node==null){
            return;
        }
        // 왼쪽 노드
        post_order_traverse(node.left_node);
        // 오른쪽 노드
        post_order_traverse(node.right_node);
        // 현재 노드
        System.out.print(node.name);
    }
}