๊ด€๋ฆฌ ๋ฉ”๋‰ด

Tech Log ๐Ÿ› ๏ธ

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

์•Œ๊ณ ๋ฆฌ์ฆ˜

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

sehaan 2023. 3. 26. 19:28

 

๋ถ„์„


์ด ๋ฌธ์ œ๋ฅผ ํ’€๋ ค๋ฉด ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ํŠธ๋ฆฌ์˜ ์ˆœํ™˜์— ๋Œ€ํ•ด ์•Œ์•„์•ผํ•œ๋‹ค.

 

ํŠธ๋ฆฌ๋ž€?

๊ฐ€๊ณ„๋„์™€ ๊ฐ™์€ ๊ณ„์ธต์ ์ธ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.

 

๋ฃจํŠธ๋…ธ๋“œ :  ๋ถ€๋ชจ๊ฐ€ ์—†๋Š” ์ตœ์ƒ์œ„ ๋…ธ๋“œ (F)๋‹จ๋ง๋…ธ๋“œ : ์ž์‹์ด ์—†๋Š” ๋…ธ๋“œ (A,C,E,H)ํฌ๊ธฐ : ํŠธ๋ฆฌ์— ํฌํ•จ ๋œ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ (8)๊นŠ์ด : ๊นŠ์ด ๋ฃจํŠธ ๋…ธ๋“œ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๋†’์ด :  ๊นŠ์ด ์ค‘ ์ตœ๋Œ“๊ฐ’ (3)์ฐจ์ˆ˜ : ๊ฐ ๋…ธ๋“œ์˜ ๊ฐ„์„  ๊ฐœ์ˆ˜

 

* ํŠธ๋ฆฌ์˜ ํฌ๊ธฐ๊ฐ€ N ์ผ๋•Œ, ์ „์ฒด ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๋Š” N-1 ์ด๋‹ค.

 

ํŠธ๋ฆฌ์˜ ์ˆœํšŒ 

ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ์— ํฌํ•จ ๋œ ๋…ธ๋“œ๋ฅผ ํŠน์ •ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•œ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

๋Œ€ํ‘œ์ ์ธ ์ˆœํšŒ์˜ ๋ฐฉ๋ฒ•์€ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

1. ์ „์œ„ ์ˆœํšŒ : ๋ฃจํŠธ๋…ธ๋“œ -> ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ -> ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ ์ˆœ์œผ๋กœ ๋ฐฉ๋ฌธํ•œ๋‹ค.

                       F B A D C E G I H

2. ์ค‘์œ„ ์ˆœํšŒ :  ์™ผ์ชฝ ์ž์‹๋…ธ๋“œ -> ๋ฃจํŠธ ๋…ธ๋“œ -> ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ

                       A B C D E F G H I

3. ํ›„์œ„ ์ˆœํšŒ :  ์™ผ์ชฝ ์ž์‹๋…ธ๋“œ -> ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ -> ๋ฃจํŠธ ๋…ธ๋“œ

                       A C E D B H I G F

 

์œ„์˜ ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•ด์„œ ์ž…๋ ฅ์œผ๋กœ ๋…ธ๋“œ๊ฐ„ ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ๋ฐ›์€ ๋’ค์— ์ˆœํšŒ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

์ฝ”๋“œ


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


public class Main {
    //์ตœ์ƒ์œ„ ๋ฃจํŠธ ๋…ธ๋“œ ์ƒ์„ฑ
    static Node root = new Node('A',null,null);
    //ํŠธ๋ฆฌ์— ๋“ค์–ด๊ฐˆ ๋…ธ๋“œ ํด๋ž˜์Šค
    static class Node{
        char name;
        Node left;
        Node right;

        public Node(char name, Node left, Node right) {
            this.name = name;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

        for(int i=0;i<num;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            InsertNode(root,st.nextToken().charAt(0),st.nextToken().charAt(0),st.nextToken().charAt(0));
        }

        preOrder(root);
        System.out.println();
        inOrder(root);
        System.out.println();
        postOrder(root);
    }

    private static void InsertNode(Node root, char value, char left, char right) {
        //ํ˜„์žฌ ๋…ธ๋“œ์™€ value ์˜ ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด
        if(root.name == value){
            root.left = (left == '.' ? null : new Node(left,null,null));
            root.right = (right == '.' ? null : new Node(right,null,null));
        }

        else{
            //์™ผ์ชฝ ์ž์‹๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ ํƒ์ƒ‰ ํ›„ ์ž์‹ ๋…ธ๋“œ ์ถ”๊ฐ€
            if(root.left != null) InsertNode(root.left,value,left,right);
            //์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ ํƒ์ƒ‰ ํ›„ ์ž์‹ ๋…ธ๋“œ ์ถ”๊ฐ€
            if(root.right != null) InsertNode(root.right,value,left,right);
        }
    }
    //์ „์œ„ ์ˆœํšŒ
    static void preOrder(Node node){
        if(node == null) return;
        System.out.print(node.name+" ");
        preOrder(node.left);
        preOrder(node.right);
    }
    //์ค‘์œ„ ์ˆœํšŒ
    static void inOrder(Node node){
        if(node == null) return;
        inOrder(node.left);
        System.out.print(node.name+" ");
        inOrder(node.right);
    }
    //ํ›„์œ„ ์ˆœํšŒ
    static void postOrder(Node node){
        if(node == null) return;
        postOrder(node.left);
        postOrder(node.right);
        System.out.print(node.name+" ");
    }


}

 

์ฐธ๊ณ  ์‚ฌ์ดํŠธ

https://www.youtube.com/watch?v=i5yHkP1jQmo&t=470s 

https://velog.io/@gandi0330/Java-%EB%B0%B1%EC%A4%80-%ED%8A%B8%EB%A6%AC-%EC%88%9C%ED%9A%8C-1991%EB%B2%88

 

[Java] ๋ฐฑ์ค€ / ํŠธ๋ฆฌ ์ˆœํšŒ / 1991๋ฒˆ

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

velog.io