Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This remove function came from cplusplus.com, only slightly tweaked
- void PlayerBST::remove(CardNode* card){
- // Find the item
- bool found = false;
- CardNode *pcard = card;
- if(pcard == NULL){
- cout << "Tree is empty" << endl;
- return;
- }
- while(pcard != NULL){
- if(*pcard == *card){
- found = true;
- break;
- }else{
- pcard->parent = pcard;
- if(*card > *pcard){
- pcard = pcard->right;
- }else{
- pcard = pcard->left;
- }
- }
- }
- if(!found){
- cout << card->suit << card->value << " not in Tree." << endl;
- return;
- }
- // CASE 1: Removing a card with a single child
- if((pcard->left == NULL && pcard->right != NULL) || (pcard->left != NULL && pcard->right == NULL)){
- // Right Leaf Present, No Left Leaf
- if(pcard->left == NULL && pcard->right != NULL){
- // If predecessor's left tree equals card
- if(pcard->parent->left == pcard){
- // then predecessor's left tree becomes card's right tree and delete card
- pcard->parent->left = pcard->right;
- pcard->right->parent = pcard->parent;
- delete pcard;
- pcard = NULL;
- cout << card->suit << card->value << " 1has been removed from the Tree." << endl;
- // If predecessor's right tree equals card
- }else{
- // then predecessor's right tree becomes card's right tree and delete card
- pcard->parent->right = pcard->left;
- pcard->left->parent = pcard->parent;
- delete pcard;
- pcard = NULL;
- cout << card->suit << card->value << " 2has been removed from the Tree." << endl;
- }
- }else{
- // Left Leaf Present, No Right Leaf Present
- //if (pcard->parent->left != NULL){
- if(pcard->parent->left == pcard){
- pcard->parent->left = pcard->right;
- pcard->right->parent = pcard->parent;
- delete pcard;
- pcard = NULL;
- cout << pcard << endl;
- cout << card->suit << card->value << " 3has been removed from the Tree." << endl;
- }else{
- pcard->parent->right = pcard->left;
- pcard->left->parent = pcard->parent;
- delete pcard;
- pcard = NULL;
- cout << card->suit << card->value << " 4has been removed from the Tree." << endl;
- }
- }
- return;
- }
- // CASE 2: Removing a Leaf Node
- if(pcard->left == NULL && pcard->right == NULL){
- if(pcard->parent->left == pcard){
- pcard->parent->left = NULL;
- }else{
- pcard->parent->right = NULL;
- }
- cout << card->suit << card->value << " 6has been removed from the Tree." << endl;
- delete pcard;
- pcard = NULL;
- return;
- }
- // CASE 3: card has two children
- // Replace card with smallest value in right subtree
- if(pcard->left != NULL && pcard->right != NULL){
- CardNode *temp = pcard->right;
- // Right child has children
- // If the card's right child has a left child
- // Move all the way down left to locate smallest element
- if(pcard->right->left != NULL){
- CardNode *temp1;
- temp1 = pcard->right->left;
- while(temp1->left != NULL){
- temp1 = temp1->left;
- }
- temp1->parent = pcard->parent;
- temp1->parent->left = temp1->right;
- temp1->right->parent = temp1->parent;
- *pcard = *temp1;
- delete temp1;
- temp1 = NULL;
- cout << card->suit << card->value << " 8has been removed from the Tree." << endl;
- }else{
- CardNode *temp1 = pcard->right;
- temp1->parent = pcard->parent;
- temp1->parent->right = temp1->left;
- temp1->left->parent = temp1->parent;
- *pcard = *temp1;
- delete temp1;
- temp1 = NULL;
- cout << card->suit << card->value << " 9has been removed from the Tree." << endl;
- }
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement