Created
August 9, 2010 23:52
-
-
Save allisons/516357 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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