Binary Search Tree Deletion | Data Structure | Bangla Tutorial

preview_player
Показать описание
In this video i have discussed about the topic of Binary search tree deletion in data structure.

you may contact via mail/skype/whatsapp:

whatsapp: 01771432336
Рекомендации по теме
Комментарии
Автор

Correction:- Example 2 te right sub tree er minimum value 9.

nafiulislamraj
Автор

Nice bro... I am see your full data structure playlist

rhsiam
Автор

you are the best, make videoes on assembly language

ashrafahmedsizan
Автор

Vai example 1 e 10 delete korte 15 o toh root e nite partam taina?

ItzRZ
Автор

The process of deleting 6 is wrong . This node has only one children. So we can delete it replacing 4 here. Waiting for your reply, sir.

MazidulIslam-no
Автор

Example 2 te right sub tree er minimum value 9.

reduanahmadswe
Автор

Last example solution is wrong. We need minimum and here 10 is the minimum value not 12

md.marufsarker
Автор

/* Deleting a node from Binary search tree */
#include<iostream>
using namespace std;
struct Node {
int data;
struct Node *left;
struct Node *right;
};
//Function to find minimum in a tree.
Node* FindMin(Node* root)
{
while(root->left != NULL) root = root->left;
return root;
}

// Function to search a delete a value from tree.
struct Node* Delete(struct Node *root, int data) {
if(root == NULL) return root;
else if(data < root->data) root->left = Delete(root->left, data);
else if (data > root->data) root->right = Delete(root->right, data);
// Wohoo... I found you, Get ready to be deleted
else {
// Case 1: No child
if(root->left == NULL && root->right == NULL) {
delete root;
root = NULL;
}
//Case 2: One child
else if(root->left == NULL) {
struct Node *temp = root;
root = root->right;
delete temp;
}
else if(root->right == NULL) {
struct Node *temp = root;
root = root->left;
delete temp;
}
// case 3: 2 children
else {
struct Node *temp = FindMin(root->right);
root->data = temp->data;
root->right = Delete(root->right, temp->data);
}
}
return root;
}

//Function to visit nodes in Inorder
void Inorder(Node *root) {
if(root == NULL) return;

Inorder(root->left); //Visit left subtree
printf("%d ", root->data); //Print data
Inorder(root->right); // Visit right subtree
}

// Function to Insert Node in a Binary Search Tree
Node* Insert(Node *root, char data) {
if(root == NULL) {
root = new Node();
root->data = data;
root->left = root->right = NULL;
}
else if(data <= root->data)
root->left = Insert(root->left, data);
else
root->right = Insert(root->right, data);
return root;
}

int main() {
/*Code To Test the logic
Creating an example tree
5
/ \
3 10
/ \ \
1 4 11
*/
Node* root = NULL;
root = Insert(root, 5); root = Insert(root, 10);
root = Insert(root, 3); root = Insert(root, 4);
root = Insert(root, 1); root = Insert(root, 11);

// Deleting node with value 5, change this value to test other cases
root = Delete(root, 5);

//Print Nodes in Inorder
cout<<"Inorder: ";
Inorder(root);
cout<<"\n";
}
why one function use left and other fuction use oppsite
this
{
while(root->left != NULL) root = root->left;
return root;
}
and this->
struct Node *temp = FindMin(root->right);
root->data = temp->data;
root->right = Delete(root->right, temp->data);

RoboX-hk
visit shbcf.ru