forked from PearsonReese/GroupProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupProjectDriver.cpp
More file actions
396 lines (366 loc) · 8.73 KB
/
GroupProjectDriver.cpp
File metadata and controls
396 lines (366 loc) · 8.73 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include "math.h"
using namespace std;
typedef struct splay {
int data;
struct splay* left;
struct splay* right;
struct splay* parent;
}splay;
splay* root = NULL;
class SplayTree
{
public:
SplayTree()
{
}
// RR(Y rotates to the right)
splay* RR_Rotate(splay* k2)
{
splay* k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
return k1;
}
// LL(Y rotates to the left)
splay* LL_Rotate(splay* k2)
{
splay* k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
return k1;
}
// An implementation of top-down splay tree
splay* Splay(int data, splay* root)
{
if (!root)
return NULL;
splay header;
/* header.right points to L tree;
header.left points to R Tree */
header.left = header.right = NULL;
splay* LeftTreeMax = &header;
splay* RightTreeMin = &header;
while (1)
{
if (data < root->data)
{
if (!root->left)
break;
if (data < root->left->data)
{
root = RR_Rotate(root);
// only zig-zig mode need to rotate once,
if (!root->left)
break;
}
/* Link to R Tree */
RightTreeMin->left = root;
RightTreeMin = RightTreeMin->left;
root = root->left;
RightTreeMin->left = NULL;
}
else if (data > root->data)
{
if (!root->right)
break;
if (data > root->right->data)
{
root = LL_Rotate(root);
// only zag-zag mode need to rotate once,
if (!root->right)
break;
}
/* Link to L Tree */
LeftTreeMax->right = root;
LeftTreeMax = LeftTreeMax->right;
root = root->right;
LeftTreeMax->right = NULL;
}
else
break;
}
/* assemble L Tree, Middle Tree and R tree */
LeftTreeMax->right = root->left;
RightTreeMin->left = root->right;
root->left = header.right;
root->right = header.left;
return root;
}
splay* New_Node(int data)
{
splay* p_node = new splay;
if (!p_node)
{
fprintf(stderr, "Out of memory!\n");
exit(1);
}
p_node->data = data;
p_node->left = p_node->right = NULL;
return p_node;
}
splay* Insert(int data, splay* root)
{
static splay* p_node = NULL;
if (!p_node)
p_node = New_Node(data);
else
p_node->data = data;
if (!root)
{
root = p_node;
p_node = NULL;
return root;
}
root = Splay(data, root);
/* This is BST that, all datas <= root->data is in root->left, all datas >
root->data is in root->right. */
if (data < root->data)
{
p_node->left = root->left;
p_node->right = root;
root->left = NULL;
root = p_node;
}
else if (data > root->data)
{
p_node->right = root->right;
p_node->left = root;
root->right = NULL;
root = p_node;
}
else
return root;
p_node = NULL;
return root;
}
splay* Delete(int data, splay* root)
{
splay* temp;
if (!root)
return NULL;
root = Splay(data, root);
if (data != root->data)
return root;
else
{
if (!root->left)
{
temp = root;
root = root->right;
}
else
{
temp = root;
/*Note: Since data == root->data,
so after Splay(data, root->left),
the tree we get will have no right child tree.*/
root = Splay(data, root->left);
root->right = temp->right;
}
free(temp);
return root;
}
}
splay* Search(int data, splay* root)
{
return Splay(data, root);
}
void InOrder(splay* root)
{
if (root)
{
InOrder(root->left);
cout << "data: " << root->data;
if (root->left)
cout << " | left child: " << root->left->data;
if (root->right)
cout << " | right child: " << root->right->data;
cout << "\n";
InOrder(root->right);
}
}
};
void add_node(splay** t_root, splay** current) {
if(*t_root==NULL) {
*t_root = *current;
return;
}
if((*current)->data < (*t_root)->data) {
if((*t_root)->left==NULL) {
(*t_root)->left = (*current);
} else {
add_node(&((*t_root)->left),current);
}
} else {
if((*t_root)->right==NULL) {
(*t_root)->right = *current;
} else {
add_node(&((*t_root)->right),current);
}
}
}
void accept_tree(){
int n;
SplayTree st;
cout<<"Create splay tree: (enter 0 to finish)\n";
while(1) {
fflush(stdin);
cout<<"\nEnter node value: ";
cin>>n;
if(n==0) {
break;
}
root =st.Insert(n,root);
}
root = st.Delete(4, root);
}
void inorder(splay* root) {
if(root!=NULL) {
inorder(root->left);
cout<<root->data;
inorder(root->right);
}
}
void preorder(splay* root) {
if(root!=NULL) {
cout<<root->data;
preorder(root->left);
preorder(root->right);
}
}
void postorder(splay* root) {
if(root!=NULL) {
postorder(root->left);
postorder(root->right);
cout<<root->data;
}
}
void drawCircle(float segments, float radius, float sx, float sy)
{
float theta = 2 * 3.1415926 / segments;
float tan_factor = tanf(theta);
float radial_factor = cosf(theta);
float x = radius;
float y = 0;
int cache_pt = 0;
double cache_x;
double cache_y;
glBegin(GL_LINES);
for (int ii = 0; ii < segments; ii++) {
if(!cache_pt) {
cache_x = x+sx;
cache_y = y+sy;
cache_pt = 1;
} else {
//glVertex2f(cache_x,cache_y);
glVertex2f(x + sx, y + sy);
cache_x = x+sx;
cache_y = y+sy;
}
float tx = -y;
float ty = x;
x += tx * tan_factor;
y += ty * tan_factor;
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}
void draw_line(float x1,float y1,float x2, float y2) {
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}
void draw_text(char* text,float x, float y) {
GLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_24;
glRasterPos3f(x, y, 0);
for (int i = 0; text[i] != '\0'; i++){
glutBitmapCharacter(font_style, text[i]);
}
}
void drawNode(splay* t_root,float x1,float y1,int level) {
if (t_root==NULL) {
return;
}
float segments = 25;
float radius = 1.0;
float left_angle = 245;
float right_angle = 115;
float branch_length = 12 - level*2.5;
float angle_change = 20;
// Draw the current circle
drawCircle(segments,radius,x1,y1);
char buff[5];
itoa(t_root->data,buff,10);
draw_text(buff,x1,y1);
if(t_root->left) {
// Draw the Left circle
float angle = left_angle - level*angle_change;
double radian = angle*3.14/180;
float m = (double)tan((double)radian);
float x2 = x1 + branch_length * sin((double) radian);
float y2 = y1 + branch_length * cos((double) radian);
drawNode(t_root->left,x2,y2,level+1);
draw_line(x1,y1,x2,y2);
}
if(t_root->right) {
// Draw the Right circle
float angle = right_angle + level*angle_change;
float radian = angle*3.14/180;
float m = (double)tan((double)radian);
float x2 = x1 + branch_length * sin((double) radian);
float y2 = y1 + branch_length * cos((double) radian);
drawNode(t_root->right,x2,y2,level+1);
draw_line(x1,y1,x2,y2);
}
}
void display(void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,10,-30);
glColor3f(1,1,1);
drawNode(root,0,0,0);
glutSwapBuffers();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main (int argc, char **argv) {
accept_tree();
inorder(root);
// OPENGL Drawing functions
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (1200, 800);
glutInitWindowPosition (0, 0);
glutCreateWindow ("A splay tree");
// Register function pointers to the drawing framework
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();
return 0;
}