File tree Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -107,10 +107,18 @@ void doDelete(){
107
107
scanf ("%d" ,& toDelete );
108
108
109
109
dNode = searchNode (tRoot ,toDelete );
110
-
110
+
111
111
if (dNode == NULL ) printf ("The node does not exist!\n" );
112
- else {
113
- printf ("The node is deleted!\n" );
112
+ else if (dNode == tRoot ) {
113
+ free (dNode );
114
+ tRoot = NULL ;
115
+ dNode = NULL ;
116
+ printf ("Node with value %d is deleted!\nThe tree is now empty.\n" );
117
+ printf ("\n\n\nPress any key to continue..." );
118
+ getch ();
119
+ showMenu ();
120
+ } else {
121
+ printf ("Deleting node...\n" );
114
122
temp = dNode ;
115
123
if (dNode -> left == NULL && dNode -> right == NULL ){ // Case 1 : leaf
116
124
if (tRoot == dNode ) tRoot = NULL ;
@@ -146,6 +154,10 @@ void doDelete(){
146
154
if (temp -> parent != NULL ) balanceCheck (temp -> parent );
147
155
temp = NULL ;
148
156
157
+ printf ("The node is now deleted!\n" );
158
+
159
+ if (dNode != NULL ) balanceCheck (dNode );
160
+
149
161
printf ("\n--Balance Factor Sheet--\n" );
150
162
if (tRoot != NULL ) bFactorPostOrder (tRoot );
151
163
else printf ("\nThe tree is empty!\n" );
@@ -361,7 +373,7 @@ struct node* searchNode(struct node *root, int value){
361
373
}
362
374
363
375
int getHeight (struct node * root ){
364
- if (root == NULL )
376
+ if (root == NULL || root == tRoot )
365
377
return 0 ;
366
378
else
367
379
{
You can’t perform that action at this time.
0 commit comments