Notice
Recent Posts
Recent Comments
Today
Total
01-10 22:06
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 1991] ํŠธ๋ฆฌ ์ˆœํšŒ (JAVA) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[๋ฐฑ์ค€ 1991] ํŠธ๋ฆฌ ์ˆœํšŒ (JAVA)

WonderJay 2023. 1. 22. 13:52
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://www.acmicpc.net/problem/1991

 

1991๋ฒˆ: ํŠธ๋ฆฌ ์ˆœํšŒ

์ฒซ์งธ ์ค„์—๋Š” ์ด์ง„ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 26)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ๋…ธ๋“œ์™€ ๊ทธ์˜ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋…ธ๋“œ์˜ ์ด๋ฆ„์€ A๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์•ŒํŒŒ

www.acmicpc.net

๋”๋ณด๊ธฐ

์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ „์œ„ ์ˆœํšŒ(preorder traversal), ์ค‘์œ„ ์ˆœํšŒ(inorder traversal), ํ›„์œ„ ์ˆœํšŒ(postorder traversal)ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์™€ ๊ฐ™์€ ์ด์ง„ ํŠธ๋ฆฌ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด,

  • ์ „์œ„ ์ˆœํšŒํ•œ ๊ฒฐ๊ณผ : ABDCEFG // (๋ฃจํŠธ) (์™ผ์ชฝ ์ž์‹) (์˜ค๋ฅธ์ชฝ ์ž์‹)
  • ์ค‘์œ„ ์ˆœํšŒํ•œ ๊ฒฐ๊ณผ : DBAECFG // (์™ผ์ชฝ ์ž์‹) (๋ฃจํŠธ) (์˜ค๋ฅธ์ชฝ ์ž์‹)
  • ํ›„์œ„ ์ˆœํšŒํ•œ ๊ฒฐ๊ณผ : DBEGFCA // (์™ผ์ชฝ ์ž์‹) (์˜ค๋ฅธ์ชฝ ์ž์‹) (๋ฃจํŠธ)

๊ฐ€ ๋œ๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ์ด์ง„ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 26)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ๋…ธ๋“œ์™€ ๊ทธ์˜ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋…ธ๋“œ์˜ ์ด๋ฆ„์€ A๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ ๋งค๊ฒจ์ง€๋ฉฐ, ํ•ญ์ƒ A๊ฐ€ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ๋œ๋‹ค. ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” .์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ „์œ„ ์ˆœํšŒ, ๋‘˜์งธ ์ค„์— ์ค‘์œ„ ์ˆœํšŒ, ์…‹์งธ ์ค„์— ํ›„์œ„ ์ˆœํšŒํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ ์ค„์— N๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ์„ ๊ณต๋ฐฑ ์—†์ด ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1 

7
A B C
B D .
C E F
E . .
F . G
D . .
G . .

์˜ˆ์ œ ์ถœ๋ ฅ 1 

ABDCEFG
DBAECFG
DBEGFCA

ํŠธ๋ฆฌ์˜ ์ˆœํšŒ ๋ฐฉ์‹์—๋Š” Postorder , Inorder, Preorder ์ด ์žˆ๋‹ค.

 

์„ธ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

 

Tree ์˜ ๊ฐ ๋…ธ๋“œ๋ฅผ Node ๋กœ ์ง€์ •ํ•˜๊ณ 

 

graph ํ˜•ํƒœ๋กœ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์–ด๋„ ๋˜์ง€๋งŒ,

 

2์ฐจ์› ๋ฐฐ์—ด์— ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด ๋” ์‰ฝ๋‹ค.

 

tree[i][0] =  i ๋…ธ๋“œ์˜ left

tree[i][1] =  i ๋…ธ๋“œ์˜ right

 

์œ„์™€ ๊ฐ™์ด 2์ฐจ์› ๋ฐฐ์—ด์— ํŠธ๋ฆฌ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ˆœํšŒํ•ด์ฃผ์ž.

 

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    static int[][] tree;

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine()); // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜
        tree = new int[26][2];
        for (int i = 0; i < n; i++) {
            String [] temp = br.readLine().split(" ");
            int now_node = temp[0].charAt(0)-'A';
            char left = temp[1].charAt(0);
            char right = temp[2].charAt(0);

            if(left=='.'){
                tree[now_node][0] = -1;
            } else {
                tree[now_node][0] = left-'A';
            }
            if(right=='.'){
                tree[now_node][1] = -1;
            } else {
                tree[now_node][1] = right-'A';
            }
        }
        preorder(0);
        System.out.println();
        inorder(0);
        System.out.println();
        postorder(0);
        System.out.println();
    }
    static void preorder(int now){
        if(now==-1)
            return;
        System.out.print((char)(now+'A'));
        preorder(tree[now][0]);
        preorder(tree[now][1]);
    }
    static void postorder(int now){
        if(now==-1)
            return;
        postorder(tree[now][0]);
        postorder(tree[now][1]);
        System.out.print((char)(now+'A'));
    }
    static void inorder(int now){
        if(now==-1)
            return;
        inorder(tree[now][0]);
        System.out.print((char)(now+'A'));
        inorder(tree[now][1]);
    }
}
728x90
๋ฐ˜์‘ํ˜•
LIST
Comments