카테고리 없음

[백준 1991 ] 트리 순회 JAVA

90만식 2022. 2. 13. 18:42
728x90
사용언어 : JAVA
문제 URL : https://www.acmicpc.net/problem/1991

1.문제

-문제설명

이진 트리를 입력받아 전위 순회(preorder traversal), 중위 순회(inorder traversal), 후위 순회(postorder traversal)한 결과를 출력하는 프로그램을 작성하시오.

 

예를 들어 위와 같은 이진 트리가 입력되면,

  • 전위 순회한 결과 : ABDCEFG // (루트) (왼쪽 자식) (오른쪽 자식)
  • 중위 순회한 결과 : DBAECFG // (왼쪽 자식) (루트) (오른쪽 자식)
  • 후위 순회한 결과 : DBEGFCA // (왼쪽 자식) (오른쪽 자식) (루트)

 

 

-제약사항

  • 첫째 줄에는 이진 트리의 노드의 개수 N(1 ≤ N ≤ 26)이 주어진다.
  • 둘째 줄부터 N개의 줄에 걸쳐 각 노드와 그의 왼쪽 자식 노드, 오른쪽 자식 노드가 주어진다.
  • 노드의 이름은 A부터 차례대로 알파벳 대문자로 매겨지며, 항상 A가 루트 노드가 된다. 자식 노드가 없는 경우에는 .으로 표현한다.

Example 1

1.입력
7
A B C
B D .
C E F
E . .
F . G
D . .
G . .

2.출력
ABDCEFG
DBAECFG
DBEGFCA

 

2.코드구현(JAVA)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

public class test {

    static class Node{
        String data;
        Node right;
        Node left;

        public Node(String data){
            this.data = data;
        }

    }

    static class Tree{

        public Node root;

        public void makeNode(String rootNode, String left, String right){

            // node가 존재하지 않을 경우
            if( root == null){
                root = new Node(rootNode);

                if( !left.equals(".") ){
                    root.left = new Node(left);
                }
                if( !right.equals(".") ){
                    root.right = new Node(right);
                }

            }else{
                SearchSubNode(root, rootNode, left, right);
            }

        }

        public void SearchSubNode(Node root, String rootNode, String left, String right){

            if( root == null){
                return;
            }else if( root.data.equals(rootNode)){
                if( !left.equals(".") ){
                    root.left = new Node(left);
                }
                if( !right.equals(".") ){
                    root.right = new Node(right);
                }
            }else{
                SearchSubNode(root.left, rootNode, left, right);
                SearchSubNode(root.right, rootNode, left, right);
            }

        }

        // 중위 순회
        // left -> Root -> Right
        public void inorder(Node node){
            if( node != null){
                inorder(node.left);
                System.out.print(node.data);
                inorder(node.right);
            }
        }
        // 전위 순회
        // Root- > left -> right
        public void preorder(Node node){
            if( node != null){
                System.out.print(node.data);
                preorder(node.left);
                preorder(node.right);
            }
        }

        // 후위 순회
        // left -> right -> root
        public void postorder(Node node){
            if(node != null){
                postorder(node.left);
                postorder(node.right);
                System.out.print(node.data);
            }
        }
    }
    public static void main(String[] args) throws IOException {
        Tree tree = new Tree();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());

        StringTokenizer st;
        String rootStr;
        String left;

        for (int i=0; i<n; i++) {
            String s = br.readLine();
            s.replaceAll(" ", "");

            st = new StringTokenizer(s);

            tree.makeNode(st.nextToken(), st.nextToken(), st.nextToken());
        }

        tree.preorder(tree.root);
        System.out.println();
        tree.inorder(tree.root);
        System.out.println();
        tree.postorder(tree.root);

        br.close();

    }


}
728x90