-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.cpp
More file actions
285 lines (265 loc) · 8.41 KB
/
Copy pathBinarySearchTree.cpp
File metadata and controls
285 lines (265 loc) · 8.41 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
// Suppose that each node containing a keyvalue (int) and link in a binary search tree also has the field LeftSize as described in the text. Please write a class BinaryTree to implement the following functions:
// (1) insertNode() to insert a node into a binary search tree.
// (2) deleteNode() to delete a node(delete by value)from the binary search tree.(If there are two child, replace the delete node with the smallest key in the right sub tree.)
// (3) searchByValue() to search a node by value, if it exists, show the LeftSize and Depth of the node.
// (4) searchByRank() to search a node by rank K(the Kth smallest).
// (5) showInorder() to print out the inorder traversal of the nodes’ values.
// (6) showPreorder() to print out the preorder traversal of the nodes’ values.
// (7) showPostorder() to print out the postorder traversal of the nodes’ values.
// and interact with the user by the corresponding command: function# keyvalue, where function# is valid from 1 to 7, and keyvalue is the value of nodes in the binary tree.
// Note : If you insert duplicate data, output : ”Duplicate data.”
// If there’s no such value to delete, output : “No such value to delete.”
// If there’s no such value to search, output : “No such value.”
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
class Node {
public:
Node(int x) {
value = x;
}
public:
int value;
Node* left = 0;
Node* right = 0;
int LeftSize = 1;
};
class Tree {
public:
bool isExist(int x) {
if (head == 0) {
return false;
}
Node* last = head;
while (true) {
if (last->value > x&&last->left!=0) {
last = last->left;
}
else if (last->value < x&&last->right != 0) {
last = last->right;
}
else if(last->value==x){
return true;
}else if (last->value < x&&last->right == 0) {
return false;
}
else if (last->value >x&&last->left == 0) {
return false;
}
}
}
void insert(int x) {
if (isExist(x)) {
cout << "Duplicate data." << endl;
return;
}
if (head == 0) {
head = new Node(x);
cout << "[INSERT SUCCESS]" << endl;
return;
}
Node* last = head;
while (true) {
if (last->value > x&&last->left != 0) {
last->LeftSize++;
last = last->left;
}
else if (last->value < x&&last->right != 0) {
last = last->right;
}
else if (last->value > x&&last->left == 0) {
last->LeftSize++;
last->left = new Node(x);
cout << "[INSERT SUCCESS]" << endl;
break;
}
else if (last->value < x&&last->right == 0) {
last->right = new Node(x);
cout << "[INSERT SUCCESS]" << endl;
break;
}
}
}
void Delete(int x) {
if (!isExist(x)) {
cout << "No such value to delete." << endl;
return;
}
Node* last = head;
Node* parent = 0;
int direction = 0;
while (true) {
if (last->value > x) {
last->LeftSize--;
parent = last;
last = last->left;
direction = 1;
}
else if (last->value < x) {
parent = last;
last = last->right;
direction = 2;
}
else {
if (last->left == 0 && last->right == 0) {
if (direction == 0) {
head = 0;
}
else if (direction == 1) {
parent->left = 0;
}
else if (direction == 2) {
parent->right = 0;
}
}else if (last->left != 0 && last->right == 0) {
if (direction == 0) {
head = last->left;
}
else if (direction == 1) {
parent->left = last->left;
}
else if (direction == 2) {
parent->right = last->left;
}
}
else if (last->left == 0 && last->right != 0) {
if (direction == 0) {
head = last->right;
}
else if (direction == 1) {
parent->left = last->right;
}
else if (direction == 2) {
parent->right = last->right;
}
}
else if (last->left != 0 && last->right != 0) {
Node* last1 = last->right;
Node* parent1 = last;
int direction1 = 0;
while (last1->left != 0) {
last1->LeftSize--;
parent1 = last1;
last1 = last1->left;
direction1 = 1;
}
if (direction1 == 0) {
last->value = last1->value;
last->right = last1->right;
}
else if(direction1==1){
last->value = last1->value;
parent1->left = 0;
}
}
cout << "[DELETE SUCCESS]" << endl;
break;
}
}
}
void searchByValue(int x) {
if (!isExist(x)) {
cout << "No such value." << endl;
}
else {
int depth = 1;
Node* last = head;
while (true) {
if (last->value > x) {
last = last->left;
depth++;
}
else if (last->value < x) {
last = last->right;
depth++;
}
else {
cout << "LeftSize = " << last->LeftSize << ", Depth = " << depth << endl;
break;
}
}
}
}
void searchByRank(int x) {
int rank = x;
Node* last = head;
while (true) {
if (rank < last->LeftSize) {
last = last->left;
}else if(rank > last->LeftSize&&last->right!=0) {
rank -= last->LeftSize;
last = last->right;
}
else if (rank > last->LeftSize&&last->right == 0) {
cout << "No such value." << endl;
break;
}
else if (rank == last->LeftSize) {
cout << "The "<<x<<"th smallest is "<<last->value << endl;
break;
}
}
}
void Inorder(Node* n) {
if (n != 0) {
Inorder(n->left);
cout << n->value << " ";
Inorder(n->right);
}
}
void Preorder(Node* n) {
if (n != 0) {
cout << n->value << " ";
Preorder(n->left);
Preorder(n->right);
}
}
void Postorder(Node* n) {
if (n != 0) {
Postorder(n->left);
Postorder(n->right);
cout << n->value << " ";
}
}
public:
Node* head = 0;
};
int main()
{
int cmd;
Tree t;
while (cin >> cmd) {
if (cmd == 1) {
int num;
cin >> num;
t.insert(num);
}else if (cmd == 2) {
int num;
cin >> num;
t.Delete(num);
}
else if (cmd == 3) {
int num;
cin >> num;
t.searchByValue(num);
}
else if (cmd == 4) {
int num;
cin >> num;
t.searchByRank(num);
}
else if (cmd == 5) {
t.Inorder(t.head);
cout << endl;
}
else if (cmd == 6) {
t.Preorder(t.head);
cout << endl;
}
else if (cmd == 7) {
t.Postorder(t.head);
cout << endl;
}
}
}