C Programming. PLEASE HELP!

Posted by Slow-Crab-8351@reddit | learnprogramming | View on Reddit | 2 comments

I need help finishing this code. My delete node function is deleting excessive nodes when the node has two children. Can anyone help me?

Node* findMin(Node* node) {

if (!node) return NULL;

while (node->left != NULL){

node = node->left;

}

return node;

}

Node* deleteNode(Node* root, char* name) {

if (!root) return NULL;

int compare = strcmp(name, root->name);

if (compare < 0) {

root->left = deleteNode(root->left, name);

return root;

}

else if (compare > 0){

root->right = deleteNode(root->right, name);

return root;

}

else {//found the node to be deleted

printf("%s deleted\n", name);

if (!root->left) {

Node* temp = root->right;

free(root);

return temp;

} else if (!root->right) {

Node* temp = root->left;

free(root);

return temp;

}

Node* temp = findMin(root->right);

strcpy(root->name, temp->name);

root->tickets = temp->tickets;

root->right = deleteNode(root->right, temp->name);

free(temp);

free(root);

return root;

}

return root;

}