ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자료구조 트리
    자료구조와 알고리즘 2020. 10. 26. 16:08

    1. 트리 (Tree) 구조

    • 트리: Node와 Branch를 이용해서, 사이클을 이루지 않도록 구성한 데이터 구조
    • 실제로 어디에 많이 사용되나?
      • 트리 중 이진 트리 (Binary Tree) 형태의 구조로, 탐색(검색) 알고리즘 구현을 위해 많이 사용됨

    2. 알아둘 용어

    • Node: 트리에서 데이터를 저장하는 기본 요소 (데이터와 다른 연결된 노드에 대한 Branch 정보 포함)
    • Root Node: 트리 맨 위에 있는 노드
    • Level: 최상위 노드를 Level 0으로 하였을 때, 하위 Branch로 연결된 노드의 깊이를 나타냄
    • Parent Node: 어떤 노드의 다음 레벨에 연결된 노드
    • Child Node: 어떤 노드의 상위 레벨에 연결된 노드
    • Leaf Node (Terminal Node): Child Node가 하나도 없는 노드
    • Sibling (Brother Node): 동일한 Parent Node를 가진 노드
    • Depth: 트리에서 Node가 가질 수 있는 최대 Level

    3. 이진 트리와 이진 탐색 트리 (Binary Search Tree) 데이터 검색에서 많이 쓰임

    • 이진 트리: 노드의 최대 Branch가 2인 트리
    • 이진 탐색 트리 (Binary Search Tree, BST): 이진 트리에 다음과 같은 추가적인 조건이 있는 트리
      • 왼쪽 노드는 해당 노드보다 작은 값, 오른쪽 노드는 해당 노드보다 큰 값을 가지고 있음!

    (출처: https://www.mathwarehouse.com/programming/gifs/binary-search-tree.php#binary-search-tree-insertion-node)

     

    4. 자료 구조 이진 탐색 트리의 장점과 주요 용도

    • 주요 용도: 데이터 검색(탐색)
    • 장점: 탐색 속도를 개선할 수 있음

    단점은 이진 탐색 트리 알고리즘 이해 후에 살펴보기로 함

    이진트리와 정렬된 배열간의 탐색 비교

    그림에서 보는 것과 같이 데이터를 검색하는데 배열보다 빠르다.

     

    5. 파이썬 객체지향 프로그래밍으로 링크드 리스트 구현하기

    5.1. 노드 클래스 만들기

    class Node:
        def __init__(self, value):
            self.value = value
            self.left = None
            self.right = None

    5.2. 이진 탐색 트리에 데이터 넣기

    • 이진 탐색 트리 조건에 부합하게 데이터를 넣어야 함
    class NodeMgmt: #노드를 관리할 수 있는 클래스
        def __init__(self, head):
            self.head = head
        
        def insert(self, value):
            self.current_node = self.head
            while True:
                if value < self.current_node.value:
                    if self.current_node.left != None:
                    #비교할 대상을 바꾸는 것, 다시 while 반복을 돈다.
                        self.current_node = self.current_node.left 
                    else:
                        self.current_node.left = Node(value)
                        break
                else:
                    if self.current_node.right != None:
                        self.current_node = self.current_node.right
                    else:
                        self.current_node.right = Node(value)
                        break
    head = Node(1)
    BST = NodeMgmt(head)
    BST.insert(2)

    5.3. 이진 탐색 트리 탐색

    class NodeMgmt:
        def __init__(self, head):
            self.head = head
        
        def insert(self, value):
            self.current_node = self.head
            while True:
                if value < self.current_node.value:
                    if self.current_node.left != None:
                        self.current_node = self.current_node.left
                    else:
                        self.current_node.left = Node(value)
                        break
                else:
                    if self.current_node.right != None:
                        self.current_node = self.current_node.right
                    else:
                        self.current_node.right = Node(value)
                        break
        
        def search(self, value):
            self.current_node = self.head
            while self.current_node:
                if self.current_node.value == value:
                    return True
                elif value < self.current_node.value:
                    self.current_node = self.current_node.left
                else:
                    self.current_node = self.current_node.right
            return False
    head = Node(1)
    BST = NodeMgmt(head)
    BST.insert(2)
    BST.insert(3)
    BST.insert(0)
    BST.insert(4)
    BST.insert(8)
    BST.search(-1)

    false

    '자료구조와 알고리즘' 카테고리의 다른 글

    자료구조 트리3  (0) 2020.10.27
    자료구조 트리2  (0) 2020.10.26
    자료구조 해쉬 테이블3  (0) 2020.10.25
    자료구조 해쉬 테이블2  (0) 2020.10.25
    자료구조 해쉬 테이블  (0) 2020.10.23
전설의 개발자