[Algorithm] ๐ŸšŽ๋ฐฑ์ค€ 1991 ํŠธ๋ฆฌ ์ˆœํšŒ

HaJingJingยท2021๋…„ 5์›” 27์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
51/119

0. ๋ฌธ์ œ

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

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

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

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

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

1. ์•„์ด๋””์–ด

ํŠธ๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์ž˜ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒŒ ํ•ต์‹ฌ

๐Ÿ’ก ๊ฐ’๊ณผ ๋…ธ๋“œ๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๋Š” Node ํด๋ž˜์Šค ์ƒ์„ฑ
๐Ÿ’ก Node๋“ค์„ ์ด์„ Tree ํด๋ž˜์Šค ์ƒ์„ฑ
๐Ÿ’ก Tree์•ˆ์— Node๋ฅผ ์ƒ์„ฑํ•˜๋Š” createNode ํ•จ์ˆ˜
๐Ÿ’ก Node์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š” searchNode ํ•จ์ˆ˜ ์ƒ์„ฑ
๐Ÿ’ก ํŠธ๋ฆฌ ์ˆœํšŒ ํ•จ์ˆ˜ ์ƒ์„ฑ

2. ํ•ต์‹ฌ ํ’€์ด

1) ๊ฐ’๊ณผ ๋…ธ๋“œ๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๋Š” Node ํด๋ž˜์Šค ์ƒ์„ฑ

static class Node{
	char value;
	Node left;
	Node right;
		
	Node(char value){
		this.value = value;
	}
}
  1. Node๋“ค์„ ์ด์„ Tree ํด๋ž˜์Šค ์ƒ์„ฑ
static class Tree{
	Node root;
	...
}
  1. Tree์•ˆ์— Node๋ฅผ ์ƒ์„ฑํ•˜๋Š” createNode ํ•จ์ˆ˜
void createNode(char value, char left, char right) {
	if(root == null) {
		root = new Node(value);
				
		if(left != '.')
			root.left = new Node(left);
		if(right != '.')
			root.right = new Node(right);
	} else {
		searchNode(root, value, left, right);
	}
}
  1. Node์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š” searchNode ํ•จ์ˆ˜ ์ƒ์„ฑ
void searchNode(Node root, char value, char left, char right) {
	if(root == null) 
    		return;
	else if(root.value == value) {
		if(left != '.')
			root.left = new Node(left);
		if(right != '.')
			root.right = new Node(right);
	} else {
		searchNode(root.left, value, left, right);
		searchNode(root.right, value, left, right);
	}
}
  1. ํŠธ๋ฆฌ ์ˆœํšŒ ํ•จ์ˆ˜ ์ƒ์„ฑ
public void preorder(Node root) {
	System.out.print(root.value);
	if(root.left != null) preorder(root.left);
	if(root.right != null) preorder(root.right);
}
		
public void inorder(Node root) {
	if(root.left != null) inorder(root.left);
	System.out.print(root.value);
	if(root.right != null) inorder(root.right);
}
		
public void postorder(Node root) {
	if(root.left != null) postorder(root.left);
	if(root.right != null) postorder(root.right);
	System.out.print(root.value);
}

3. ์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Graph_6 {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		Tree tree = new Tree();
		
		for(int i=0; i<n; i++) {
			char[] c = br.readLine().replaceAll(" ","").toCharArray();
			tree.createNode(c[0], c[1], c[2]);
		}
		
		tree.preorder(tree.root);
		System.out.println();
		tree.inorder(tree.root);
		System.out.println();
		tree.postorder(tree.root);

	}
	
	static class Node{
		char value;
		Node left;
		Node right;
		
		Node(char value){
			this.value = value;
		}
	}
	
	static class Tree{
		Node root;
		
		void createNode(char value, char left, char right) {
			if(root == null) {
				root = new Node(value);
				
				if(left != '.')
					root.left = new Node(left);
				if(right != '.')
					root.right = new Node(right);
			} else {
				searchNode(root, value, left, right);
			}
		}
		
		void searchNode(Node root, char value, char left, char right) {
			if(root == null) return;
			else if(root.value == value) {
				if(left != '.')
					root.left = new Node(left);
				if(right != '.')
					root.right = new Node(right);
			} else {
				searchNode(root.left, value, left, right);
				searchNode(root.right, value, left, right);
			}
		}
		
		public void preorder(Node root) {
			System.out.print(root.value);
			if(root.left != null) preorder(root.left);
			if(root.right != null) preorder(root.right);
		}
		
		public void inorder(Node root) {
			if(root.left != null) inorder(root.left);
			System.out.print(root.value);
			if(root.right != null) inorder(root.right);
		}
		
		public void postorder(Node root) {
			if(root.left != null) postorder(root.left);
			if(root.right != null) postorder(root.right);
			System.out.print(root.value);
		}
		
	}

}

4. ๊ฒฐ๊ณผ

์„ฑ๊ณตโœจ

profile
๐ŸŒฑ์ดˆ๋ณด ๊ฐœ๋ฐœ์ž๐ŸŒฑ

0๊ฐœ์˜ ๋Œ“๊ธ€