[Python] Binary Search Tree
Updated:
Python Binary Search Tree
[LeetCode] Sum of Nodes with Even-Valued Grandparent
λ¬Έμ λ₯Ό νλ©° κΆκΈμ¦μ΄ μ겨 νμ΄μ¬μΌλ‘ μ΄μ§ νμ νΈλ¦¬λ₯Ό λ§λ€μ΄ λ³΄λ €κ³ νλ€.
C λ‘ κ΅¬ννλ €λ©΄ ν¬μΈν°λ₯Ό μ¨μΌν΄μ κ΅μ₯ν 볡μ‘νλ° νμ΄μ¬μ μ΄λ¨κΉ ?
κ·Έ μ μ μ΄μ§νΈλ¦¬μ μ΄μ§νμνΈλ¦¬λ₯Ό λΉκ΅ ν΄ λ³΄μ. μ΄μ§ νΈλ¦¬λ λͺ¨λ λ Έλμ μμμ΄ 2κ° μ΄νμ΄λ©΄ λλ€.
νμ§λ§ μ΄μ§ νμ νΈλ¦¬λ μ¬κΈ°μ μ‘°κ±΄μ΄ λ μΆκ° λλλ° νμ¬ λ Έλλ₯Ό κΈ°μ€μΌλ‘ μΌμͺ½ μμμ κ°μ΄ μμμΌ νκ³ μ€λ₯Έμͺ½ μμμ κ°μ΄ μ»€μΌ νλ€.
λ λͺ¨λ λ Έλλ μ μΌ ν κ°μ κ°μ§λ€. μ΄λ° 쑰건μ κ°μ§λ μ΄μ λ λ΄κ° μνλ κ°μ μ°Ύμκ°λ λΉ λ₯΄κ² μ°Ύμ κ° μ μκΈ° λλ¬Έμ΄λ€.
data = [9, 7, 15, 10, 3, 8, 2, 17, 13, 25, 4] λ₯Ό μ΄μ§ νμ νΈλ¦¬λ‘ λ§λ€μ΄ λ³΄λ €κ³ νλ€. λ¨Όμ λ Έλλ₯Ό λ§λ€μ΄ μ£Όμ΄μΌ νλ€.
λ Έλλ κ°μ κ°μ§κ³ μλ ν λ©μ΄λ¦¬λ‘ μκ°νλ©΄ λλ€.
class Node:
def __init__(self, data):
self.data = data
self.left = self.right = None
C λ‘ ν λ κ° λ Έλλ₯Ό ν¬μΈν°λ‘ μ°κ²° ν΄ μ£Όμ΄μΌ νλλ° κ°μ²΄λ‘ λ§λλ λμ λ§ ν΄μ£Όλ©΄ λμ΄μ μμ£Ό νΈνλ€..
λ Έλλ₯Ό λ§λ€μλ€λ©΄ μ°λ¦¬κ° λ§λ€κ³ μ νλ μ΄μ§ νμ νΈλ¦¬ κ°μ²΄λ₯Ό λ§λ λ€.
class Binary_Search_Tree:
def __init__(self):
self.root = None
my_tree = Binary_Search_Tree()
μ΄ μνλ λ Έλκ° μμ μλ λΉ νΈλ¦¬λ₯Ό ν κ° λ§λ μνμ΄λ€.
Insert
insert ν¨μλ₯Ό λ§λ€μ΄ κ°μ κ°μ§ λ Έλλ₯Ό νΈλ¦¬λ‘ λ§λ€μ΄ μ€λ€.
class Binary_Search_Tree:
def insert(self, data):
self.root = self.insert_node(self.root, data)
return self.root
def insert_node(self, node, data):
if node is None:
node = Node(data) # λ£¨νΈ λ
Έλ μμ±
else:
if data < node.data:
node.left = self.insert_node(node.left, data)
else:
node.right = self.insert_node(node.right, data)
return node
Find
μ΄μ§ νμ νΈλ¦¬μ λ΄κ° μνλ κ°μ΄ μλμ§ κΆκΈ ν κ²½μ°κ° μλ€. μ΄λ μ΄μ§ νμ νΈλ¦¬μ νΉμ§μ ν΅ν΄ λΉ λ₯΄κ² κ°μ μ°Ύμ μ μλ€.
class Binary_Search_Tree:
def find(self, key):
return self._find(self.root, key)
def _find(self, root, key):
if root is None or key == root.data:
return root is not None
else:
if key < root.data:
return self._find(root.left, key)
else:
return self._find(root.right, key)
μ¬κ·ν¨μλ₯Ό ν΅ν΄ λλ³΄λ€ μμΌλ©΄ μΌμͺ½, λλ³΄λ€ ν¬λ©΄ μ€λ₯Έμͺ½μΌλ‘ κ° νμμ νλ€.
Pre-Order (μ μ μν)
λ£¨νΈ βΒ μΌμͺ½ μλΈνΈλ¦¬ βΒ μ€λ₯Έμͺ½ μλΈνΈλ¦¬ μμλ‘ μν νλ λ°©μμ΄λ€.
class Binary_Search_Tree:
def pre_order(self):
output = []
def _pre_order(node):
if node is None:
return
else:
output.append(node.data)
_pre_order(node.left)
_pre_order(node.right)
_pre_order(self.root)
print('Pre_Order :', output)
data = [9, 7, 15, 10, 3, 8, 2, 17, 13, 25, 4] μ΄ λ°°μ΄λ‘ μ΄μ§ νμ νΈλ¦¬λ₯Ό λ§λ λ€ μ μ μνλ₯Ό νλ©΄
[9, 7, 3, 2, 4, 8, 15, 10, 13, 17 ,25] μμλ‘ μν νλ€.
In-Order (μ€μ μν)
μΌμͺ½ μλΈνΈλ¦¬ β λ£¨νΈ β μ€λ₯Έμͺ½ μλΈνΈλ¦¬ μμλ‘ μν νλ λ°©μμ΄λ€.
class Binary_Search_Tree:
def in_order(self):
output = []
def _in_order(node):
if node is None:
return
else:
_in_order(node.left)
output.append(node.data)
_in_order(node.right)
_in_order(self.root)
print('In_Order :', output)
data = [9, 7, 15, 10, 3, 8, 2, 17, 13, 25, 4] μ΄ λ°°μ΄λ‘ μ΄μ§ νμ νΈλ¦¬λ₯Ό λ§λ λ€ μ€μ μνλ₯Ό νλ©΄
[2, 3, 4, 7, 8, 9, 10, 13, 15, 17, 25] μμλ‘ μν νλ€. μ λ ¬ λ κ°μ μ»μ μ μλ€λ νΉμ§μ΄ μλ€.
Post-Order (νμ μν)
μΌμͺ½ μλΈνΈλ¦¬ β μ€λ₯Έμͺ½ μλΈνΈλ¦¬β λ£¨νΈ μμλ‘ μν νλ λ°©μμ΄λ€.
class Binary_Search_Tree:
def post_order(self):
output = []
def _post_order(node):
if node is None:
return
else:
_post_order(node.left)
_post_order(node.right)
output.append(node.data)
_post_order(self.root)
print('Post_Order :', output)
data = [9, 7, 15, 10, 3, 8, 2, 17, 13, 25, 4] μ΄ λ°°μ΄λ‘ μ΄μ§ νμ νΈλ¦¬λ₯Ό λ§λ λ€ νμ μνλ₯Ό νλ©΄
[2, 4, 3, 8, 7, 13, 10, 25, 17, 15, 9] μμλ‘ μν νλ€. μ λ ¬ λ κ°μ μ»μ μ μλ€λ νΉμ§μ΄ μλ€.
Level-Order (λ 벨 μν)
λ 벨 μμΌλ‘ λ Έλλ€μ μν νλ€.
class Binary_Search_Tree:
def level_order(self):
output = []
def _level_order(root):
queue = deque()
queue.append(root)
while queue:
node = queue.popleft()
if node is not None:
output.append(node.data)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
_level_order(self.root)
print('level_order :', output)
data = [9, 7, 15, 10, 3, 8, 2, 17, 13, 25, 4] μ΄ λ°°μ΄λ‘ μ΄μ§ νμ νΈλ¦¬λ₯Ό λ§λ λ€ λ 벨 μνλ₯Ό νλ©΄
[9, 7, 15, 3, 8, 10, 17, 2, 4, 13, 25] μμλ‘ μν νλ€. κ·Έλνμ λ¬λ¦¬ μ°κ²°μ΄ λμ΄μμ§ μκΈ° λλ¬Έμ λ°©λ¬Έ μ¬λΆλ₯Ό 체ν¬νμ§ μμλ λλ€.
Leave a comment