01-24 13:36
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„1991 :: ํŠธ๋ฆฌ ์ˆœํšŒ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„1991 :: ํŠธ๋ฆฌ ์ˆœํšŒ

miinsun 2022. 4. 4. 23:18

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ „์œ„ ์ˆœํšŒ(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

 

โ€‹

๐Ÿ’ป  Main.java

  • ํŠธ๋ฆฌ์˜ ๊ธฐ์ดˆ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฌธ์ œ!
  • ํŠธ๋ฆฌ ๋ฌธ์ œ๋ฅผ ๋„ˆ๋ฌด ์˜ค๋žœ๋งŒ์— ํ’€์–ด์„œ ์กฐ๊ธˆ ํ—ค๋ฉจ์ง€๋งŒ, ๋‹ค์‹œ ํŠธ๋ฆฌ๋ฅผ ์ตํž ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ๋ฌธ์ œ์˜€๋‹ค~
/* ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ - 1991 :: ํŠธ๋ฆฌ ์ˆœํšŒ */

import java.util.*;
import java.io.*;

// ๋…ธ๋“œ
class Node { 
	public char data; 
	Node lt, rt;
	
	Node (char data){ 
		this.data = data;
	} 
}
	
class Tree{
	Node root;
	
	// ํŠธ๋ฆฌ ์ž…๋ ฅ
	void add(char data, char left, char right) {
		// ๋ฃจํŠธ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ๋ฐ์ดํ„ฐ ์ž…๋ ฅ
		if(root == null) {
			if(data != '.')
				root = new Node(data);
			if(left != '.')
				root.lt = new Node(left);
			if(right != '.')
				root.rt = new Node(right);
		}
		else // ๋ฃจํŠธ ์™ธ์—๋Š” ์ž๊ธฐ ์ž๋ฆฌ๋ฅผ ์ฐพ์•„์คŒ
			search(root, data, left, right);
	}
	
	// ๊ฒ€์ƒ‰
	void search(Node root, char data, char left, char right) {
		// ๋…ธ๋“œ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ์ข…๋ฃŒ
		if(root == null) return;
		else if(root.data == data) { // ์ฐพ๊ณ  ์žˆ๋˜ ๋…ธ๋“œ๋ฅผ ์ฐพ์œผ๋ฉด ์ •๋ณด ์ž…๋ ฅ
			if(left != '.') 
				root.lt = new Node(left);
			if(right != '.')
				root.rt = new Node(right);
		}
		else { // ์™ผ์ชฝ ํฌ์ธํ„ฐ, ์˜ค๋ฅธ์ชฝ ํฌ์ธํ„ฐ๋กœ ๋…ธ๋“œ ์ฐพ๊ธฐ
			search(root.lt, data, left, right);
			search(root.rt, data, left, right);
		}
	}
	
	
	// ์ „์œ„์ˆœํšŒ
	void preOrder(Node root) {
		//root -> left -> right
		System.out.print(root.data);
		if(root.lt != null) preOrder(root.lt);
		if(root.rt != null) preOrder(root.rt);
	}
	
	// ์ค‘์œ„์ˆœํšŒ
	void inOrder(Node root) {
		// left -> root -> right
		if(root.lt != null) inOrder(root.lt);
		System.out.print(root.data);
		if(root.rt != null) inOrder(root.rt);
	}
	
	// ํ›„์œ„์ˆœํšŒ
	void postOrder(Node root) {
		// left -> right -> root
		if(root.lt != null) postOrder(root.lt);
		if(root.rt != null) postOrder(root.rt);
		System.out.print(root.data);
	}
}

public class Main {		
	public static void main(String[] args) throws IOException{
		Scanner sc = new Scanner (System.in);
		int n = sc.nextInt();
		
		// ํŠธ๋ฆฌ ์ •๋ณด ์ž…๋ ฅ
		Tree tree = new Tree();
		for(int i = 0; i < n; i++) {
			tree.add(sc.next().charAt(0), sc.next().charAt(0), sc.next().charAt(0));
		}
			
		tree.preOrder(tree.root);
		System.out.println();
		tree.inOrder(tree.root);
		System.out.println();
		tree.postOrder(tree.root);
		
		sc.close();
	}
}
Comments