Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Delete(int key) { //deletion of an element with given key
- bool check = Search(key); //using "Search" function to determine if emlement with given key exists within the tree
- Node* parent = NULL; //temporary pointer at parent of the element that's going to be deleted
- Node* temp = root; //element that's going to be deleted
- if (!check) { return; } //if there's no element with given key
- else { //there is an element with given key
- while (temp->key != key) { //loop to determine the temp and it's parent
- parent = temp;
- if (temp->key < key) { temp = temp->right; }
- else { temp = temp->left; }
- }
- if (temp->right == NULL && temp->left == NULL) { //if temp has no subtrees
- if (temp = root) { root = NULL; Initialize(); delete temp; return; } //if temp is the root, root becomes NULL, initialization of a new BST
- else if (parent->right = temp) { parent->right = NULL; delete temp; return; } //if temp is parent's right child
- else { parent->left = NULL; delete temp; return; } //if temp is parent's left child
- }
- else if (temp->right == NULL) { //if temp has only left subtree
- if (temp == root) { root = temp->left; delete temp; return; } //if temp is the root, root becomes it's left child
- else if (parent->right == temp) { parent->right = temp->left; delete temp; return; } //if temp is parent's right child
- else { parent->left = temp->left; delete temp; return; } //if temp is parent's left child
- }
- else if (temp->left == NULL) { //if temp has only right subtree
- if (temp == root) { root = temp->right; delete temp; return; } //if temp is the root, root becomes it's right child
- else if (parent->right == temp) { parent->right = temp->right; delete temp; return; } //if temp is parent's right child
- else { parent->left = temp->right; delete temp; return; } //if temp is parent's left child
- }
- //if temp has both subtrees, algorythm that uses predecessor instead of successor
- Node*preparent = temp; //pointer at temp's predecessor's parent
- Node*child = temp->left; //pointer at temp's predecessor
- while (child->right != NULL) { //loop to determine temp's predecessor
- preparent = child;
- child = child->right;
- }
- if (child == temp->left) { //if predecessor is temp's left child
- if (temp == root) { root = temp->left; root->right = temp->right; delete temp; return; } //temp is the root
- else if (parent->right == temp) { parent->right = child; delete temp; return; } //temp is it's parent's right child
- else { parent->left = child; delete temp; return; } //temp is it's parent's left child
- }
- //if temp's predecessor isn't it's child but grandchild or grandgrandchild or so on
- Node*grandchild = child->left; //pointer at possible precdecessor's child
- //filling the blank space that appears when we "take out" an node from a tree, that has both subtrees
- if (preparent->right == child) { preparent->right = grandchild; } //if predecessor is it's parent's right child
- else { preparent->left = grandchild; } //if predecessor is it's parent's left child
- //after filling the blank space we can delete the node
- child->left = temp->left; //temp's left child becomes predecessor's left child
- if (temp == root) { root=child; delete temp; return; } //if temp is the root, predecessor becomes the root
- else if (parent->right == temp) { parent->right = child; delete temp; return; } //if temp is it's parent's right child
- else { parent->left = child; delete temp; return; } //if temp is it's parent's left child
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement