Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool remove(node* temp, int val) {
- if (temp == NULL) return false;
- node* parent = NULL;
- while (temp->data != val) {
- if (temp == NULL) return false;
- if (val = temp->data) continue;
- parent = temp;
- if (val > temp->data) temp = temp->right;
- if (val < temp->data) temp = temp->left;
- }
- //In case it has no child
- if ((temp->right == NULL) && (temp->left == NULL)) delete temp;
- //Only left child is present
- if ((temp->right == NULL)) {
- if (parent == NULL) head = temp->left;
- else {
- if (parent->data > temp->left->data) parent->left = temp->left;
- else parent->right = temp->left;
- }
- delete temp;
- }
- //Only right child is present
- if ((temp->left == NULL)) {
- if (parent == NULL) head = temp->right;
- else {
- if (parent->data > temp->right->data) parent->left = temp->right;
- else parent->right = temp->right;
- }
- delete temp;
- }
- //In case it has two children
- if ((temp->right != NULL) && (temp->left != NULL)) {
- node* inorder_successor = getInOrderSuccessor(temp->right);
- temp->data = inorder_successor->data;
- remove(head,inorder_successor->data);
- }
- no_of_elements--;
- }
- node* getInOrderSuccessor(node* temp) {
- if(temp->left!=NULL)
- return getInOrderSuccessor(temp->left);
- return temp;
- }
- BST *tree = new BST();
- bool temp = tree->remove(tree->head,key);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement