Advertisement
Guest User

Untitled

a guest
Mar 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. // This remove function came from cplusplus.com, only slightly tweaked
  2. void PlayerBST::remove(CardNode* card){
  3. // Find the item
  4. bool found = false;
  5. CardNode *pcard = card;
  6. if(pcard == NULL){
  7. cout << "Tree is empty" << endl;
  8. return;
  9. }
  10. while(pcard != NULL){
  11. if(*pcard == *card){
  12. found = true;
  13. break;
  14. }else{
  15. pcard->parent = pcard;
  16. if(*card > *pcard){
  17. pcard = pcard->right;
  18. }else{
  19. pcard = pcard->left;
  20. }
  21. }
  22. }
  23. if(!found){
  24. cout << card->suit << card->value << " not in Tree." << endl;
  25. return;
  26. }
  27. // CASE 1: Removing a card with a single child
  28. if((pcard->left == NULL && pcard->right != NULL) || (pcard->left != NULL && pcard->right == NULL)){
  29. // Right Leaf Present, No Left Leaf
  30. if(pcard->left == NULL && pcard->right != NULL){
  31. // If predecessor's left tree equals card
  32. if(pcard->parent->left == pcard){
  33. // then predecessor's left tree becomes card's right tree and delete card
  34. pcard->parent->left = pcard->right;
  35. pcard->right->parent = pcard->parent;
  36. delete pcard;
  37. pcard = NULL;
  38. cout << card->suit << card->value << " 1has been removed from the Tree." << endl;
  39. // If predecessor's right tree equals card
  40. }else{
  41. // then predecessor's right tree becomes card's right tree and delete card
  42. pcard->parent->right = pcard->left;
  43. pcard->left->parent = pcard->parent;
  44. delete pcard;
  45. pcard = NULL;
  46. cout << card->suit << card->value << " 2has been removed from the Tree." << endl;
  47. }
  48. }else{
  49. // Left Leaf Present, No Right Leaf Present
  50. //if (pcard->parent->left != NULL){
  51. if(pcard->parent->left == pcard){
  52. pcard->parent->left = pcard->right;
  53. pcard->right->parent = pcard->parent;
  54. delete pcard;
  55. pcard = NULL;
  56. cout << pcard << endl;
  57. cout << card->suit << card->value << " 3has been removed from the Tree." << endl;
  58. }else{
  59. pcard->parent->right = pcard->left;
  60. pcard->left->parent = pcard->parent;
  61. delete pcard;
  62. pcard = NULL;
  63. cout << card->suit << card->value << " 4has been removed from the Tree." << endl;
  64. }
  65. }
  66. return;
  67. }
  68.  
  69. // CASE 2: Removing a Leaf Node
  70. if(pcard->left == NULL && pcard->right == NULL){
  71. if(pcard->parent->left == pcard){
  72. pcard->parent->left = NULL;
  73. }else{
  74. pcard->parent->right = NULL;
  75. }
  76. cout << card->suit << card->value << " 6has been removed from the Tree." << endl;
  77. delete pcard;
  78. pcard = NULL;
  79. return;
  80. }
  81.  
  82. // CASE 3: card has two children
  83. // Replace card with smallest value in right subtree
  84. if(pcard->left != NULL && pcard->right != NULL){
  85. CardNode *temp = pcard->right;
  86. // Right child has children
  87. // If the card's right child has a left child
  88. // Move all the way down left to locate smallest element
  89. if(pcard->right->left != NULL){
  90. CardNode *temp1;
  91. temp1 = pcard->right->left;
  92. while(temp1->left != NULL){
  93. temp1 = temp1->left;
  94. }
  95. temp1->parent = pcard->parent;
  96. temp1->parent->left = temp1->right;
  97. temp1->right->parent = temp1->parent;
  98. *pcard = *temp1;
  99. delete temp1;
  100. temp1 = NULL;
  101. cout << card->suit << card->value << " 8has been removed from the Tree." << endl;
  102. }else{
  103. CardNode *temp1 = pcard->right;
  104. temp1->parent = pcard->parent;
  105. temp1->parent->right = temp1->left;
  106. temp1->left->parent = temp1->parent;
  107. *pcard = *temp1;
  108. delete temp1;
  109. temp1 = NULL;
  110. cout << card->suit << card->value << " 9has been removed from the Tree." << endl;
  111. }
  112. return;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement