Added code for delete operation on BST (#19516)

This commit is contained in:
Aditi Aggarwal
2018-10-16 20:32:16 +05:30
committed by Quincy Larson
parent 606eef21d8
commit 829c80cdb0

View File

@ -154,6 +154,42 @@ void insert(int data) {
}
}
```
#### Delete Operation
void deleteNode(struct node* root, int data){
if (root == NULL) root=tempnode;
if (data < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
return root;
}
Binary search trees (BSTs) also give us quick access to predecessors and successors.
Predecessors can be described as the node that would come right before the node you are currently at.
@ -244,4 +280,4 @@ Complete Binary Tree: A Binary Tree is complete Binary Tree if all levels are co
/ \ / \
40 50 100 40
/ \ /
8 7 9
8 7 9