#i
nclude
using namespace std;
typedef struct node
{
int data;
struct node *left;
struct node *right;
} fk;
fk *createnode(int data)
{
fk *node = new fk;
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
void preorder(fk *root)
{
if (root != NULL)
{
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
void inorder(fk *root)
{
if (root != NULL)
{
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
void postorder(fk *root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
}
fk *search(fk *root, int data)
{
if (root == NULL || data == root->data)
{
return root;
}
else if (data < root->data)
{
return search(root->left, data);
}
else
{
return search(root->right, data);
}
}
fk *insert(fk *root, int data)
{
if (root == NULL)
{
root = createnode(data);
return root;
}
if (data < root->data)
{
root->left = insert(root->left, data);
}
else if (data > root->data)
{
root->right = insert(root->right, data);
}
return root;
}
fk *findmin(fk *root){
fk *current=root;
while(current!=NULL && current->left!=NULL){
current=current->left;
}
return current;
}
fk *delnode(fk * root, int data){
if(root==NULL){
return root;
}
if(data<root->data){
root->left=delnode(root->left,data);
}
else if(data>root->data){
root->right=delnode(root->right,data);
}
else{
if(root->left==NULL){
fk * temp=root->right;
delete(root);
return temp;
}
else if(root->right==NULL){
fk * temp=root->left;
delete(root);
return temp;
}
fk * temp=findmin(root->right);
root->data=temp->data;
root->right=delnode(root->right, temp->data);
}
return root;
}
int main()
{
// allocate
fk *n1, *n2, *root = createnode(9);
// node creation
n1 = createnode(1);
n2 = createnode(3);
root->left = n1;
root->right = n2;
// traversal
inorder(root);
cout << "n";
// search operation
fk *result = search(root, 7);
if (result == NULL)
{
cout << "it isn't there";
}
else
{
cout << "its there";
}
// insert function
root = insert(root, 2);
cout << "n";
inorder(root);
cout << "n";
// delete function
root=delnode(root,3);
inorder(root);
cout << "n";
// deallocate
delete root;
return 0;
}````
deletion isn’t working as expected i can’t delet the nodes, , help me as i am stuck
deletion isn’t working as expected i can’t delet the nodes, , help me as i am stuck
deletion isn’t working as expected i can’t delet the nodes, , help me as i am stuck v
deletion isn’t working as expected i can’t delet the nodes, , help me as i am stuck
deletion isn’t working as expected i can’t delet the nodes, , help me as i am stuck
deletion isn’t working as expected i can’t delet the nodes, , help me as i am stuck
user24702060 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.