3.7.1 Pre-Order Traversal(์ „์œ„ ์ˆœํšŒ)

2024. 2. 8. 21:29ใ†CS - Roadmap.sh/3. Common Algorithms

CS - 3. Common Algorithms - 3.7 Tree - 3.7.1 Pre-Order Traversal

Tree Algorithms

๐Ÿ’กA tree is non-linear and a hierarchical data structure consisting of a collection of nodes such that each node of the tree stores a value and a list of references to other nodes (the “children”).

ํŠธ๋ฆฌ๋Š” ๋น„์„ ํ˜•์ ์ด๋ฉฐ ๋…ธ๋“œ ๋ชจ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ณ„์ธต์  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ, ํŠธ๋ฆฌ์˜ ๊ฐ ๋…ธ๋“œ๊ฐ€ ๊ฐ’๊ณผ ๋‹ค๋ฅธ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ฐธ์กฐ ๋ชฉ๋ก("์ž์‹")์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

Pre-Order Traversal

๐Ÿ’กPre-order traversal is a tree traversal algorithm that visits the root node first, then recursively traverses the left subtree, followed by the right subtree.

์‚ฌ์ „ ์ˆœํšŒ ํƒ์ƒ‰(์ „์œ„ ์ˆœํšŒ)์€ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ๋ฐฉ๋ฌธํ•œ ๋‹ค์Œ ์™ผ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํƒ์ƒ‰ํ•œ ๋‹ค์Œ ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ํŠธ๋ฆฌ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

 

 

 

์ž๋ฃŒ๊ตฌ์กฐ์—์„œ Tree ๋ฅผ ๋‹ค๋ค„๋ดค์Šต๋‹ˆ๋‹ค. 

https://parkpakrsu.tistory.com/336

 

Data structures (with python) - 8.Tree - 8.1 Binary Tree

Data structures (with python) - 8.Tree - 8.1 Binary Tree Tree ๐Ÿ’ก A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such that each node of the tree stores a value and a list of references to other nodes (the “chi

parkpakrsu.tistory.com

์ด์ œ ํŠธ๋ฆฌ์— ๊ด€๋ จ๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

http://www.ktword.co.kr/test/view/view.php?no=5560

Pre-Order Traversal ์ด๋ž€?

 

๋จผ์ € ํŠธ๋ฆฌ๋ฅผ ์ „์œ„ ์ˆœํšŒ ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๅ‰ไฝๅทกๅปป ์ „์œ„, Pre-order, ์•ž์ž๋ฆฌ ๋ถ€ํ„ฐ ์ˆœํšŒํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

ํŠธ๋ฆฌ๋‚˜ ๊ทธ๋ž˜ํ”„๋ฅผ ์ˆœํšŒํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ, ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๊ฐ€์žฅ ๋จผ์ € ๋ฐฉ๋ฌธํ•œ ํ›„, ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ, ๊ทธ๋ฆฌ๊ณ  ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ์ˆœ์œผ๋กœ ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ์–ด, ํŠธ๋ฆฌ์˜ ๊ตฌ์กฐ๋ฅผ ๋ณต์‚ฌํ•˜๊ฑฐ๋‚˜ ํŠธ๋ฆฌ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

In Python

์ด์ œ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•ด Pre-Order Traversal์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def pre_order_traversal(node):
    if node is not None:
        print(node.value, end=" ")
        pre_order_traversal(node.left)
        pre_order_traversal(node.right)

 

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)

pre_order_traversal(root) # 1 2 4 5 3 6 7

 

 

์œ„์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ์ „์œ„ ์ˆœํšŒ ํ•˜๋ฉด ์ด๋ ‡์Šต๋‹ˆ๋‹ค.

1-2,3 ์—์„œ ๋‚ฎ์€ 2๋ฒˆ๋…ธ๋“œ๋กœ ์ง„์ž… (1-2),

2-4,5 ์—์„œ ๋‚ฎ์€๋…ธ๋“œ ์ˆœ์„œ๋Œ€๋กœ 2-4, ๊ทธ๋‹ค์Œ 2-5, ๋” ๋‚ด๋ ค๊ฐˆ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋˜๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.

1-3 ์ง„์ž… ํ›„ 3-6,7 ๋„ ์œ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋Œ€๋กœ ํƒ์ƒ‰.

๋”ฐ๋ผ์„œ 1,2,4,5,3,6,7 ์ˆœ์„œ๋Œ€๋กœ ํƒ์ƒ‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

 

 

์ฐธ๊ณ 

์ •๋ณดํ†ต์‹ ๊ธฐ์ˆ ์šฉ์–ดํ•ด์„ค

http://www.ktword.co.kr/test/view/view.php?no=5560

 

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

ํŠธ๋ฆฌ ํƒ์ƒ‰, Pre-order Traversal, ์ „์œ„ ์ˆœํšŒ, In-order Traversal, ์ค‘์œ„ ์ˆœํšŒ, Post-order Traversal, ํ›„์œ„ ์ˆœํšŒ

www.ktword.co.kr

 

 

728x90