-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaba.cpp
More file actions
364 lines (304 loc) · 8.06 KB
/
laba.cpp
File metadata and controls
364 lines (304 loc) · 8.06 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
// Matt Matto
// Lab A
// implements the method in laba.h
// preorder print, postorder print, depth, height, and rotate
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include "laba.h"
using namespace std;
// recursively prints the BST in preorder format
void BSTree::recursive_preorder(int level, BSTNode *n) {
// prints the correct amount of spcaes
for (int i = 0; i < level; i++) {
printf(" ");
}
// prints the name
printf("%s\n", n->key.c_str());
// recursively goes down the tree to print
if (n->left != sentinel) recursive_preorder(level + 1, n->left);
if (n->right != sentinel) recursive_preorder(level + 1, n->right);
// returns
return;
}
// recursively prints the BST in postorder format
void BSTree::recursive_postorder(int level, BSTNode *n) {
//recursively goes down the tree to print
if (n->left != sentinel) recursive_postorder(level + 1, n->left);
if (n->right != sentinel) recursive_postorder(level + 1, n->right);
// prints the correct number of spaces
for (int i = 0; i < level; i++) {
printf(" ");
}
// prints the name
printf("%s\n", n->key.c_str());
return;
}
int BSTree::recursive_height(BSTNode *n) {
// initializes the right and left height ints
int l = 0, r = 0;
// checks for if you have reached a leaf
if (n == sentinel) return -1;
// goes down the right and left branch
l = recursive_height(n->left);
r = recursive_height(n->right);
// returns the bigger of the two height ints
if (l > r) return (l + 1);
else return (r + 1);
}
// calls the recursive postorder function
void BSTree::Postorder() {
recursive_postorder(0, sentinel->right);
}
// calls the recursive preorder function
void BSTree::Preorder() {
recursive_preorder(0, sentinel->right);
}
// returns the depth of a given key
int BSTree::Depth(string key) {
// initializes the depth counter and BSTNode iterator
int count = 0;
BSTNode *n = sentinel->right;
// searches until it reaches a sentinel, signaling the key was not found
while (n != sentinel) {
// goes left down the tree
if (key < n->key) {
count++;
n = n->left;
// goes right down the tree
} else if (key > n->key) {
count++;
n = n->right;
// if the key was found, returns the depth of the key
} else if (key == n->key) {
return count;
}
}
// if it cant find the key, returns -1
return -1;
}
// checks to see if the tree if empty, then calls the recursive height function
int BSTree::Height() {
if (Empty()) return -1;
return recursive_height(sentinel->right);
}
// checks if a BST is also and AVL tree
int BSTree::IsAVL() {
// if it is empty, returns -1
if (Empty()) return -1;
//calls the recursive height and avl check function
int height;
height = recursive_height_and_avl_check(sentinel->right);
// if the function returned -2, then the tree is not an AVL tree
if (height == -2) return 0;
else return 1;
}
// checks if a tree is an AVL tree
int BSTree::recursive_height_and_avl_check(BSTNode *n) {
// initializes the right and left heights
int l = 0, r = 0;
if (n == sentinel) return 0;
// calls the function for the left and right branches and goes down the tree
l = recursive_height_and_avl_check(n->left);
r = recursive_height_and_avl_check(n->right);
// checks that the left anf right heights differ by no more than 1
// also checks if a -2 is being passed up the function
if (((l - r) < -1) or ((l - r) > 1) or (l == -2) or (r == -2)) return -2;
// returns the bigger of te two heights
if (l > r) return (l + 1);
else return (r + 1);
}
// rotates a branch around a particular node
int BSTree::Rotate(string key) {
BSTNode *n = sentinel->right;
BSTNode *p;
// searches for a key, and if it is found rotates the tree
while (n != sentinel) {
// the key was found
if (n->key == key) {
// checks to make sure the tree is not empty
if (n == sentinel->right) return 0;
// checks if n is to the left of its parent
if (n == n->parent->left) {
// sets the parent pointer(p) and n's parent pointer is p's parent
p = n->parent;
n->parent = p->parent;
// checks and adjusts the grandparent's pointer to n
if (n->parent->right == p) p->parent->right = n;
else if (p->parent->left == p) p->parent->left = n;
// keep the middle node in the middle
p->left = n->right;
// moves the last few around
if (n->right != sentinel) n->right->parent = p;
n->right = p;
// if n is to the right of the parent
} else {
//sets the parent pointer(p) nd n's parent pointer to p's parent
p = n->parent;
n->parent = p->parent;
// adjusts the grandparent pointer to n
if (p->parent->right == p) p->parent->right = n;
else if (p->parent->left == p) p->parent->left = n;
// keeps the middle pointer in the middle
p->right = n->left;
// makes sure n->left is filled and if it is pointer it back to p
if (n->left != sentinel) n->left->parent = p;
// n becoms the parent to p
n->left = p;
}
// p points back to n
p->parent = n;
// success
return 1;
}
// travels down through the tree trying to find the key
else if (key < n->key) n = n->left;
else n = n->right;
}
// it failed
return 0;
}
// ------------------------------------------------------------------------
// eveything after this was already in laba.cpp
// ------------------------------------------------------------------------
BSTree::BSTree()
{
sentinel = new BSTNode;
sentinel->parent = NULL;
sentinel->left = NULL;
sentinel->right = sentinel;
size = 0;
}
BSTree::~BSTree()
{
recursive_destroy(sentinel->right);
delete sentinel;
}
int BSTree::Insert(string s, void *val)
{
BSTNode *parent;
BSTNode *n;
parent = sentinel;
n = sentinel->right;
while (n != sentinel) {
if (n->key == s) return 0;
parent = n;
if (s < n->key) {
n = n->left;
} else {
n = n->right;
}
}
n = new BSTNode;
n->key = s;
n->val = val;
n->parent = parent;
n->left = sentinel;
n->right = sentinel;
if (parent == sentinel) {
sentinel->right = n;
} else if (s < parent->key) {
parent->left = n;
} else {
parent->right = n;
}
size++;
return 1;
}
void *BSTree::Find(string s)
{
BSTNode *n;
n = sentinel->right;
while (1) {
if (n == sentinel) return NULL;
if (s == n->key) return n->val;
if (s < n->key) {
n = n->left;
} else {
n = n->right;
}
}
}
int BSTree::Delete(string s)
{
BSTNode *n, *parent, *mlc;
string tmpkey;
void *tmpval;
n = sentinel->right;
while (n != sentinel && s != n->key) {
if (s < n->key) {
n = n->left;
} else {
n = n->right;
}
}
if (n == sentinel) return 0;
parent = n->parent;
if (n->left == sentinel) {
if (n == parent->left) {
parent->left = n->right;
} else {
parent->right = n->right;
}
if (n->right != sentinel) n->right->parent = parent;
delete n;
size--;
} else if (n->right == sentinel) {
if (n == parent->left) {
parent->left = n->left;
} else {
parent->right = n->left;
}
n->left->parent = parent;
delete n;
size--;
} else {
for (mlc = n->left; mlc->right != sentinel; mlc = mlc->right) ;
tmpkey = mlc->key;
tmpval = mlc->val;
Delete(tmpkey);
n->key = tmpkey;
n->val = tmpval;
}
return 1;
}
vector <void *>BSTree::Sorted_Vector()
{
array.clear();
recursive_make_vector(sentinel->right);
return array;
}
void BSTree::Print()
{
recursive_inorder_print(0, sentinel->right);
}
int BSTree::Size()
{
return size;
}
int BSTree::Empty()
{
return (size == 0);
}
void BSTree::recursive_inorder_print(int level, BSTNode *n)
{
if (n == sentinel) return;
recursive_inorder_print(level+2, n->right);
printf("%*s%s\n", level, "", n->key.c_str());
recursive_inorder_print(level+2, n->left);
}
void BSTree::recursive_make_vector(BSTNode *n)
{
if (n == sentinel) return;
recursive_make_vector(n->left);
array.push_back(n->val);
recursive_make_vector(n->right);
}
void BSTree::recursive_destroy(BSTNode *n)
{
if (n == sentinel) return;
recursive_destroy(n->left);
recursive_destroy(n->right);
delete n;
}