

theory
๐ณ ๊ฐ๊ณ๋์ ๊ฐ์ ๊ณ์ธต์ ์ธ ๊ตฌ์กฐ๋ฅผ ํํํ ๋ ์ฌ์ฉํ ์ ์๋ ์๋ฃ๊ตฌ์กฐ
: Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each element in a binary tree can have only 2 children, we typically name them the left and right child.
๐ณ ํธ๋ฆฌ ๊ด๋ จ ์ฉ์ด
โ root node: ๋ถ๋ชจ๊ฐ ์๋ ์ต์์ ๋ ธ๋
โก leaf node: ์์์ด ์๋ ๋ ธ๋
โข size: ํธ๋ฆฌ์ ํฌํจ๋ ๋ชจ๋ ๋ ธ๋์ ๊ฐ์
โฃ depth: ํน์ ๋ ธ๋์์ ๋ฃจํธ ๋ ธ๋๋ถํฐ์ ๊ฑฐ๋ฆฌ(์ฆ ์ 1๋ฒ ๋ ธ๋์ ๊น์ด๋ 0, 2๋ฒ ๋ ธ๋์ ๊น์ด๋ 1)
โค height: ๊น์ด ์ค ์ต๋๊ฐ(์ 8๋ฒ ๋ ธ๋์ ๊น์ด๊ฐ height)
โฅ degree: ๊ฐ ๋ ธ๋์ (์์ ๋ฐฉํฅ) ๊ฐ์ ๊ฐ์(์ฆ ์์์ ๊ฐ์) (์ 1๋ฒ ๋ ธ๋์ ์ฐจ์๋ 2)
๐ณ ๊ธฐ๋ณธ์ ์ผ๋ก ํธ๋ฆฌ์ ํฌ๊ธฐ๊ฐ N์ผ ๋, ์ ์ฒด ๊ฐ์ ์ ๊ฐ์๋ N-1๊ฐ
binary search tree
๐ณ binary tree ์ค์์ ์ด์ง ํ์์ด ๋์ํ ์ ์๋๋ก ๊ณ ์๋ ํจ์จ์ ์ธ ํ์์ด ๊ฐ๋ฅํ ์๋ฃ๊ตฌ์กฐ์ ์ผ์ข
๐ณ ์ผ์ชฝ ์์ ๋ ธ๋ < ๋ถ๋ชจ ๋ ธ๋ < ์ค๋ฅธ์ชฝ ์์ ๋ ธ๋
๐ณ The left and right subtree each must also be a binary search tree. There must be no duplicate nodes.
basis of comparison | binary tree | binary search tree |
definition | a nonlinear data structure where each node can have at most two child nodes. | a node based binary tree that further has right and left subtree that too are binary search tree. |
types | Full binary tree Complete binary tree Extended Binary tree and more |
AVL tree Splay tree T-trees and more |
structure | In BINARY TREE there is no ordering in terms of how the nodes are arranged | In BINARY SEARCH TREE the left subtree has elements less than the nodes element and the right subtree has elements greater than the nodes element. |
data representation | Data Representation is carried out in a hierarchical format. | Data Representation is carried out in the ordered format. |
duplicate values | Binary trees allow duplicate values. | Binary Search Tree does not allow duplicate values. |
speed | The speed of deletion, insertion, and searching operations in Binary Tree is slower as compared to Binary Search Tree because it is unordered. | Because the Binary Search Tree has ordered properties, it conducts element deletion, insertion, and searching faster. |
complexity | Time complexity is usually O(n). | Time complexity is usually O(logn). |
application | It is used for retrieval of fast and quick information and data lookup. | It works well at element deletion, insertion, and searching. |
usage | It serves as the foundation for implementing Full Binary Tree, BSTs, Perfect Binary Tree, and others. | It is utilized in the implementation of Balanced Binary Search Trees such as AVL Trees, Red Black Trees, and so on. |
๐ณ binary searching example

๐ณ binary search tree๊ฐ ์ด๋ฏธ ๊ตฌ์ฑ๋์ด ์๋ค๊ณ ๊ฐ์ ํ๊ณ ๋ฐ์ดํฐ ์กฐํํ์. ์ฐพ๊ณ ์ ํ๋ ์์๊ฐ 37์ด๋ผ ๊ฐ์ ํ๋ฉด, root node๋ถํฐ ๋ฐฉ๋ฌธํด์ ํ์ ์งํ. ํ์ฌ node ๊ฐ์ 30์ด๋ฏ๋ก ์ฐพ๋ ์์๊ฐ ๊ฐ์ด ๋ ํผ. ๋ฐ๋ผ์ ํ์ฌ root node์์ ์ค๋ฅธ์ชฝ์ผ๋ก ๋ฐฉ๋ฌธ. ๊ทธ ๋ค์ ๋จ๊ณ์์๋ ๊ฐ์ด 48์ธ node ๋ฐฉ๋ฌธ(์ฆ root node์ ์ผ์ชฝ ๋ฒ์๋ ๋ฐฉ๋ฌธํ ํ์ ์์ - ํ์ ๋ฒ์ ์ ๋ฐ์ผ๋ก ์ค์ด๋ฆ). 37์ 48๋ณด๋ค ์์ผ๋ฏ๋ก ์ผ์ชฝ ๋ฐฉ๋ฌธ. 37 ํ์ธ! ์์๋ฅผ ์ฐพ์์ผ๋ฏ๋ก ํ์ ์ข ๋ฃ
๐ณ ์ฆ ์ด์ง ํ์์ด ๊ฐ๋ฅํ๊ฒ๋ ๊ณ ์๋ ํธ๋ฆฌ ์๋ฃ๊ตฌ์กฐ. ์ด์์ ์ธ ๊ฒฝ์ฐ ๋ฐ์ดํฐ ํ์ O(logn). ํ์ง๋ง ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ tree๊ฐ ๊ท ํ์ด ์กํ์์ ๊ฒฝ์ฐ๋ง ๊ฐ๋ฅํ ์๊ฐ ๋ณต์ก๋. ๋ฐ๋ผ์ ์ค์ ๋ก๋ ํธ๋ฆฌ ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํ ๋ค์ํ ์๋ฃ๊ตฌ์กฐ ์กด์ฌ
traversal(ํธ๋ฆฌ์ ์ํ)
๐ณ ํธ๋ฆฌ ์๋ฃ๊ตฌ์กฐ์ ํฌํจ๋ ๋ ธ๋๋ฅผ ํน์ ํ ๋ฐฉ๋ฒ์ผ๋ก ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธํ๋ ๋ฐฉ๋ฒ. ํธ๋ฆฌ์ ์ ๋ณด๋ฅผ ์๊ฐ์ ์ผ๋ก ํ์ธ ๊ฐ๋ฅ
โ ์ ์ ์ํ(pre-order traverse): ๋ฃจํธ๋ฅผ ๋จผ์ ๋ฐฉ๋ฌธ
โก ์ค์ ์ํ(in-order traverse): ์ผ์ชฝ ์์์ ๋ฐฉ๋ฌธํ ๋ค์ ๋ฃจํธ๋ฅผ ๋ฐฉ๋ฌธ
โข ํ์ ์ํ(post-order traverse): ์ค๋ฅธ์ชฝ ์์์ ๋ฐฉ๋ฌธํ ๋ค์ ๋ฃจํธ๋ฅผ ๋ฐฉ๋ฌธ
ex)

โ ์ ์ ์ํ
: ๋ฃจํธ๋ฅผ ๋จผ์ ์ถ๋ ฅ(A)ํ ์ผ์ชฝ ๋ค์ด๊ฐ์ ์ถ๋ ฅ(B) ๊ทธ ๋ค์ ์ผ์ชฝ(D)์ถ๋ ฅ. ์ด์ ์์ ๋ ธ๋ ์์ผ๋ฉด ๋ค์ ์๋ก ์ฌ๋ผ๊ฐ์ ์ค๋ฅธ์ชฝ(E) ์ถ๋ ฅ. ๋ค์ ์๋ก ์ฌ๋ผ๊ฐ์ ์ค๋ฅธ์ชฝ(C) ์ถ๋ ฅ. ๊ทธ ๋ค์ ์ผ์ชฝ(F) ์ถ๋ ฅ. ์์ ๋ ธ๋ ์์ผ๋ฏ๋ก ๋ค์ ์๋ก ์ฌ๋ผ๊ฐ์ ์ค๋ฅธ์ชฝ(G) ์ถ๋ ฅ
A โ B โ D โ E โ C โ F โ G
โก ์ค์ ์ํ(์๋๋ก ์ญ ๋ด๋ ค๊ฐ์ ์ผ์ชฝ ์์ ๋ ธ๋๋ถํฐ ์ถ๋ ฅ)
: ๋ฃจํธ์์ ์์ํด ๋งจ ์ผ์ชฝ ์์ ๋ ธ๋(D)๋ก ์ด๋. ๋ค์ ์๋ก ์ฌ๋ผ๊ฐ์ ๋ ธ๋(B) ์ถ๋ ฅ. ์ค๋ฅธ์ชฝ(E) ์ถ๋ ฅ. ๋ค์ ์๋ก ์ฌ๋ผ๊ฐ์ A ์ถ๋ ฅ. ์ค๋ฅธ์ชฝ ์์ ๋ ธ๋ C ์ถ๋ ฅ. ์ผ์ชฝ๋ถํฐ F ์ถ๋ ฅ. ๋ง์ง๋ง G ์ถ๋ ฅ
D โ B โ E โ A โ F โ C โ G
โข ํ์ ์ํ
: ๋ฃจํธ์์ ์์ํด ๋งจ ์ผ์ชฝ ์์ ๋ ธ๋(D)๋ก ์ด๋. ๊ทธ ๋ค์ ์๋ก ์ฌ๋ผ๊ฐ์ ๋ค๋ฅธ ์ค๋ฅธ์ชฝ ์์ ๋ ธ๋(E) ์ถ๋ ฅ. ์ด์ ์์ ๋ ธ๋ B ์ถ๋ ฅํ๋ ๋ฐฉ์
D โ E โ B โ F โ G โ C โ A
โ ์ฆ ์ ์ ์ํ๋ ๋ฃจํธ ๋จผ์ / ์ค์ ์ํ๋ ์ผ์ชฝ ๋จผ์ โ ๋ฃจํธ โ ์ค๋ฅธ์ชฝ / ํ์ ์ํ๋ ์ผ์ชฝ โ ์ค๋ฅธ์ชฝ โ ๋ฃจํธโ
traversal ๊ตฌํ ์์
โ pre-order๋ upper - left -right / in-order๋ left - upper - right / post-order๋ left - right - upper โ
class Node:
def __init__(self, data, left_node, right_node):
self.data = data
self.left_node = left_node
self.right_node = right_node
#preorder traversal
def pre_order(node):#upper -> left -> right
print(node.data, end=' ') #upper print: when visited, just print right away
if node.left_node != None: #left first(recursive)
pre_order(tree[node.left_node])
if node.right_node != None:
pre_order(tree[node.right_node])
#inorder traversal
def in_order(node):#left -> upper -> right
if node.left_node != None: #if left_node, search
in_order(tree[node.left_node])
print(node.data, end= ' ') #if left_node doesn't exist, print upper-node
if node.right_node != None:
in_order(tree[node.right_node])
#postorder traversal
def post_order(node): #left -> right -> upper
if node.left_node != None:
post_order(tree[node.left_node])
if node.right_node != None:
post_order(tree[node.right_node])
print(node.data, end= ' ')
n=int(input())
tree={}
for i in range(n):
data, left_node, right_node = input().split()
if left_node == 'None':
left_node = None
if right_node == 'None':
right_node = None
tree[data] = Node(data, left_node, right_node)
pre_order(tree['A']) #starts from A node
print()
in_order(tree['A'])
print()
post_order(tree['A'])
#input
'''
7
A B C
B D E
C F G
D None None
E None None
F None None
G None None
'''
#output
'''
A B D E C F G
D B E A F C G
D E B F G C A
'''
'Computer Science > Data Structures' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐Binary Heap (0) | 2024.07.17 |
---|---|
๐ฅชArray & Linked List Intro (2) | 2024.01.08 |
๐ Map (0) | 2023.08.03 |
Types of Binary Tree๐ฒ (0) | 2023.01.10 |
๐ฏPriority Queue(feat. Binary Heap) (0) | 2023.01.09 |
๋๊ธ