-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Search_3.cpp
More file actions
294 lines (259 loc) · 6.95 KB
/
Binary_Search_3.cpp
File metadata and controls
294 lines (259 loc) · 6.95 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef long long llong;
// 二分木の定義
struct Node {
int key;
Node *left, *right, *parent;
};
// 根・葉を定義
Node *root, *NIL;
// 二分木に新しい値を挿入する
void insert(int key)
{
Node *x, *y, *z;
// 初期化
z = new Node;
z->key = key;
z->left = NIL;
z->right = NIL;
y = NIL;
x = root;
// 根から適切な位置までたどる
while (x != NIL) {
y = x; // 親を設定
if (z->key < x->key) {
x = x->left;
}
else {
x = x->right;
}
}
z->parent = y;
// 要素を配置する場所を決定する
if (y == NIL) { // T が空の場合
root = z;
}
else if (z->key < y->key){
y->left = z; // z を y の左の子にする
}
else {
y->right = z; // z を y の右の子にする
}
}
// キーが存在するか確認する
bool find(Node* node, int key)
{
while (node != NIL) {
if (node->key == key) return true;
// 二分木の特性を利用して場合分け
// 探すキーと現在のキーの子の左右の大小を比較する
// 終了
if (node->key == key) {
return true;
} else if (node->key < key)
{
// 右の子ノードよりキーが大きければ右へ
node = node->right;
} else {
// 左の子ノードよりキーが小さければ左へ
node = node->left;
}
}
return false;
}
void DeleteNoChildren(Node* node, int key, bool isRight)
{
if (isRight)
{
node->parent->right = NIL;
}
else
{
node->parent->left = NIL;
}
node = NIL;
return;
}
void DeleteOneChild(Node* node, int key, bool isRight, bool ischildright)
{
// 自身が親に対して右、子を右にだけ持つ場合
if (isRight && ischildright) {
node->parent->right = node->right;
node->right->parent = node->parent;
} else if(!isRight && ischildright) {
node->parent->left = node->right;
node->right->parent = node->parent;
} else if(isRight && !ischildright) {
node->parent->right = node->left;
node->left->parent = node->parent;
} else {
node->parent->left = node->left;
node->left->parent = node->parent;
}
node = NIL;
return;
}
// 次節点を求める
Node* next_node(Node *node) {
Node *x;
// 右部分木を保つ場合は
// その中で最小のキーを持つノードが次節点
if (node->right != NIL) {
x = node->right;
while (x->left != NIL) {
x = x->left;
}
// 右部分木がない場合は
// 親を辿っていって最初にでてくる左部分木が次節点
} else {
x = node;
while (x->parent != NIL && x == x->parent->right) {
x = x->parent;
}
x = x->parent;
}
return x;
}
// 指定されたキーを探し削除する
void Binary_Tree_delete(Node* node, int key)
{
// 指定キーの場所を探す
while (node != NIL) {
// 二分木の特性を利用して場合分け
// 探すキーと現在のキーの子の左右の大小を比較する
// 終了
if (node->key == key) {
break;
} else if (node->key < key)
{
// 右の子ノードよりキーが大きければ右へ
node = node->right;
} else {
// 左の子ノードよりキーが小さければ左へ
node = node->left;
}
}
bool isright;
// 指定キーが親の左右どちらの子か判定する
if (node->parent != NIL) {
isright = (node->parent->right->key == node->key)? true : false;
}
// 指定キー配下に子がいない場合
Node* ParentNode = node->parent;
if (node->right == NIL && node->left == NIL)
{
DeleteNoChildren(node, key, isright);
return;
}
// 指定キー配下に一個だけ子がいる場合
Node* ChildNode_right = node->right;
bool isChildright;
if (node->right != NIL && node->left == NIL)
{
DeleteOneChild(node, key, isright, true);
return;
} else if(node->right == NIL && node->left != NIL) {
DeleteOneChild(node, key, isright, false);
return;
}
// 指定キー配下に二個子がいる場合
if (node->right != NIL && node->left != NIL) {
// 指定キーの次節点を得る
Node* tgtnode;
tgtnode = next_node(node);
node->key = tgtnode->key;
bool istgtright = false;
if (tgtnode->parent->right != NIL && tgtnode->parent->right->key == tgtnode->key) {
istgtright = true;
}
if (tgtnode->right == NIL && tgtnode->left == NIL)
{
DeleteNoChildren(tgtnode, tgtnode->key, istgtright);
return;
}
else if (tgtnode->right != NIL && tgtnode->left == NIL)
{
DeleteOneChild(tgtnode, tgtnode->key, istgtright, true);
return;
}
else if (tgtnode->right == NIL && tgtnode->left != NIL)
{
DeleteOneChild(tgtnode, tgtnode->key, istgtright, false);
return;
}
}
return;
}
// 根から先行順巡回を行う
// 親→左部分木→右部分木の順
void PreOrder(Node* node)
{
cout << " " << node->key;
// 左部分木をチェック
if (node->left != NIL) {
PreOrder(node->left);
}
// 右部分木をチェック
if (node->right != NIL) {
PreOrder(node->right);
}
}
// 根から中間順巡回を行う
// 左部分木→親→右部分木の順
void InOrder(Node* node)
{
// 左部分木をチェック
if (node->left != NIL) {
InOrder(node->left);
}
cout << " " << node->key;
// 右部分木をチェック
if (node->right != NIL)
{
InOrder(node->right);
}
}
void PrintVec()
{
// 先行順巡回、 中間順巡回
InOrder(root);
cout << "\n";
PreOrder(root);
cout << "\n";
}
int main() {
// 節点の個数
int n = 0;
// 次数
int iDeg = 0;
int iNodeIdx = 0;
int left, node;
string strCmd,strRes;
bool bRet = false;
cin >> n;
for (int i = 0; i < n; i++)
{
//節点番号を取得
cin >> strCmd;
if (strCmd == "insert") {
cin >> iNodeIdx;
insert(iNodeIdx);
}
else if (strCmd == "print") {
PrintVec();
}
else if(strCmd == "find") {
cin >> iNodeIdx;
bRet = find(root, iNodeIdx);
strRes = bRet? "yes" : "no";
cout << strRes << "\n";
}
else if (strCmd == "delete") {
cin >> iNodeIdx;
Binary_Tree_delete(root, iNodeIdx);
}
}
return 0;
}