![[자료구조] Tree](https://inblog.ai/_next/image?url=https%3A%2F%2Finblog.ai%2Fapi%2Fog%3Ftitle%3D%255B%25EC%259E%2590%25EB%25A3%258C%25EA%25B5%25AC%25EC%25A1%25B0%255D%2520Tree%26logoUrl%3Dhttps%253A%252F%252Finblog.ai%252Finblog_logo.png%26blogTitle%3D%25EB%25B0%25B1%25EC%2597%2594%25EB%2593%259C%25EB%25B8%2594%25EB%25A1%259C%25EA%25B7%25B8-dohyeong&w=1920&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fs3-us-west-2.amazonaws.com%252Fsecure.notion-static.com%252F739ca014-3b4a-432e-8807-c96bffc450c5%252FUntitled.png%3Ftable%3Dblock%26id%3D761796fd-16c4-401a-a8f7-2f0ddd52325b%26cache%3Dv2&w=3840&q=75)
트리 구조는 운영체제의 파일 시스템이 트리구조, HTML이나 XML 문서를 다룰 때 사용하는 DOM도 트리구조로 이루어져 있다. 또한 검색 엔진이나 데이터베이스도 트리 자료구조를 기반으로 한다
- root : 최상위 노드
- 부모노드 : 자식 노드를 가진 노드
- 자식노드 : 부모 노드의 하위노드
- 형제노드 : 부모가 같은 노드
- 리프노드 : 자식이 없는 노드
- 서브트리 : 특정 노드를 루트로 생각할 때 생기는 트리
트리 노드 표현 두가지
- N-링크 표현법
- 왼쪽 자식 오른쪽 형제 트리
왼쪽 자식 - 오른쪽 형제 트리
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fs3-us-west-2.amazonaws.com%252Fsecure.notion-static.com%252F8612f605-98d8-4504-aa16-66a2638d922b%252FUntitled.png%3Ftable%3Dblock%26id%3D9496b173-eb84-41ae-8ed6-5f0681d3cdf4%26cache%3Dv2&w=3840&q=75)
class LCRSNode: def __init__(self, new_data): self.left_child = None self.right_sibling = None self.data = new_data def LCRS_DestroyTree(root): if root.right_sibling is not None: LCRS_DestroyTree(root.right_sibling) if root.left_child is not None: LCRS_DestroyTree(root.left_child) root.left_child = None root.right_sibling = None def LCRS_AddChildNode(parent, child): if parent.left_child is None: parent.left_child = child else: temp_node = parent.left_child while temp_node.right_sibling is not None: temp_node = temp_node.right_sibling temp_node.right_sibling = child def LCRS_PrintTree(node, depth): for i in range(depth-1): print(" ", end='') # 공백 3칸 if depth > 0: # 자식 노드 여부 표시 print("+--", end='') # 노드 데이터 출력 print(node.data) if node.left_child is not None: LCRS_PrintTree(node.left_child, depth + 1) if node.right_sibling is not None: LCRS_PrintTree(node.right_sibling, depth)
이진트리
하나의 노드가 자식 노드를 2개까지 만 가질 수 있는 이진 트리 → 각 노드의 최대 차수가 2이다.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fs3-us-west-2.amazonaws.com%252Fsecure.notion-static.com%252Fa3dd26fb-9665-403f-b1f2-7e1d05a0d4d2%252FUntitled.png%3Ftable%3Dblock%26id%3D0660b147-422a-428f-9289-3cd40a28e491%26cache%3Dv2&w=3840&q=75)
위의 그림을 포화 이진트리 ( Full Binary Tree )
포화 이진 트리의 전단계를 완전 이진트리(Complete Binary Tree)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fs3-us-west-2.amazonaws.com%252Fsecure.notion-static.com%252Ff2ad34f0-1db7-4ea6-9902-187508ef3f1e%252FUntitled.png%3Ftable%3Dblock%26id%3Dce66cbe1-4821-4106-b265-cb40e57c2815%26cache%3Dv2&w=3840&q=75)
포화 이진 트리와 완전 이진 트리를 분류해서 설명하는 이유는 검색에서 트리의 노드가 가능한 한 완전한 모습으로 유지해야 높은 성능이 가능하다.
높이 균형 트리와 완전 높이 균형 트리
루트 노드를 기준으로 왼쪽 하위 트리와 오른쪽 하위 트리의 높이가 2이상 차이가 나지 않는 이진 트리를 말함
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fs3-us-west-2.amazonaws.com%252Fsecure.notion-static.com%252F24787fbf-b9cb-40ca-8ac2-bec0ca1c3c37%252FUntitled.png%3Ftable%3Dblock%26id%3D10c783c8-6c6d-4555-8fc9-0f8f5e3c8fa9%26cache%3Dv2&w=3840&q=75)
이진 트리의 순회
순회 Traversal는 트리 안에서 노드 사이를 이동하는 연산
전위 순회, 중위 순회, 후위 순회가 있다
- 전위 순회 Preorder Traversal
- 루트 노드부터 시작하여 아래로 내려오며넛
- 왼쪽 하위 트리를 방문하고, 방문이 끝나면
- 오른쪽 하위 트리를 방문한다
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fs3-us-west-2.amazonaws.com%252Fsecure.notion-static.com%252F315b8d5b-bdf3-4e64-8944-943bcc6d9eca%252FUntitled.png%3Ftable%3Dblock%26id%3Dc1ef5b1a-26c7-4b7e-b6f4-f6dac9a50d6d%26cache%3Dv2&w=3840&q=75)
- 중위 순회 Inorder Traversal
- 왼쪽 하위 트리부터 시작해서
- 루트를 거쳐
- 오른쪽 하위 트리를 방문하는 방법이다
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fs3-us-west-2.amazonaws.com%252Fsecure.notion-static.com%252Fe751b46f-0b31-4cc0-9bea-c38ebf03ce54%252FUntitled.png%3Ftable%3Dblock%26id%3D42519d79-1382-4f8d-b152-0df490fbb2c3%26cache%3Dv2&w=3840&q=75)
응용하는 대표 사례는 수식 트리
- 후위 순회 Postorder Traversal
- 왼쪽 하위 트리 방문
- 오른쪽 하위 트리 방문 후
- 루트 노드를 방문한다
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fs3-us-west-2.amazonaws.com%252Fsecure.notion-static.com%252Ff4d166e8-4e02-495f-a158-794159e248e4%252FUntitled.png%3Ftable%3Dblock%26id%3D4206bc4d-15bb-4a86-ab03-45fe8e774eab%26cache%3Dv2&w=3840&q=75)
class Node: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right class BinaryTree: def __init__(self, root): self.root = root def preorder(node): if node is not None: print(node.data,end=" ") if node.left: preorder(node.left) if node.right: preorder(node.right) def inorder(node): if node is not None: inorder(node.left) print(node.data,end=" ") inorder(node.right) def postorder(node): if node is not None: postorder(node.left) postorder(node.right) print(node.data,end=" ") A = Node('A') B = Node('B') C = Node('C') D = Node('D') E = Node('E') F = Node('F') G = Node('G') # 트리에 노드 추가 A.left = B B.left = C B.right = D A.right = E E.left = F E.right = G # 트리 출력 print("Preorder ...") preorder(A) print() print("Inorder ...") inorder(A) print() print("Postorder ...") postorder(A) print()
Share article