Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. void Delete(int key) {                                  //deletion of an element with given key
  2.    
  3.         bool check = Search(key);                           //using "Search" function to determine if emlement with given key exists within the tree
  4.         Node* parent = NULL;                                //temporary pointer at parent of the element that's going to be deleted
  5.         Node* temp = root;                                  //element that's going to be deleted
  6.        
  7.         if (!check) { return; }                             //if there's no element with given key
  8.  
  9.         else {                                              //there is an element with given key
  10.        
  11.             while (temp->key != key) {                      //loop to determine the temp and it's parent
  12.            
  13.                 parent = temp;
  14.  
  15.                 if (temp->key < key) { temp = temp->right; }
  16.                
  17.                 else { temp = temp->left; }
  18.            
  19.             }
  20.             if (temp->right == NULL && temp->left == NULL) {        //if temp has no subtrees
  21.            
  22.                 if (temp = root) { root = NULL; Initialize(); delete temp; return; }        //if temp is the root, root becomes NULL, initialization of a new BST
  23.            
  24.                 else if (parent->right = temp) { parent->right = NULL; delete temp; return; }   //if temp is parent's right child
  25.  
  26.                 else { parent->left = NULL; delete temp; return; }          //if temp is parent's left child
  27.             }
  28.             else if (temp->right == NULL) {                         //if temp has only left subtree
  29.            
  30.                 if (temp == root) { root = temp->left; delete temp; return; }       //if temp is the root, root becomes it's left child
  31.  
  32.                 else if (parent->right == temp) { parent->right = temp->left; delete temp; return; }  //if temp is parent's right child
  33.  
  34.                 else { parent->left = temp->left; delete temp; return; }        //if temp is parent's left child
  35.            
  36.             }
  37.             else if (temp->left == NULL) {          //if temp has only right subtree
  38.  
  39.                 if (temp == root) { root = temp->right; delete temp; return; }  //if temp is the root, root becomes it's right child
  40.  
  41.                 else if (parent->right == temp) { parent->right = temp->right; delete temp; return; }       //if temp is parent's right child
  42.  
  43.                 else { parent->left = temp->right; delete temp; return; }       //if temp is parent's left child
  44.  
  45.             }
  46.            
  47.             //if temp has both subtrees, algorythm that uses predecessor instead of successor
  48.  
  49.             Node*preparent = temp;              //pointer at temp's predecessor's parent
  50.             Node*child = temp->left;            //pointer at temp's predecessor
  51.  
  52.             while (child->right != NULL) {      //loop to determine temp's predecessor
  53.            
  54.                 preparent = child;
  55.                 child = child->right;
  56.  
  57.             }
  58.  
  59.             if (child == temp->left) {          //if predecessor is temp's left child
  60.            
  61.                 if (temp == root) { root = temp->left; root->right = temp->right; delete temp; return; }        //temp is the root
  62.  
  63.                 else if (parent->right == temp) { parent->right = child; delete temp; return; }         //temp is it's parent's right child
  64.  
  65.                 else { parent->left = child; delete temp; return; }         //temp is it's parent's left child
  66.  
  67.             }
  68.  
  69.             //if temp's predecessor isn't it's child but grandchild or grandgrandchild or so on
  70.  
  71.             Node*grandchild = child->left;   //pointer at possible precdecessor's child
  72.  
  73.             //filling the blank space that appears when we "take out" an node from a tree, that has both subtrees
  74.  
  75.             if (preparent->right == child) { preparent->right = grandchild; }       //if predecessor is it's parent's right child
  76.        
  77.             else { preparent->left = grandchild; }              //if predecessor is it's parent's left child
  78.  
  79.             //after filling the blank space we can delete the node
  80.  
  81.             child->left = temp->left;           //temp's left child becomes predecessor's left child
  82.  
  83.             if (temp == root) { root=child; delete temp; return; }      //if temp is the root, predecessor becomes the root
  84.  
  85.             else if (parent->right == temp) { parent->right = child; delete temp; return; }     //if temp is it's parent's right child
  86.  
  87.             else { parent->left = child; delete temp; return; }         //if temp is it's parent's left child
  88.  
  89.         }
  90.  
  91.    
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement