-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChange of Key in BST
60 lines (60 loc) · 1.45 KB
/
Change of Key in BST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Structure of the binary Search Tree is as
struct node
{
int key;
struct node *left, *right;
};
*/
// your task is to complete the Function
// Function is used to change a key value in the given BST
// Note: Function should return root node to the new modified BST
node* insertKey(node *root,int newVal){
if(root==NULL){
node *t=new node(newVal);
return t;
}
if(newVal<root->key)
root->left=insertKey(root->left,newVal);
else
root->right=insertKey(root->right,newVal);
return root;
}
node* minValueNode(node *root){
node* curr=root;
while(curr->left!=NULL){
curr=curr->left;
}
return curr;
}
node* deleteKey(node *root,int oldVal){
if(root==NULL)
return NULL;
if(oldVal<root->key){
root->left=deleteKey(root->left,oldVal);
}
else if(oldVal>root->key){
root->right=deleteKey(root->right,oldVal);
}else{
if(root->left==NULL){
node *temp=root->right;
free(root);
return temp;
}else if(root->right==NULL){
node *temp=root->left;
free(root);
return temp;
}
node* temp=minValueNode(root->right);
root->key=temp->key;
root->right=deleteKey(root->right,temp->key);
}
return root;
}
struct node *changeKey(struct node *root, int oldVal, int newVal)
{
// Code here
root=deleteKey(root,oldVal);
root=insertKey(root,newVal);
return root;
}