Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
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
Archives
Today
Total
관리 메뉴

codingfarm

BST(Binary Search Tree) 본문

Algorithm & Data structure/이론

BST(Binary Search Tree)

scarecrow1992 2020. 9. 6. 16:59
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<iostream>
 
using namespace std;
 
class TreeNode {
public:
    int value;
    TreeNode *left, *right, *parent;
};
 
TreeNode treeNodePool[1500000];
int treeNodePoolIndex;
 
TreeNode* NewTreeNode() {
    int tmp = treeNodePoolIndex;
    treeNodePoolIndex++;
    treeNodePool[tmp].value = -1;
    treeNodePool[tmp].left = 0;
    treeNodePool[tmp].right = 0;
    treeNodePool[tmp].parent = 0;
 
    return &treeNodePool[tmp];
}
 
class Tree {
public:
    TreeNode * root;
 
    void Init() {
        root = 0;
    }
 
    void Insert(int value) {
        TreeNode *parent = 0*finder = root, **= 0;
 
        while (1) {
            if (finder == 0) {
                TreeNode *newNode = NewTreeNode();
                newNode->value = value;
                if (root == 0) {
                    root = newNode;
                }
                else {
                    *= newNode;
                    newNode->parent = parent;
                }
                return;
            }
 
            parent = finder;
            if (value >= finder->value) {
                p = &parent->right;
                finder = finder->right;
            }
            else {
                p = &parent->left;
                finder = finder->left;
            }
        }
    }
 
    TreeNode* Find(int value) {
        TreeNode *finder = root;
        while (1) {
            if (finder == 0)                
                return 0;
            
            if (finder->value == value)
                return finder;
 
            if (value > finder->value)
                finder = finder->right;
            else
                finder = finder->left;
        }
    }
 
    TreeNode* Erase(int value) {
        TreeNode *eraser = Find(value);
        
        if (eraser == 0)
            return 0;
        
        TreeNode *parent = eraser->parent, **= 0;
        if (eraser != root) {
            if (parent->right == eraser)
                p = &parent->right;
            else if (parent->left == eraser)
                p = &parent->left;
        }
 
        if (eraser->left == 0 && eraser->right == 0) {
            //자식이 하나도 없을 경우
            if (eraser == root)
                root = 0;
            else
                *= 0;
        }
        else if (eraser->left != 0 && eraser->right == 0) {
            //자식이 왼쪽에만 있을 경우
            if (eraser == root) {
                root = eraser->left;
                eraser->left->parent = 0;
            }
            else {
                *= eraser->left;
                eraser->left->parent = parent;
            }
        }
        else if (eraser->left == 0 && eraser->right != 0) {
            //자식이 오른쪽에만 있을 경우
            if (eraser == root) {
                root = eraser->right;
                eraser->right->parent = 0;
            }
            else {
                *= eraser->right;
                eraser->right->parent = parent;
            }
        }
        else if (eraser->left != 0 && eraser->right != 0) {
            //자식이 양쪽에 있을 경우
            TreeNode *finder = eraser->left;
            while (1) {
                if (finder->right == 0)
                    break;
                finder = finder->right;
            }
        
            Erase(finder->value);
 
            if (eraser == root) {
                root = finder;
                finder->parent = 0;
            }
            else {
                *= finder;
                finder->parent = parent;
            }
 
            finder->left = eraser->left;
            finder->right = eraser->right;
 
            if (eraser->left != 0)
                finder->left->parent = finder;
            
            if (eraser->right != 0)
                finder->right->parent = finder;
        }
    }
};
 
Tree tree;
 
void Init() {
    treeNodePoolIndex = 0;
    tree.Init();
}
 
 
int main(void) {
    Init();
 
    int N;
    cin >> N;
    int input;
    for (int i = 0; i < N; i++) {
        cin >> input;
        tree.Insert(input);
    }
 
    for (int i = 0; i < N; i++) {
        cin >> input;
        tree.Erase(input);
    }
 
    return 0;
}
 
/*
7
100
150
50
175
125
25
75
100
150
50
175
125
25
75
*/
cs

'Algorithm & Data structure > 이론' 카테고리의 다른 글

트라이 알고리즘 (trie)  (0) 2021.03.17
구간 길이 구하기  (0) 2021.01.06
Heap  (0) 2020.09.06
LCA(Lowest Common Ancestor)  (0) 2020.09.06
MST(Minimum Spanning Tree) & Kruskal  (0) 2020.09.06
Comments