본문 바로가기
스터디/자료구조

[ 자료구조 ] Binary Search Tree

by 알 수 없는 사용자 2020. 2. 23.

트리(Tree)는 임의의 노드에서 다른 노드로의 경로가 하나 밖에 없는 자료구조입니다.

단 하나의 루트 노드(Root Node)가 있고, 루트 노드에서 하위 노드(Sub Node)들이 연결된 비선형 계층 구조입니다.

 

이진 트리 구조(Binary Tree)는 트리 자료구조 중에서 모든 노드가 최대 2개씩 자식 노드를 가질 수 있는 구조를 말합니다. 

 

< 이진 트리 분류 >

  • 포화 이진 트리(Full Binary Tree) : 노드가 꽉 차 있는 트리입니다.
  • 완전 이진 트리(Complete Binary Tree) : 마지막 레벨 전까지는 노드가 꽉 차있고, 마지막 레벨의 왼쪽에서 오른쪽으로 노드가 채워져 있는 트리입니다. (마지막 레벨이 다 채워지지 않아도 됨.)
  • 편향 이진 트리(Skewed Binary Tree) :  왼쪽 혹은 오른쪽 서브트리만을 가지는 트리입니다.

이진 탐색 트리(Binary Search Tree) : 이진 트리 기반의 탐색을 위한 자료구조입니다.  모든 노드는 유일한 값이고

왼쪽 서브 트리의 값은 루트의 값보다 작고 오른쪽 서브 트리의 값은 루트보다 큰 값을 가지도록 구성합니다.

 

1. 구현 사항

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
 
using namespace std;
 
template<typename T>
class Node
{
public:
    T data;
    Node* left;
    Node* right;
 
    Node(T _data = 0, Node* _left = NULL, Node* _right  = NULL) : data(_data), left(_left), right(_right)
    {
 
    }
 
    ~Node()
    {
        left = NULL;
        right = NULL;
    }
};
 
template<typename T>
class Binary_Tree
{
public:
    int size;
    Node<T>* root;
 
    Binary_Tree(T data = 0)
    {
        root = new Node<T>(data, NULLNULL);
    }
 
 
    ~Binary_Tree()
    {
        delete root;
    }
 
    Node<T>* Getroot()
    {
        return root;
    }
 
    void visit(Node<T>* current)
    {
        cout << current-> data << endl;
    }
 
    // 전위 순회 Current - Left - Right
    void preorder(Node<T>* current) 
    {
        if (current != NULL)
        {
            visit(current);
            preorder(current->left);
            preorder(current->right);
        }
    }
 
    // 중위 순회 Left - Current - Right
    void inorder(Node<T>* current)
    {
        if (current != NULL)
        {
            inorder(current->left);
            visit(current);
            inorder(current->right);
        }
    }
 
    // 후위 순회 Left - Right - Current
    void postorder(Node<T>* current)
    {
        if (current != NULL)
        {
            postorder(current->left);
            postorder(current->right);
            visit(current);
        }
    }
 
    // 노드 추가
    void insert(T data)
    {
        Node<T>* temp = root;
 
        while (true)
        {
            if (data < temp-> data) // 왼쪽
            {
                if (!temp-> data) // 데이터 없다면 추가.
                {
                    temp-> left = data;
                    return;
                }
                else // 있다면 다시
                {
                    temp = root-> left;
                }
            }
            else // 오른쪽
            {
                if (!temp-> data) // 데이터 없다면 추가.
                {
                    temp-> right = data;
                    return;
                }
                else // 있다면 다시
                {
                    temp = root-> right;
                }
            }
        }
 
    }
};
 

2. STL 활용 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <set>
 
using namespace std;
 
int main()
{
    set<string> s;
 
    // insert(element)
    s.insert("abc");
    s.insert("def");
    s.insert("ghi");
    s.insert("jkl");
 
    // erase(element)
    s.erase("jkl");
 
    // empty(), size()
    if (!s.empty()) cout << "s size : " << s.size() << '\n';
 
    // find(element)
    cout << *s.find("abc"<< '\n';
    cout << *s.find("def"<< '\n';
 
    // count(element)
    cout << "abc count : " << s.count("abc"<< '\n';
 
    return 0;
}
 
 

 

'스터디 > 자료구조' 카테고리의 다른 글

[ 자료구조 ] Bubble Sort  (0) 2020.02.23
[ 자료구조 ] Selection Sort  (0) 2020.02.23
[ 자료구조 ] Deque  (0) 2020.02.22
[ 자료구조 ] Queue  (0) 2020.02.21
[ 자료구조 ] Stack  (0) 2020.02.21