Skip to content

Instantly share code, notes, and snippets.

@allisons
Created August 9, 2010 23:52
Show Gist options
  • Save allisons/516357 to your computer and use it in GitHub Desktop.
Save allisons/516357 to your computer and use it in GitHub Desktop.
bool BST::remove(const char* key)
{
data aData;
if (!retrieve(key, aData))
cout << "The data you're looking for isn't in the BST" << endl;
return false;
int node = 0;
if(isLeaf(retrievePrivate(key, node)))
{
cout << "Lucky for us, the node we want to remove is a leaf!" << endl;
cout << "I'll just delete that and we'll be done." << endl;
items[node].theData.setName(NULL);
noOfItems--;
return true;
}
else if(items[rightChild(retrievePrivate(key, node))].theData.getName() != NULL && items[leftChild(retrievePrivate(key, node))].theData.getName() != NULL)
{
items[retrievePrivate(key, node)].theData = items[inOrderSuccessor(retrievePrivate(key, node))].theData;
items[inOrderSuccessor(retrievePrivate(key, node))].theData.setName(NULL);
noOfItems--;
return true;
}
else if(items[leftChild(retrievePrivate(key, node))].theData.getName() != NULL)
{
moveUp(leftChild(node));
noOfItems--;
return true;
}
else
{
moveUp(rightChild(node));
noOfItems--;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment