Skip to content

Commit 81a67a1

Browse files
author
Raven
committed
Updated deletion bugs
1 parent 8fed187 commit 81a67a1

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

avl.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ void correctParents(struct node *a,struct node *b,struct node *c,struct node *f)
3737
struct node* searchNode(struct node *root, int value); // Return NULL if not found or Pointer to node if found
3838
struct node* findMin(struct node *root);
3939
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+
4343
// InOrder print
4444
void inOrder(struct node *root);
4545

@@ -140,11 +140,14 @@ void doDelete(){
140140
else predecessor->parent->left = predecessor->left;
141141

142142
predecessor->left->parent = predecessor->parent;
143+
} else {
144+
if (predecessor->parent->right == predecessor)predecessor->parent->right = NULL;
145+
else predecessor->parent->left = NULL;
143146
}
144-
147+
145148
dNode->value = predecessor->value;
149+
146150
free(predecessor);
147-
148151
} else {
149152
successor = findMin(dNode->right); // Get successor
150153
// update successor parent
@@ -154,7 +157,10 @@ void doDelete(){
154157
else successor->parent->left = successor->right;
155158

156159
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+
}
158164

159165
dNode->value = successor->value;
160166
free(successor);
@@ -187,7 +193,7 @@ void doSearch(){
187193
struct node *nodeF = searchNode(tRoot,toFind);
188194
if (nodeF!= NULL){
189195
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));
191197
printf("Node %d Left sub-tree Height: %d\n",toFind,getHeight(nodeF->left));
192198
printf("Node %d Right subtree Height: %d\n",toFind,getHeight(nodeF->right));
193199
} else printf("Not found!\n");
@@ -369,7 +375,7 @@ void bFactorPostOrder(struct node *root){
369375
int rHeight = getHeight(root->right);
370376
int lHeight = getHeight(root->left);
371377
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);
373379
}
374380
}
375381

@@ -409,6 +415,14 @@ int getHeight(struct node *root){
409415
}
410416
}
411417

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+
412426
struct node* findMin(struct node *root){
413427
// Go to left most part to get the minimum
414428
while (root->left != NULL) {
@@ -431,7 +445,7 @@ void inOrder(struct node *root){
431445
if (root == NULL) return;
432446
else {
433447
inOrder(root->left);
434-
printf("~%d~\n",root->value);
448+
if (root->value != (int)NULL)printf("~%d~\n",root->value);
435449
inOrder(root->right);
436450
}
437451
}

0 commit comments

Comments
 (0)