C Code For Deletion in a Binary Search Tree

C Code For Deletion in a Binary Search Tree

CodeWithHarry

3 года назад

155,772 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@kumarjatin8685
@kumarjatin8685 - 14.12.2023 05:30

code provide kr do

Ответить
@sp8976
@sp8976 - 11.12.2023 11:21

where is the source code and notes for this video SIR ?

Ответить
@rahulgarpatti1502
@rahulgarpatti1502 - 29.11.2023 15:33

Theme is worst 😢

Ответить
@-DevashishDhope
@-DevashishDhope - 28.11.2023 20:36

code link????

Ответить
@shivamsemwalscat7214
@shivamsemwalscat7214 - 23.11.2023 16:43

this code fails corner case when there is only one node in bst

Ответить
@himanshuranjan9976
@himanshuranjan9976 - 20.11.2023 13:47

I have a question if we give a value that is not in the bst then this code will just go and delete the leaf node we are not checking if the value is there or not we are assuming it is.

Ответить
@Jeel_Mangukiya
@Jeel_Mangukiya - 20.11.2023 07:25

Are bhai , yeh code download kaha se karna hain????

Ответить
@ritik9740
@ritik9740 - 13.11.2023 17:10

Notes kha pe hae?

Ответить
@literally_ankur
@literally_ankur - 06.11.2023 19:14

Not discussed the case where left child is null


struct node* inorderpred(struct node* root){
root=root->left;

while(root->right!=NULL){
root=root->right; // rightmost not null node of left subtree of the value to be deleted will be the inorder predecessor of the value
}
return root;
}

struct node* inordersucc(struct node* root){
root=root->right;

while(root->right!=NULL){
root=root->left; // leftmost not null node of right subtree of the value to be deleted will be the inorder successor of the value
}
return root;
}

struct node* deleteNode(struct node* root,int value){
struct node* ipre;
struct node* insuc;
// printf("%d ",root->data);
if(root==NULL) {return NULL;}
if(root->left==NULL && root->right==NULL && root->data==value){ // inorder predecessor of the value will always be a leaf node
free(root);
return NULL;
}
// searching for the node value
if(value<root->data) {root->left=deleteNode(root->left,value);}
else if(value>root->data) {root->right=deleteNode(root->right,value);}
else{
// when the value is found
if(root->left==NULL){
insuc=inordersucc(root);
root->data=insuc->data;
root->right=deleteNode(root->right,insuc->data);
}
else{
printf("%d ",root->data);
ipre=inorderpred(root); // finding the inorder predecessor
root->data= ipre->data;
root->left=deleteNode(root->left,ipre->data); // deletion of the predecessor data

}
}
return root;
}


for example

5
/ \
3 6
/ \ \
1 4 7

Ответить
@dhananjay1801
@dhananjay1801 - 12.10.2023 15:54

i thought 3 case k liye 3 alag alag code honge

Ответить
@manjeet_singh_rathore7016
@manjeet_singh_rathore7016 - 09.10.2023 16:20

the case when a middle node is to be deleted and its left pointer is null and right point to another node .This case is not taken in consideration

Ответить
@indrajitmali9917
@indrajitmali9917 - 04.10.2023 08:32

For anyone who needs code :
Code For Deletion as Explained by Harry Bhai ----->

Code :

Node* inOrderPredecessor(struct Node* root){
root = root->left;
while(root->right!=NULL){
root = root->right;
}
return root;
}

struct Node * deleteNode(struct Node* root, int value){
Node* iPre;
if(root==NULL){
return NULL;
}
if(root->left == NULL && root->right==NULL && root->data == value){
free(root);
return NULL;
}

//search for the node to be deleted
if(value<root->data){
root->left = deleteNode(root->left, value);
}else if (value>root->data){
root->right = deleteNode(root->right, value);
}

//Deletion strategy when tree is found
else{
iPre = inOrderPredecessor(root);
root->data = iPre->data;
root->left=deleteNode(root->left, iPre->data);
}
return root;
}

Ответить
@dailyroutine1939
@dailyroutine1939 - 04.10.2023 03:36

we can write 2nd if statement in the deletenode function as " if(head->left == NULL && head->right == NULL && head->data == dat)" where it checks the value matches with target node or not otherwise it can delete a node which is just a leaf node having left and right pointers NULL on providing the value which is not in the tree

Ответить
@pritamdas7780
@pritamdas7780 - 18.09.2023 11:08

source code nahi dia hai apne bhai...aur jo code apke website mein dikha raha hai wo galat hai as per your video wala code !!!😑

Ответить
@princekumar2021
@princekumar2021 - 24.07.2023 10:55

bhaiya code khaa hai

Ответить
@aaryanaik2915
@aaryanaik2915 - 19.07.2023 09:23

if the node to be deleted is found in the "if" conditions then how does it enter the else condition to perform the deletion strategy? please help its confusing me for days now @CodeWithHarry

Ответить
@devanshbhakhri7108
@devanshbhakhri7108 - 10.07.2023 09:14

Hope you get 5M subscribers asap❤❤

Ответить
@surajkumardas4237
@surajkumardas4237 - 01.07.2023 23:03

I have a doubt. Let's assume there's a single node with its left and right both pointed to null and data set to 10. And then if the deleteNode function gets called with value 8 (or any value), then won't the second IF statement inside the deleteNode function delete that single node in the tree even if the value for which deleteNode was called doesn't match with the existing single node we have??

Ответить
@kunalparkar2437
@kunalparkar2437 - 24.06.2023 09:29

I'm facing an issue while solving this I have created one .cpp in which I have written all operations in BST (create a new node, in order, pre, post, and all) and in the same file now when I write this code it is not giving me output but when I run it separately in other .cpp file it runs smoothly. not getting what the issue is if you got it please explain me.

Ответить
@harshtrada9904
@harshtrada9904 - 08.06.2023 15:28

Font ligatures nahi ho raha🥲

Ответить
@ANANDRAJ-xh2nx
@ANANDRAJ-xh2nx - 29.05.2023 13:33

sir if we try to delete those node which is not in the Bst then it is not working what is the best solution

Ответить
@spade_
@spade_ - 14.04.2023 09:18

The tutorial is good i guess the changes are not implemented on website's code.

Ответить
@vHirenOfficial
@vHirenOfficial - 08.04.2023 19:27

This code seems to have 'Address Mismatch Error' as its trying to free the memory which is previously never allocated. Its working for the cases with taking this problem too.

Ответить
@Nikita-ny9fo
@Nikita-ny9fo - 03.04.2023 17:08

Amazing bro👌👌👌👌

Ответить
@mommy8180
@mommy8180 - 30.03.2023 23:42

Where is the code

Ответить
@vijetakumar7347
@vijetakumar7347 - 29.03.2023 00:46

There is some addition in the code to handle cases when the node is not present OR when the left child of root is not present. When left child is not present, we use the in-order successor to fill the created void. Here is the code:

Node* inOrderPredecessor(Node* root){
root = root->left;
while (root->right!=NULL)
{
root = root->right;
}
return root;
}

//Inorder successor is the leftmost element of the right subtree

Node* inOrderSuccessor(Node* root){
root = root->right;
while (root->left!=NULL)
{
root = root->left;
}
return root;
}

Node* deleteNode(Node* root, int value){
Node* i_pre_post;
if (root == NULL){
return NULL;
}
if (root->left==NULL && root->right==NULL && root->data == value){ //Additional check before deleting the leaf Node
free(root);
return NULL;
}

//searching for the node to be deleted
if (value < root->data){
root->left = deleteNode(root->left,value);
}
else if (value > root->data){
root->right =deleteNode(root->right,value);
}
//deletion strategy when the node is found
else{
if(root->left == NULL && root->right != NULL){ //Using inorder Successor when left child is not present
i_pre_post = inOrderSuccessor(root);
root->data = i_pre_post->data;
root->right = deleteNode(root->right, i_pre_post->data);

}
else{
i_pre_post = inOrderPredecessor(root);
root->data = i_pre_post->data;
root->left = deleteNode(root->left, i_pre_post->data);
}
}
return root;
}

//Like if this helped (^_^). Comments are welcome ->

Ответить
@SarveshKumar-mm4lg
@SarveshKumar-mm4lg - 24.03.2023 16:43

great help. thanks harry bhai!. small correction -------- code for inorder precessor is not correct. what if root->left itself is null

Ответить
@itspurelypassionate
@itspurelypassionate - 23.03.2023 21:01

I was confused in this one before watching this video. Thanks for clearing it up

Ответить
@VijayKumar-pz7wb
@VijayKumar-pz7wb - 02.03.2023 08:15

Ab tak ka sabse tough code yhi tha. 😓😓😤😤😤😞😤😭😭😭😦 shi yad ho payega 😤

Ответить
@Ranjeetvishwakarma-72
@Ranjeetvishwakarma-72 - 01.03.2023 04:42

Bhai iss video ki starting ke kuch AAPKE sabd bahut hi adbhut aur rahasmayi the

Ответить
@tanmaygoel3910
@tanmaygoel3910 - 19.02.2023 15:01

where is code link

Ответить
@nicodiangelo5991
@nicodiangelo5991 - 17.02.2023 17:27

where is the source code link

Ответить
@Sans_K5
@Sans_K5 - 27.01.2023 20:29

thanks_Sir❤️🙏

Ответить
@ManishSharma-fi2vr
@ManishSharma-fi2vr - 20.01.2023 18:52

Showing Gratitude by Commenting.

Ответить
@mahandeepparida9789
@mahandeepparida9789 - 18.01.2023 15:25

shows error for deleting 98 in
87
/ \
44 98
/ \ \
12 49 100

Ответить
@rneditz3250
@rneditz3250 - 18.01.2023 06:20

WHy i am not getting the output when i am going to delete 5...it is not deleting 5

Ответить
@arijaftab5864
@arijaftab5864 - 18.01.2023 00:15

this code is slightly wrong.....for example it will not work for a right skewed BST.......

Ответить
@aryanbarnwal5645
@aryanbarnwal5645 - 04.01.2023 19:38

Please make videos on dp and greedy algorithms 🙏🙏

Ответить
@anirudhsingh9720
@anirudhsingh9720 - 09.12.2022 15:46

Harry bhai aapka bhi ye video samjh nhi aaya , concept clear nahi hua

Ответить
@hippityhoppity71
@hippityhoppity71 - 07.12.2022 10:28

Bhai, iska pura source code share kijiye pls

Ответить