๐ ์ด์ง ํธ๋ฆฌ (binary Search tree) - ์ํ
ํธ๋ฆฌ ์ํ
ํธ๋ฆฌ ์ํ๋ ํธ๋ฆฌ์ ์ ์ฅ๋ ๊ฐ์ ๋ชจ๋ ํ์ธํ ๋ ์ฌ์ฉํ๋ค. ์ํ ๋ฐฉ๋ฒ์ ๋ฐ๋ผ ๊ฐ์ ํ์ธํ๋ ์์๊ฐ ๋ค๋ฅด๋ค.์ํ์ ์ข ๋ฅ๋ก๋ ์ ์ ์ํ (preorder traversal), ์ค์ ์ํ(inorder traversal), ํ์ ์ํ(postorder traversal)์ด ์๋ค.
๐ต ์ ์ ์ํ (preorder traversal)
๐จ ์์ฑ๋์ด ์๋ ํธ๋ฆฌ๋ฅผ ๋ค๋ฅธ ์๋ฒ์ ๋ฆฌ์คํธ ํํ๋ก ๋ณด๋ด์ ๊ทธ ์๋ฒ์์ ๋ค์ ํธ๋ฆฌ๋ฅผ ๊ตฌ์ฑํ ๋ ์ฌ์ฉ
๋ ธ๋ > ์ผ์ชฝ ์๋ธํธ๋ฆฌ > ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ ์์ผ๋ก ์ํํ๋ค. ๊ตฌํ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
- ๋ ธ๋ ๋ฐฉ๋ฌธ
- ์ผ์ชฝ ์๋ธํธ๋ฆฌ ๋ฐฉ๋ฌธ
- ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ ๋ฐฉ๋ฌธ
์ ์ ์ํ ๊ฒฐ๊ณผ: F, B, A, D, C, E, G, I, H
def preorder_traverse(self):
if self.head is not None:
self.__preorder(self.head)
def __preorder(self, cur):
self.preorder_list.append(cur.val)
print(cur.val)
if cur.left is not None:
self.__preorder(cur.left)
if cur.right is not None:
self.__preorder(cur.right)
๐ต ์ค์ ์ํ (inorder traversal)
๐จ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ ๋, O(n)์ ์๊ฐ ๋ณต์ก๋๋ก ์ ๋ ฌ ๊ฐ๋ฅ
์ผ์ชฝ ์๋ธํธ๋ฆฌ > ๋ ธ๋ > ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ ์์ผ๋ก ์ํํ๋ค. ๊ตฌํ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
- ์ผ์ชฝ ์๋ธํธ๋ฆฌ ๋ฐฉ๋ฌธ
- ๋ ธ๋ ๋ฐฉ๋ฌธ
- ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ ๋ฐฉ๋ฌธ
์ค์ ์ํ ๊ฒฐ๊ณผ: A, B, C, D, E, F, G, H, I
def inorder_traverse(self):
if self.head is not None:
self.__inorder(self.head)
def __inorder(self, cur):
if cur.left is not None:
self.__inorder(cur.left)
self.inorder_list.append(cur.val)
print(cur.val)
if cur.right is not None:
self.__inorder(cur.right)
๐ต ํ์ ์ํ(postorder traversal)
์ผ์ชฝ ์๋ธํธ๋ฆฌ > ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ > ๋ ธ๋ ์์ผ๋ก ์ํํ๋ค. ๊ตฌํ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
- ์ผ์ชฝ ์๋ธํธ๋ฆฌ ๋ฐฉ๋ฌธ
- ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ ๋ฐฉ๋ฌธ
- ๋ ธ๋ ๋ฐฉ๋ฌธ
ํ์ ์ํ ๊ฒฐ๊ณผ: A, C, E, D, B, H, I, G, F
def postorder_traverse(self):
if self.head is not None:
self.__postorder(self.head)
def __postorder(self, cur):
if cur.left is not None:
self.__postorder(cur.left)
if cur.right is not None:
self.__postorder(cur.right)
self.postorder_list.append(cur.val)
print(cur.val)
๐ต ์ ์ฒด ์ฝ๋
# ๋
ธ๋ ๋ง๋ค๊ธฐ
# A(val)
# / \
# B(left) C(right)
class Node:
def __init__(self, item):
self.val = item
self.left = None
self.right = None
# ์ด์งํธ๋ฆฌ ๋ง๋ค๊ธฐ
class BinaryTree:
# ์ด๊ธฐ๊ฐ head๋ None
def __init__(self):
self.head = Node(None)
self.preorder_list=[]
self.inorder_list=[]
self.postorder_list=[]
# ๊ฐ ์ถ๊ฐํ๊ธฐ head๊ฐ ์์ ๊ฒฝ์ฐ
def add(self, item):
if self.head.val is None:
self.head.val = item
# head๊ฐ ์์ผ๋ฉด ์ผ์ชฝ๋ฐฐ์น or ์ค๋ฅธ์ชฝ๋ฐฐ์น
else:
self.__add_node(self.head, item)
# head๊ฐ ์๋ ๊ฒฝ์ฐ
def __add_node(self, cur, item):
print('๋ถ๋ชจ:', cur.val, '์์:', item)
# head ๊ฐ์ด ํฌ๋ฉด ์ผ์ชฝ์ผ๋ก
if cur.val >= item:
if cur.left is not None:
self.__add_node(cur.left, item)
else:
cur.left = Node(item)
# head ๊ฐ์ด ์์ผ๋ฉด ์ค๋ฅธ์ชฝ์ผ๋ก
else:
if cur.right is not None:
self.__add_node(cur.right, item)
else:
cur.right = Node(item)
# ์ฐพ๊ธฐ!!
def search(self, item):
if self.head.val is None:
return False
else:
return self.__search_node(self.head, item)
def __search_node(self, cur, item):
print(cur.val, item)
if cur.val == item:
return True
else:
if cur.val >= item:
if cur.left is not None:
return self.__search_node(cur.left, item)
else:
return False
else:
if cur.right is not None:
return self.__search_node(cur.right, item)
else:
return False
# ์ ์์ํ
def preorder_traverse(self):
if self.head is not None:
self.__preorder(self.head)
def __preorder(self, cur):
self.preorder_list.append(cur.val)
print(cur.val)
if cur.left is not None:
self.__preorder(cur.left)
if cur.right is not None:
self.__preorder(cur.right)
# ์ค์์ํ
def inorder_traverse(self):
if self.head is not None:
self.__inorder(self.head)
def __inorder(self, cur):
if cur.left is not None:
self.__inorder(cur.left)
self.inorder_list.append(cur.val)
print(cur.val)
if cur.right is not None:
self.__inorder(cur.right)
# ํ์์ํ
def postorder_traverse(self):
if self.head is not None:
self.__postorder(self.head)
def __postorder(self, cur):
if cur.left is not None:
self.__postorder(cur.left)
if cur.right is not None:
self.__postorder(cur.right)
self.postorder_list.append(cur.val)
print(cur.val)
lst = [50, 30, 24, 5, 28, 45, 98, 52, 60]
bt = BinaryTree()
for num in lst:
bt.add(num)
print(bt.search(60))
# ์ ์ฐ
bt.preorder_traverse()
print(bt.preorder_list)
# ์ค์
bt.inorder_traverse()
print(bt.inorder_list)
# ํ์
bt.postorder_traverse()
print(bt.postorder_list)
# ์ถ์ฒ: https://www.youtube.com/watch?v=0CqDdXOr6Kk
'CS > Data Structure' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ(Stack) (0) | 2020.11.04 |
---|---|
[python] ์ด์ง ํ์ ํธ๋ฆฌ (binary Search tree) - 4. ์ญ์ (0) | 2020.09.05 |
[python] ์ด์ง ํ์ ํธ๋ฆฌ (binary Search tree) - 2. ํ์ (0) | 2020.09.03 |
[python] ์ด์ง ํ์ ํธ๋ฆฌ (binary Search tree) - 1. ํด๋์ค ์ ์, ์ด๊ธฐํ, ๋ ธ๋ ์ถ๊ฐ (0) | 2020.09.02 |
[python] ์ด์ง ํธ๋ฆฌ (binary tree) - ๊ฐ๋ ์ ๋ฆฌ (0) | 2020.09.01 |