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
|
#include <iostream>
using namespace std;
class SBTNode {
public:
int data, size, value;
SBTNode *lchild, *rchild, *father;
SBTNode(int init_data, int init_size = 0, SBTNode *init_father = NULL);
~SBTNode();
void insert(int value);
SBTNode *search(int value);
SBTNode *predecessor();
SBTNode *successor();
void remove_node(SBTNode *delete_node);
bool remove(int value);
};
class BinaryTree {
private:
SBTNode *root;
public:
BinaryTree();
~BinaryTree();
void insert(int value);
bool find(int value);
bool remove(int value);
};
SBTNode ZERO(0);
SBTNode *ZPTR = &ZERO;
SBTNode::SBTNode(int init_data, int init_size, SBTNode *init_father) {
data = init_data;
size = init_size;
lchild = ZPTR;
rchild = ZPTR;
father = init_father;
}
SBTNode::~SBTNode() {
if (lchild != ZPTR) {
delete lchild;
}
if (rchild != ZPTR) {
delete rchild;
}
}
SBTNode *left_rotate(SBTNode * node) {
SBTNode *temp = node->rchild;
node->rchild = temp->lchild;
temp->lchild->father = node;
temp->lchild = node;
temp->father = node->father;
node->father = temp;
temp->size = node->size;
node->size = node->lchild->size + node->rchild->size + 1;
return temp;
}
SBTNode *right_rotate(SBTNode *node) {
SBTNode* temp = node->lchild;
node->lchild = temp->rchild;
temp->rchild->father = node;
temp->rchild = node;
temp->father = node->father;
node->father = temp;
temp->size = node->size;
node->size = node->lchild->size + node->rchild->size + 1;
return temp;
}
// node 表示要调整子树的根结点
// flag 用来标记是处理左子树更高的情况还是右子树更高的情况
SBTNode *maintain(SBTNode *node, bool flag) {
if (flag == false) { // 左子树更高
if (node->lchild->lchild->size>node->rchild->size) {
// LL 的情况
node = right_rotate(node);
}
else if (node->lchild->rchild->size>node->rchild->size) {
// LR 的情况
node->lchild = left_rotate(node->lchild);
node = right_rotate(node);
}
else {
return node;
}
}
else { // 右子树更高
if (node->rchild->rchild->size>node->lchild->size) {
// RR 的情况
node = left_rotate(node);
}
else if (node->rchild->lchild->size>node->lchild->size) {
// RL 的情况
node->rchild = right_rotate(node->rchild);
node = left_rotate(node);
}
else {
return node;
}
}
// 执行完旋转操作,要递归地对左右子树和当前子树进行调整
node->lchild = maintain(node->lchild, false);
node->rchild = maintain(node->rchild, true);
node = maintain(node, false);
node = maintain(node, true);
return node;
}
SBTNode *insert(SBTNode *node, int value) {
if (value == node->data) {
return node;
}
else {
node->size++;
if (value > node->data) {
if (node->rchild == ZPTR) {
node->rchild = new SBTNode(value, 1, node);
}
else {
node->rchild = insert(node->rchild, value);
}
}
else {
if (node->lchild == ZPTR) {
node->lchild = new SBTNode(value, 1, node);
}
else {
node->lchild = insert(node->lchild, value);
}
}
}
return maintain(node, value > node->data);
}
SBTNode *SBTNode::search(int value) {
if (data == value) {
return this;
}
else if (value > data) {
if (rchild == ZPTR) {
return ZPTR;
}
else {
return rchild->search(value);
}
}
else {
if (lchild == ZPTR) {
return ZPTR;
}
else {
return lchild->search(value);
}
}
}
SBTNode *SBTNode::predecessor() {
SBTNode * temp = lchild;
while (temp != ZPTR && temp->rchild != ZPTR) {
temp = temp->rchild;
}
return temp;
}
SBTNode *SBTNode::successor() {
SBTNode *temp = rchild;
while (temp != ZPTR && temp->lchild != ZPTR) {
temp = temp->lchild;
}
return temp;
}
void SBTNode::remove_node(SBTNode *delete_node) {
SBTNode *temp = ZPTR;
if (delete_node->lchild != ZPTR) {
temp = delete_node->lchild;
temp->father = delete_node->father;
delete_node->lchild = ZPTR;
}
if (delete_node->rchild != ZPTR) {
temp = delete_node->rchild;
temp->father = delete_node->father;
delete_node->rchild = ZPTR;
}
if (delete_node->father->lchild == delete_node) {
delete_node->father->lchild = temp;
}
else {
delete_node->father->rchild = temp;
}
temp = delete_node;
while (temp != NULL) {
temp->size--;
temp = temp->father;
}
delete delete_node;
}
bool SBTNode::remove(int value) {
SBTNode *delete_node, *current_node;
current_node = search(value);
if (current_node == ZPTR) {
return false;
}
size--;
if (current_node->lchild != ZPTR) {
delete_node = current_node->predecessor();
}
else if (current_node->rchild != ZPTR) {
delete_node = current_node->successor();
}
else {
delete_node = current_node;
}
current_node->data = delete_node->data;
remove_node(delete_node);
return true;
}
BinaryTree::BinaryTree() {
root = NULL;
}
BinaryTree::~BinaryTree() {
if (root != NULL) {
delete root;
}
}
void BinaryTree::insert(int value) {
if (root == NULL) {
root = new SBTNode(value, 1);
}
else {
root = ::insert(root, value);
}
}
bool BinaryTree::find(int value) {
if (root->search(value) == NULL) {
return false;
}
else {
return true;
}
}
bool BinaryTree::remove(int value) {
return root->remove(value);
}
int main() {
BinaryTree binarytree;
int arr[10] = { 8, 9, 10, 3, 2, 1, 6, 4, 7, 5 };
for (int i = 0; i < 10; i++) {
binarytree.insert(arr[i]);
}
int value;
cin >> value;
if (binarytree.find(value)) {
cout << "search success!" << endl;
}
else {
cout << "search failed!" << endl;
}
cin >> value;
if (binarytree.remove(value)) {
cout << "delete success!" << endl;
}
else {
cout << "delete failed!" << endl;
}
return 0;
}
|