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

2024. 1. 9. 21:32ใ†CS - Roadmap.sh/1. Data structures

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 “children”).

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

 

Binary Tree

๐Ÿ’ก A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.

์ด์ง„ ํŠธ๋ฆฌ๋Š” ๊ฐ ๋…ธ๋“œ์— ์ตœ๋Œ€ ๋‘ ๊ฐœ์˜ ์ž์‹์ด ์žˆ๋Š” ํŠธ๋ฆฌ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ, ์™ผ์ชฝ ์ž์‹๊ณผ ์˜ค๋ฅธ์ชฝ ์ž์‹์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

์œ„ํ‚คํ”ผ๋””์•„

 

์ด์ง„ํŠธ๋ฆฌ(binary tree)๋Š” ๊ฐ ๋…ธ๋“œ๊ฐ€ ์ตœ๋Œ€ ๋‘๊ฐœ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ํŠธ๋ฆฌํ˜•ํƒœ์˜ ์ž๋ฃŒ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

์ด์ง„ ํŠธ๋ฆฌ์˜ ๊ฐ ๋…ธ๋“œ๋Š” '์™ผ์ชฝ', '์˜ค๋ฅธ์ชฝ' ์ž์‹๋…ธ๋“œ๋ฅผ ๊ฐ€์งˆ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋“ค์€ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

 

ํŠนํžˆ ์ด์ง„ ๊ฒ€์ƒ‰ํŠธ๋ฆฌ(Binary Search Tree, BST, ๋ฐ”๋กœ ๋‹ค์Œ์žฅ์—์„œ ๋‹ค๋ฃธ), ํž™(heap), ํŠธ๋ผ์ด(Trie) ๋“ฑ ๋งŽ์€ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ธฐ๋ฐ˜์ด ๋œ๋‹ค.

ํŠธ๋ผ์ด(Trie)

์ด๋Ÿฌํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์€ ๋ฐ์ดํ„ฐ ์ •๋ ฌ, ๊ฒ€์ƒ‰, ์šฐ์„ ์ˆœ์œ„ ํ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋ฅผ ํšจ๊ด’๊ฑฐ์œผ๋กœ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

 

In Python

์ด์ง„ํŠธ๋ฆฌ๋ฅผ ํŒŒ์ด์ฌ์—์„œ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด Node ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

์ด ํด๋ž˜์Šค๋Š” ๋…ธ๋“œ์˜ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” 'data' ํ•„๋“œ์™€ ์™ผ/์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” 'left', 'right'ํ•„๋“œ๋ฅผ ๊ฐ€์ง„๋‹ค.

 

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

# ๋…ธ๋“œ ์ƒ์„ฑ
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

1์„ ๋ฃจํŠธ ๋…ธ๋“œ๋กœ ๊ฐ€์ง€๋Š” ์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ์ƒ์„ฑ ํ•ด ๋ณด์•˜๋‹ค.

์ด์ง„ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ๋…ธ๋“œ๋Š” 1, 1์˜ ์ž์‹๋…ธ๋“œ๋Š” 2์™€ 3, 2์˜ ์ž์‹๋…ธ๋“œ๋Š” 4์™€ 5์ด๋‹ค.

 

ํ™œ์šฉ

์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ํ™œ์šฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ ํŠธ๋ฆฌ ์ˆœํšŒ (Tree Traversal)๊ฐ€ ์žˆ๋‹ค.

๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š” ์ˆœ์„œ์— ๋”ฐ๋ผ ์ „์œ„ ์ˆœํšŒ(Pre-order), ์ค‘์œ„ ์ˆœํšŒ(In-order), ํ›„์œ„ ์ˆœํšŒ(Post-order)๋กœ ๋‚˜๋‰œ๋‹ค.

 

์ „์œ„ ์ˆœํšŒ๋Š” ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ , ๊ทธ ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ, ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ์ˆœ์„œ๋กœ ์ˆœํšŒํ•œ๋‹ค.

์ค‘์œ„ ์ˆœํšŒ๋Š” ์™ผ์ชฝ, ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ˆœ์„œ๋กœ ์ˆœํšŒ

ํ›„์œ„ ์ˆœํšŒ๋Š” ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ, ๋…ธ๋“œ ์ˆœ์„œ๋กœ ๋ฐฉ๋ฌธํ•œ๋‹ค.

 

์ด์ง„ ํŠธ๋ฆฌ์˜ ์ˆœํšŒ๋Š” ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์™œ? => ์ด๋Š” ๊ฐ ๋…ธ๋“œ์—์„œ ๋™์ผํ•œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋–„๋ฌธ์ด๋‹ค.

๊ฐ ํŠธ๋ฆฌ์ˆœํšŒ์˜ ๋ฐฉ๋ฒ•์„ ํŒŒ์ด์ฌ์—์„œ ๊ตฌํ˜„ํ•ด ๋ด…๋‹ˆ๋‹ค.

# ์ „์œ„ ์ˆœํšŒ
def pre_order(node):
    if node is not None:
        print(node.data, end=' ')
        pre_order(node.left)
        pre_order(node.right)

# ์ค‘์œ„ ์ˆœํšŒ
def in_order(node):
    if node is not None:
        in_order(node.left)
        print(node.data, end=' ')
        in_order(node.right)

# ํ›„์œ„ ์ˆœํšŒ
def post_order(node):
    if node is not None:
        post_order(node.left)
        post_order(node.right)
        print(node.data, end=' ')

 

์œ„ ํ•จ์ˆ˜๋“ค์€ ์ „์œ„, ์ค‘์œ„, ํ›„์œ„ ์ˆœํšŒ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ด์ง„ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

728x90