@@ -37,9 +37,9 @@ void correctParents(struct node *a,struct node *b,struct node *c,struct node *f)
37
37
struct node * searchNode (struct node * root , int value ); // Return NULL if not found or Pointer to node if found
38
38
struct node * findMin (struct node * root );
39
39
struct node * findMax (struct node * root );
40
- int getHeight (struct node * root ); // Note: This is a generic getHeight function if you want
41
- // to do as ask by Ma'am Raboy for the project to have getHeight of root, left and right
42
- // then just use these: getHeight(root), getHeight(root->left), getHeight(root->right)
40
+ int getHeight (struct node * root );
41
+ int getHeightRoot ( struct node * root ); // Getting height of root
42
+
43
43
// InOrder print
44
44
void inOrder (struct node * root );
45
45
@@ -140,11 +140,14 @@ void doDelete(){
140
140
else predecessor -> parent -> left = predecessor -> left ;
141
141
142
142
predecessor -> left -> parent = predecessor -> parent ;
143
+ } else {
144
+ if (predecessor -> parent -> right == predecessor )predecessor -> parent -> right = NULL ;
145
+ else predecessor -> parent -> left = NULL ;
143
146
}
144
-
147
+
145
148
dNode -> value = predecessor -> value ;
149
+
146
150
free (predecessor );
147
-
148
151
} else {
149
152
successor = findMin (dNode -> right ); // Get successor
150
153
// update successor parent
@@ -154,7 +157,10 @@ void doDelete(){
154
157
else successor -> parent -> left = successor -> right ;
155
158
156
159
successor -> right -> parent = successor -> parent ;
157
- }
160
+ } else {
161
+ if (successor -> parent -> right == successor )successor -> parent -> right = NULL ;
162
+ else successor -> parent -> left = NULL ;
163
+ }
158
164
159
165
dNode -> value = successor -> value ;
160
166
free (successor );
@@ -187,7 +193,7 @@ void doSearch(){
187
193
struct node * nodeF = searchNode (tRoot ,toFind );
188
194
if (nodeF != NULL ){
189
195
printf ("Found Node %d!\n" ,toFind );
190
- printf ("Node %d Height: %d\n" ,toFind ,getHeight (nodeF ));
196
+ printf ("Node %d Height: %d\n" ,toFind ,getHeightRoot (nodeF ));
191
197
printf ("Node %d Left sub-tree Height: %d\n" ,toFind ,getHeight (nodeF -> left ));
192
198
printf ("Node %d Right subtree Height: %d\n" ,toFind ,getHeight (nodeF -> right ));
193
199
} else printf ("Not found!\n" );
@@ -369,7 +375,7 @@ void bFactorPostOrder(struct node *root){
369
375
int rHeight = getHeight (root -> right );
370
376
int lHeight = getHeight (root -> left );
371
377
int bFactor = abs (rHeight - lHeight );
372
- printf ("~Node %d~ \t lHeight: %d \t rHeight: %d \t Balance Factor: %d\n" ,root -> value ,lHeight ,rHeight ,bFactor );
378
+ if ( root -> value != ( int ) NULL ) printf ("~Node %d~ \t lHeight: %d \t rHeight: %d \t Balance Factor: %d\n" ,root -> value ,lHeight ,rHeight ,bFactor );
373
379
}
374
380
}
375
381
@@ -409,6 +415,14 @@ int getHeight(struct node *root){
409
415
}
410
416
}
411
417
418
+ int getHeightRoot (struct node * root ){
419
+ if (root == NULL ) return 0 ;
420
+ else {
421
+ if (getHeight (root -> left ) > getHeight (root -> right )) return getHeight (root -> left );
422
+ else return getHeight (root -> right );
423
+ }
424
+ }
425
+
412
426
struct node * findMin (struct node * root ){
413
427
// Go to left most part to get the minimum
414
428
while (root -> left != NULL ) {
@@ -431,7 +445,7 @@ void inOrder(struct node *root){
431
445
if (root == NULL ) return ;
432
446
else {
433
447
inOrder (root -> left );
434
- printf ("~%d~\n" ,root -> value );
448
+ if ( root -> value != ( int ) NULL ) printf ("~%d~\n" ,root -> value );
435
449
inOrder (root -> right );
436
450
}
437
451
}
0 commit comments