@@ -15,6 +15,13 @@ struct node {
15
15
struct node * right ;
16
16
} * tRoot = NULL , * tLeaf = NULL ;
17
17
18
+ // Menu functions
19
+ void showMenu ();
20
+ void doInsert ();
21
+ void doDelete ();
22
+ void doSearch ();
23
+ void doInOrder ();
24
+
18
25
// Functions for creating a BST structure
19
26
struct node * newNode (int value ,struct node * parent );
20
27
struct node * insertN (struct node * root ,int value ,struct node * parent );
@@ -36,13 +43,6 @@ struct node* findMax(struct node *root);
36
43
int getHeight (struct node * root ); // Note: This is a generic getHeight function if you want
37
44
// to do as ask by Ma'am Raboy for the project to have getHeight of root, left and right
38
45
// then just use these: getHeight(root), getHeight(root->left), getHeight(root->right)
39
- // Menu functions
40
- void showMenu ();
41
- void doInsert ();
42
- void doDelete ();
43
- void doSearch ();
44
- void doInOrder ();
45
-
46
46
// InOrder print
47
47
void inOrder (struct node * root );
48
48
@@ -214,99 +214,8 @@ struct node* insertN(struct node *root,int value,struct node *parent){
214
214
}
215
215
}
216
216
217
- struct node * searchNode (struct node * root , int value ){
218
- if (root == NULL ) return NULL ;
219
- else {
220
- if (root -> value == value ) return root ;
221
- else {
222
- if (value < root -> value ) return searchNode (root -> left ,value );
223
- else return searchNode (root -> right ,value );
224
- }
225
- }
226
- }
227
-
228
- int getHeight (struct node * root ){
229
- if (root == NULL )
230
- return 0 ;
231
- else
232
- {
233
- /* compute the depth of each subtree */
234
- int lDepth = getHeight (root -> left );
235
- int rDepth = getHeight (root -> right );
236
-
237
- /* use the larger one */
238
- if (lDepth > rDepth )
239
- return (lDepth + 1 );
240
- else return (rDepth + 1 );
241
- }
242
- }
243
-
244
- struct node * findMin (struct node * root ){
245
- // Go to left most part to get the minimum
246
- while (root -> left != NULL ) {
247
- root = root -> left ;
248
- }
249
-
250
- return (root );
251
- }
252
-
253
- struct node * findMax (struct node * root ){
254
- // Go to right most part to get the maximum
255
- while (root -> right != NULL ) {
256
- root = root -> right ;
257
- }
258
-
259
- return (root );
260
- }
261
-
262
- void inOrder (struct node * root ){
263
- if (root == NULL ) return ;
264
- else {
265
- inOrder (root -> left );
266
- printf ("~%d~\n" ,root -> value );
267
- inOrder (root -> right );
268
- }
269
- }
270
-
271
- void bFactorInorder (struct node * root ){
272
- if (root == NULL ) return ;
273
- else {
274
- bFactorInorder (root -> left );
275
- bFactorInorder (root -> right );
276
-
277
- int rHeight = getHeight (root -> right );
278
- int lHeight = getHeight (root -> left );
279
- int bFactor = abs (rHeight - lHeight );
280
- printf ("~Node %d~ \t lHeight: %d \t rHeight: %d \t Balance Factor: %d\n" ,root -> value ,lHeight ,rHeight ,bFactor );
281
- }
282
- }
283
-
284
217
// AVL Functions - As AVL tree is derived from Binary Search Trees
285
218
286
- void balanceCheck (struct node * root ){
287
- if ( getHeight (root -> left ) > (getHeight (root -> right ) + 1 ) ){
288
- printf ("Imbalance! The Right sub-tree of node %d is taller!\n" ,root -> value );
289
- if ( getHeight (root -> left -> right ) > (getHeight (root -> left -> left )) ){
290
- printf ("Rotating using LR Rotation...\n" );
291
- rotateLR (root );
292
- } else {
293
- printf ("Rotating using LL Rotation...\n" );
294
- rotateLL (root );
295
- }
296
-
297
- } else if ( getHeight (root -> right ) > (getHeight (root -> left ) + 1 ) ){
298
- printf ("Imbalance! The Right sub-tree of node %d is taller!\n" ,root -> value );
299
-
300
- if ( getHeight (root -> right -> left ) > (getHeight (root -> right -> right )) ){
301
- printf ("Rotating using RL Rotation...\n" );
302
- rotateRL (root );
303
- } else {
304
- printf ("Rotating using RR Rotation...\n" );
305
- rotateRR (root );
306
- }
307
- }
308
- }
309
-
310
219
void rotateRL (struct node * root ){
311
220
struct node * a = root ;
312
221
struct node * b = root -> right ;
@@ -396,6 +305,43 @@ void rotateLL(struct node *root){
396
305
correctParents (a ,b ,c ,f );
397
306
}
398
307
308
+ void balanceCheck (struct node * root ){
309
+ if ( getHeight (root -> left ) > (getHeight (root -> right ) + 1 ) ){
310
+ printf ("Imbalance! The Right sub-tree of node %d is taller!\n" ,root -> value );
311
+ if ( getHeight (root -> left -> right ) > (getHeight (root -> left -> left )) ){
312
+ printf ("Rotating using LR Rotation...\n" );
313
+ rotateLR (root );
314
+ } else {
315
+ printf ("Rotating using LL Rotation...\n" );
316
+ rotateLL (root );
317
+ }
318
+
319
+ } else if ( getHeight (root -> right ) > (getHeight (root -> left ) + 1 ) ){
320
+ printf ("Imbalance! The Right sub-tree of node %d is taller!\n" ,root -> value );
321
+
322
+ if ( getHeight (root -> right -> left ) > (getHeight (root -> right -> right )) ){
323
+ printf ("Rotating using RL Rotation...\n" );
324
+ rotateRL (root );
325
+ } else {
326
+ printf ("Rotating using RR Rotation...\n" );
327
+ rotateRR (root );
328
+ }
329
+ }
330
+ }
331
+
332
+ void bFactorInorder (struct node * root ){
333
+ if (root == NULL ) return ;
334
+ else {
335
+ bFactorInorder (root -> left );
336
+ bFactorInorder (root -> right );
337
+
338
+ int rHeight = getHeight (root -> right );
339
+ int lHeight = getHeight (root -> left );
340
+ int bFactor = abs (rHeight - lHeight );
341
+ printf ("~Node %d~ \t lHeight: %d \t rHeight: %d \t Balance Factor: %d\n" ,root -> value ,lHeight ,rHeight ,bFactor );
342
+ }
343
+ }
344
+
399
345
void correctParents (struct node * a ,struct node * b ,struct node * c ,struct node * f ){
400
346
if (a -> left != NULL ) a -> left -> parent = a ;
401
347
if (a -> right != NULL ) a -> right -> parent = a ;
@@ -407,3 +353,57 @@ void correctParents(struct node *a,struct node *b,struct node *c,struct node *f)
407
353
if (f != NULL ) f -> right -> parent = f ;
408
354
}
409
355
356
+ struct node * searchNode (struct node * root , int value ){
357
+ if (root == NULL ) return NULL ;
358
+ else {
359
+ if (root -> value == value ) return root ;
360
+ else {
361
+ if (value < root -> value ) return searchNode (root -> left ,value );
362
+ else return searchNode (root -> right ,value );
363
+ }
364
+ }
365
+ }
366
+
367
+ int getHeight (struct node * root ){
368
+ if (root == NULL )
369
+ return 0 ;
370
+ else
371
+ {
372
+ /* compute the depth of each subtree */
373
+ int lDepth = getHeight (root -> left );
374
+ int rDepth = getHeight (root -> right );
375
+
376
+ /* use the larger one */
377
+ if (lDepth > rDepth )
378
+ return (lDepth + 1 );
379
+ else return (rDepth + 1 );
380
+ }
381
+ }
382
+
383
+ struct node * findMin (struct node * root ){
384
+ // Go to left most part to get the minimum
385
+ while (root -> left != NULL ) {
386
+ root = root -> left ;
387
+ }
388
+
389
+ return (root );
390
+ }
391
+
392
+ struct node * findMax (struct node * root ){
393
+ // Go to right most part to get the maximum
394
+ while (root -> right != NULL ) {
395
+ root = root -> right ;
396
+ }
397
+
398
+ return (root );
399
+ }
400
+
401
+ void inOrder (struct node * root ){
402
+ if (root == NULL ) return ;
403
+ else {
404
+ inOrder (root -> left );
405
+ printf ("~%d~\n" ,root -> value );
406
+ inOrder (root -> right );
407
+ }
408
+ }
409
+
0 commit comments