Skip to content

Instantly share code, notes, and snippets.

@davelyon
Created April 1, 2009 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davelyon/88509 to your computer and use it in GitHub Desktop.
Save davelyon/88509 to your computer and use it in GitHub Desktop.
BinaryNode* BinaryNode::remove(char *n) {
int test = strcmp(n,name);
BinaryNode *temp;
if(test > 0) { //Name we're deleting is bigger
if(right != NULL){
right = right->remove(n);
return this;
}
}
if(test < 0){ //Name we're deleting is smaller
if(left != NULL){
left = left->remove(n);
return this;
}
}
if(test == 0) {
temp = NULL;
if(left != NULL && right != NULL){
temp = right->goLeft();
if(right != temp)
temp->right = this->right;
else
temp->right = NULL;
temp->left = this->left;
}else{
if(left != NULL){
temp = left;
}
if(right != NULL){
temp = right;
}
}
left = NULL;
right = NULL;
delete(this);
cout << n << " was deleted." << endl;
return temp;
}
if( (left == NULL && right == NULL)
|| (left == NULL && test < 0)
|| (right == NULL && test > 0) )
cout << n << " is not in the gradebook and cannot be removed." << endl;
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment