2024. 1. 9. 22:06ใCS - Roadmap.sh/1. Data structures
Data structures (with python) - 8.Tree - 8.2 Binary Search Tree
๐ก A binary search tree, also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node’s left subtree and less than the ones in its right subtree.
์ ๋ ฌ๋ ์ด์ง ํธ๋ฆฌ ๋๋ ์ ๋ ฌ๋ ์ด์ง ํธ๋ฆฌ๋ผ๊ณ ๋ ํ๋ ์ด์ง ๊ฒ์ ํธ๋ฆฌ๋ ๊ฐ ๋ด๋ถ ๋ ธ๋์ ํค๊ฐ ํด๋น ๋ ธ๋์ ์ผ์ชฝ ํ์ ํธ๋ฆฌ์ ์๋ ๋ชจ๋ ํค๋ณด๋ค ํฌ๊ณ ์ค๋ฅธ์ชฝ ํ์ ํธ๋ฆฌ์ ์๋ ํค๋ณด๋ค ์์ ๋ฃจํ ๋ ์ด์ง ํธ๋ฆฌ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๋๋ค.
8.1 binary tree ์ ์ด์ด์
Binary search tree ์ ๋๋ค.
Binary Search Tree ๋ ์ด๋ฆ์ ์ Search๊ฐ ๋ค์ด๊ฐ ์์๊น?
์ด๋ฆ๋ง ๋ด์๋ search ๊ฐ ์ค๊ฐ์ ๋ค์ด๊ฐ ์๋๋ฐ ๋ณด์ด๋ฏ์ด, ์ด์งํธ๋ฆฌ์ ํ ์ข ๋ฅ์ด์ง๋ง search ํ๋๋ฐ (ํน์ ์กฐ๊ฑดํ์) ์ฅ์ ์ด ์์ด์ ๋ฐ๋ก ๋ฐฐ์ด๋ค๊ณ ๋ณผ ์ ์๋ค.
์ด์งํธ๋ฆฌ์ ์ ์๋ 8.1 ์์ ๊ฐ ๋ ธ๋๊ฐ ์ต๋ ๋๊ฐ์ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ง๋ ํธ๋ฆฌ ๊ตฌ์กฐ์ด๋ค. ๋ ธ๋์ ๋ฐฐ์น์ ์ ํ์ด๋ ๊ท์น์ด ์๋ค.
๋ฐ๋ฉด, ์ด์ง ๊ฒ์ ํธ๋ฆฌ(์ดํ BST)์์๋ ๋ ธ๋์ ๋ฐฐ์น์ ํน๋ณํ ๊ท์น์ด ์๋ค.
BST์์๋ ๊ฐ ๋ ธ๋์ ๋ํด ๊ทธ ๋ ธ๋์ ๊ฐ์ด ์ผ์ชฝ ์๋ธํธ๋ฆฌ์ ๋ชจ๋ ๋ ธ๋์ ๊ฐ๋ณด๋ค ํฌ๊ณ , ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ์ ๋ชจ๋ ๋ ธ๋์ ๊ฐ๋ณด๋ค ์์์ผ ํ๋ค๋ ๊ท์น์ด ์๋ค.
์ด ๊ท์น ๋๋ถ์ BST์์๋ ๊ฐ์ ๊ฒ์ ํ ๋, ํ์ฌ ๋ ธ๋์ ๊ฐ๊ณผ ์ฐพ๊ณ ์ ํ๋ ๊ฐ์ ๋น๊ตํ์ฌ ์ผ์ชฝ or ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ก ์ด๋ํ๋ฉด ๋ฉ๋๋ค.
๋ฐ๋ผ์ BST๋ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ๊ฒ์ํ ์ ์๋ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๊ฒ ๋ฉ๋๋ค. ์ด๋ฌํ ์ด์ ๋ก 'Search'๊ฐ ์ด๋ฆ์ ํํจ ๋์ด ์์ต๋๋ค.
BST์ ํน์ง
- ๊ฐ ๋ ธ๋์ ํค๋ ๊ณ ์
- ์ผ์ชฝ ์๋ธํธ๋ฆฌ์ ๋ชจ๋ ๋ ธ๋์ ํค๋ ๊ทธ ๋ฃจํธ์ ํค๋ณด๋ค ์๋ค.
- ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ์ ๋ชจ๋ ๋ ธ๋์ ํค๋ ๊ทธ ๋ฃจํธ์ ํค๋ณด๋ค ํฌ๋ค.
- ์ผ์ชฝ, ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ ์ด์ง ๊ฒ์ ํธ๋ฆฌ์ด๋ค.
์ด๋ฌํ ํน์ง ๋๋ถ์, BST๋ ๊ฒ์, ์ฝ์ , ์ญ์ ๋ฑ์ ์ฐ์ฐ์ ํจ์จ์ ๊ธ๋ก ์ํํ ์ ์์ต๋๋ค.
ํ๊ท ์ ๊ธ๋ก ์ด๋ฌํ ์ฐ์ฐ๋ค์ ์๊ฐ ๋ณต์ก๋๋ O(log N) ์ ๋๋ค.
In Python
Python ์์ BST๋ฅผ ๊ตฌํํ๋ ค๋ฉด ์ด์งํธ๋ฆฌ ๋์ ๋น์ทํ๊ฒ ์ ์ฌํ๊ฒ ๋ ธ๋ ํด๋์ค๋ฅผ ์์ฑํ๊ณ , ์ด ํด๋์ค์ ๊ฒ์, ์ฝ์ , ์ญ์ ๋ฉ์๋๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
class BST:
def __init__(self):
self.root = None
def insert(self, key):
if self.root is None:
self.root = Node(key)
else:
self._insert(self.root, key)
def _insert(self, node, key):
if key < node.key:
if node.left is None:
node.left = Node(key)
else:
self._insert(node.left, key)
else:
if node.right is None:
node.right = Node(key)
else:
self._insert(node.right, key)
def search(self, key):
return self._search(self.root, key)
def _search(self, node, key):
if node is None or node.key == key:
return node
if key < node.key:
return self._search(node.left, key)
return self._search(node.right, key)
BST ํด๋์ค๋ BST์ ๋ฃจํธ ๋ ธ๋๋ฅผ ์ ์ฅํ๋ฉฐ,
insert ๋ฉ์๋์ search ๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค.
Insert ๋ฉ์๋๋ ์๋ก์ด ํค๋ฅผ ์ฝ์ , search ๋ฉ์๋๋ ์ฃผ์ด์ง ํค๋ฅผ ๊ฐ์ง ๋ ธ๋๋ฅผ ๊ฒ์ํฉ๋๋ค.
์ด ๋ฉ์๋ค๋ค์ _insert , _search ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์ฌ๊ท์ ์ผ๋ก ์๋ํ๋ค.
BST๋ ์ด๋ฐ ๋ฐฉ์์ผ๋ก ๊ตฌํํ ์ ์์ง๋ง, ์์ ์ฝ๋๋ ๊ท ํ ์กํ ํธ๋ฆฌ๋ฅผ ์ ์งํ์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
์ต์ ์ ๊ฒฝ์ฐ ํธ๋ฆฌ๊ฐ ํ ์ชฝ์ผ๋ก ์น์ฐ์ณ์ ธ์ ์๊ฐ๋ณต์ก๋๊ฐ O(n)์ด ๋ ์ ์๋ค.
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ AVL ํธ๋ฆฌ๋ ๋ ๋-๋ธ๋ ํธ๋ฆฌ ๋ฑ์ ๊ท ํ ๊ฒ์ ํธ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
(๊ท ํ ๊ฒ์ํธ๋ฆฌ๋ ์ฐจํ ๋ค๋ฃฌ๋ค.)
'CS - Roadmap.sh > 1. Data structures' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Data structures (with python) - 8.Tree - 8.4 Complete Binary Tree (1) | 2024.01.11 |
---|---|
Data structures (with python) - 8.Tree - 8.3 Full Binary Tree (1) | 2024.01.10 |
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 |
Data structures (with python) - 6. Queue (2) | 2024.01.08 |