2024. 1. 10. 23:58ใCS - Roadmap.sh/1. Data structures
Data structures (with python) - 8.Tree - 8.3 Full Binary Tree
๐ก A full Binary tree is a special type of binary tree in which every parent node/internal node has either two or no children. It is also known as a proper binary tree.
์ ์ด์ง ํธ๋ฆฌ๋ ๋ชจ๋ ๋ถ๋ชจ ๋ ธ๋/๋ด๋ถ ๋ ธ๋๊ฐ ๋ ๊ฐ์ ์์์ ๊ฐ๊ฑฐ๋ ์ ํ ๊ฐ์ง ์๋ ํน๋ณํ ์ ํ์ ์ด์ง ํธ๋ฆฌ์ ๋๋ค. ์ ์ ์ด์ง ํธ๋ฆฌ๋ผ๊ณ ๋ ํฉ๋๋ค.
์ ์ด์ง ํธ๋ฆฌ(Full Binary Tree)๋ ๋ชจ๋ ๋ ธ๋๊ฐ 0๊ฐ ๋๋ 2๊ฐ์์์ ๋ ธ๋๋ฅผ ๊ฐ์ง๋ ์ด์ง ํธ๋ฆฌ๋ฅผ ๋งํฉ๋๋ค.
์ฆ, ํ ๋ ธ๋๊ฐ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ง๋ค๋ฉด, ๊ทธ๊ฒ์ ๋ฐ๋์ ๋ ๊ฐ์ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ ธ์ผ ํฉ๋๋ค. ์ด๋ฌํ ํน์ง ๋๋ถ์ ์ ์ด์ง ํธ๋ฆฌ(Full Binary Tree)๋ ๊ท ๋ฑํ๊ฒ ๋ถํฌ๋ ํธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋๋ค.
In Python
์ ์ด์ง ํธ๋ฆฌ๋ฅผ ํ์ด์ฌ์์ ๊ตฌํํ๋๊ฒ์ ์ด์ง ํธ๋ฆฌ ๊ตฌํ๊ณผ ๊ทธ๋ค์ง ๋ค๋ฅด์ง ์์ต๋๋ค.
๋ ธ๋ ํด๋์ค ์ ์, ํธ๋ฆฌ๋ฅผ ๊ตฌ์ฑํ๋ ๋ ธ๋๋ฅผ ์ฐ๊ฒฐํ๋ ๋ฐฉ์์ผ๋ก ๋ง์ฐฌ๊ฐ์ง์ด๋ค.
๋ค๋ง, ์ ์ด์ง ํธ๋ฆฌ์ ์กฐ๊ฑด์ ๋ง๊ฒ ๋ชจ๋ ๋ ธ๋๊ฐ 0๊ฐ ๋๋ 2๊ฐ์ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ ธ์ผ ํ๋ค๋ ์ ์ ์ฃผ์ํด์ผ ํ๋ค.
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)
root.right.left = Node(6)
root.right.right = Node(7)
์์ ์ฝ๋๋ 1์ ๋ฃจํธ ๋ ธ๋๋ก ๊ฐ์ง๋ ์ ์ด์ง ํธ๋ฆฌ(Full Binary Tree)๋ฅผ ๋ง๋๋ ์์์ ๋๋ค.
ํ์ฉ
์ ์ด์ง ํธ๋ฆฌ(Full Binary Tree)๋ ์ด์ง ํ(binary heap)๊ณผ ๊ฐ์ ์๋ฃ ๊ตฌ์กฐ์ ๊ตฌํ์ ์ฌ์ฉ๋ฉ๋๋ค.
์ด์ง ํ ์ ์ ์ด์ง ํธ๋ฆฌ(Full Binary Tree)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ฌ, ๋ฐ์ดํฐ์ ์ต์๊ฐ ๋๋ ์ต๋๊ฐ์ ๋น ๋ฅด๊ฒ ์ฐพ๋ ๋ฐ ์ฌ์ฉํ๋ ์๋ฃ๊ตฌ์กฐ ์ ๋๋ค.
(์ฐจํ์ถ๊ฐ)
'CS - Roadmap.sh > 1. Data structures' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Data structures (with python) - 8.Tree - 8.5 Balanced Tree (1) | 2024.01.11 |
---|---|
Data structures (with python) - 8.Tree - 8.4 Complete Binary Tree (1) | 2024.01.11 |
Data structures (with python) - 8.Tree - 8.2 Binary Search Tree (0) | 2024.01.09 |
Data structures (with python) - 8.Tree - 8.1 Binary Tree (0) | 2024.01.09 |
Data structures (with python) - 7. Hash Table (0) | 2024.01.08 |