-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSpaceGoat_Tree.cpp
More file actions
351 lines (330 loc) · 8.36 KB
/
SpaceGoat_Tree.cpp
File metadata and controls
351 lines (330 loc) · 8.36 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* C++ Program to Implement ScapeGoat Tree
*/
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
/*
* Class SGTNode
*/
class SGTNode
{
public:
SGTNode *right, *left, *parent;
int value;
SGTNode()
{
value = 0;
right = NULL;
left = NULL;
parent = NULL;
}
SGTNode(int val)
{
value = val;
right = NULL;
left = NULL;
parent = NULL;
}
};
/*
* Class ScapeGoatTree
*/
class ScapeGoatTree
{
private:
SGTNode *root;
int n, q;
public:
ScapeGoatTree()
{
root = NULL;
n = 0;
}
/* Function to check if tree is empty */
bool isEmpty()
{
return root == NULL;
}
/* Function to clear tree */
void makeEmpty()
{
root = NULL;
n = 0;
}
/* Function to count number of nodes recursively */
int size(SGTNode *r)
{
if (r == NULL)
return 0;
else
{
int l = 1;
l += size(r->left);
l += size(r->right);
return l;
}
}
/* Functions to search for an element */
bool search(int val)
{
return search(root, val);
}
/* Function to search for an element recursively */
bool search(SGTNode *r, int val)
{
bool found = false;
while ((r != NULL) && !found)
{
int rval = r->value;
if (val < rval)
r = r->left;
else if (val > rval)
r = r->right;
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
/* Function to return current size of tree */
int size()
{
return n;
}
/* Function for inorder traversal */
void inorder()
{
inorder(root);
}
void inorder(SGTNode *r)
{
if (r != NULL)
{
inorder(r->left);
cout<<r->value<<" ";
inorder(r->right);
}
else
return;
}
/* Function for preorder traversal */
void preorder()
{
preorder(root);
}
void preorder(SGTNode *r)
{
if (r != NULL)
{
cout<<r->value<<" ";
preorder(r->left);
preorder(r->right);
}
else
return;
}
/* Function for postorder traversal */
void postorder()
{
postorder(root);
}
void postorder(SGTNode *r)
{
if (r != NULL)
{
postorder(r->left);
postorder(r->right);
cout<<r->value<<" ";
}
else
return;
}
static int const log32(int q)
{
double const log23 = 2.4663034623764317;
return (int)ceil(log23 * log(q));
}
/* Function to insert an element */
bool add(int x)
{
/* first do basic insertion keeping track of depth */
SGTNode *u = new SGTNode(x);
int d = addWithDepth(u);
if (d > log32(q))
{
/* depth exceeded, find scapegoat */
SGTNode *w = u->parent;
while (3 * size(w) <= 2 * size(w->parent))
w = w->parent;
rebuild(w->parent);
}
return d >= 0;
}
/* Function to rebuild tree from node u */
void rebuild(SGTNode *u)
{
int ns = size(u);
SGTNode *p = u->parent;
SGTNode **a = new SGTNode* [ns];
packIntoArray(u, a, 0);
if (p == NULL)
{
root = buildBalanced(a, 0, ns);
root->parent = NULL;
}
else if (p->right == u)
{
p->right = buildBalanced(a, 0, ns);
p->right->parent = p;
}
else
{
p->left = buildBalanced(a, 0, ns);
p->left->parent = p;
}
}
/* Function to packIntoArray */
int packIntoArray(SGTNode *u, SGTNode *a[], int i)
{
if (u == NULL)
{
return i;
}
i = packIntoArray(u->left, a, i);
a[i++] = u;
return packIntoArray(u->right, a, i);
}
/* Function to build balanced nodes */
SGTNode *buildBalanced(SGTNode **a, int i, int ns)
{
if (ns == 0)
return NULL;
int m = ns / 2;
a[i + m]->left = buildBalanced(a, i, m);
if (a[i + m]->left != NULL)
a[i + m]->left->parent = a[i + m];
a[i + m]->right = buildBalanced(a, i + m + 1, ns - m - 1);\
if (a[i + m]->right != NULL)
a[i + m]->right->parent = a[i + m];
return a[i + m];
}
/* Function add with depth */
int addWithDepth(SGTNode *u)
{
SGTNode *w = root;
if (w == NULL)
{
root = u;
n++;
q++;
return 0;
}
bool done = false;
int d = 0;
do
{
if (u->value < w->value)
{
if (w->left == NULL)
{
w->left = u;
u->parent = w;
done = true;
}
else
{
w = w->left;
}
}
else if (u->value > w->value)
{
if (w->right == NULL)
{
w->right = u;
u->parent = w;
done = true;
}
else
{
w = w->right;
}
}
else
return -1;
d++;
}
while (!done);
n++;
q++;
return d;
}
};
int main()
{
ScapeGoatTree sgt;
cout<<"ScapeGoat Tree Test"<<endl;
char ch;
int val;
/* Perform tree operations */
do
{
cout<<"\nScapeGoat Tree Operations\n";
cout<<"1. Insert "<<endl;
cout<<"2. Count nodes"<<endl;
cout<<"3. Search"<<endl;
cout<<"4. Check empty"<<endl;
cout<<"5. Make empty"<<endl;
int choice;
cout<<"Enter your Choice: ";
cin>>choice;
switch (choice)
{
case 1 :
cout<<"Enter integer element to insert: ";
cin>>val;
sgt.add(val);
break;
case 2 :
cout<<"Nodes = " <<sgt.size()<<endl;
break;
case 3 :
cout<<"Enter integer element to search: ";
cin>>val;
if (sgt.search(val))
cout<<val<<" found in the tree"<<endl;
else
cout<<val<<" not found"<<endl;
break;
case 4 :
cout<<"Empty status = ";
if (sgt.isEmpty())
cout<<"Tree is empty"<<endl;
else
cout<<"Tree is non - empty"<<endl;
break;
case 5 :
cout<<"\nTree cleared\n";
sgt.makeEmpty();
break;
default :
cout<<"Wrong Entry \n ";
break;
}
/* Display tree*/
cout<<"\nPost order : ";
sgt.postorder();
cout<<"\nPre order : ";
sgt.preorder();
cout<<"\nIn order : ";
sgt.inorder();
cout<<"\nDo you want to continue (Type y or n) \n";
cin>>ch;
}
while (ch == 'Y'|| ch == 'y');
return 0;
}