-2
struct node
{ int data;
  struct node* left;
  struct node* right;};
int Getheight(struct node* root)
{
    if(root==NULL)
    {
        return -1;
    }
    if(root->left==NULL && root->right==NULL)
    {
        return 0;
    }
    else if(root->left!=NULL && root->right==NULL)
    {
        return root->left->height+1;
    }
    else if(root->left==NULL && root->right!=NULL)
    {
        return root->right->height+1;
    }
    else if(root->left->height>=root->right->height)
    {
        return root->left->height+1;
    }
    return root->right->height+1;

}

int Getbalance(struct node* x)
{
    return Getheight(x->left)-Getheight(x->right);
}


struct node* FindMin(struct node* root)
{   if(root==NULL)
    {
        return root;
    }
    else if(root->left==NULL)
    {
        return root;
    }
    else 
    {
        return FindMin(root->left);
    }
}
struct node* Delete(struct node* root,int data)
{  
    if(root->data==data)
    {
       
        if(root->left==NULL || root->right==NULL)
        {
            struct node* temp=root->left?root->left:root->right;
            if(temp==NULL)
            {
                temp=root;
                free(temp)
                root=NULL;
            }
            else
            {
                *root=*temp;
                free(temp);

            }
             
        }
        
        else
        {
             struct node* temp=FindMin(root->right);
             root->data=temp->data;
             root->right=Delete(root->right,root->data);

        }
        if(root!=NULL)
        {
            root->height=Getheight(root);
            int bf=Getbalance(root);
            if(bf>1 && data<root->data)
            {
                root=rightrotate(root);
            }
            if(bf<-1 && data>root->data)
            {
                root=leftrotate(root);
            }
            if(bf>1 && data>root->data)
            {
                root->left=leftrotate(root->left);
                root=rightrotate(root);
            }
            if(bf<-1 && data<root->data)
            {
                root->right=rightrotate(root->right);
                root=leftrotate(root);
            }
        }
        return root;
    }
    else if(root==NULL)
    {
        printf("FALSE");
        printf("\n");
        return root;
    }
    else if(data>root->data)
    {
        root->right=Delete(root->right,data);
    }
    else if(data<root->data)
    {
        root->left=Delete(root->left,data);
    }


}

int main()
{   struct node* root=NULL;
    int data;
    char c='j';
    while(c!='e')
    {
        scanf(" %c",&c);
        if(c=='i')
        {   
            scanf(" %d",&data);
            root=Insert(root,data);
        }
        else if(c=='p')
        {
            preorder(root);
            printf("\n");
        }
        else if(c=='s')
        {
            scanf(" %d",&data);
            Search(root,data);
        }
        else if(c=='b')
        {
            scanf(" %d",&data);
            Balance(root,data);
        }
         else if(c=='d')
        {
            scanf(" %d",&data);
            printf("%d",data);
            printf("\n");
            root=Delete(root,data);
        }

    }
}

The input/output I get are

i 4 //i is insert operation
i 6
i 3
i 2
i 1
s 2 //s is search returns true and false
TRUE//output for above search
p //preorder traversal:3 () ()=>node with data 3 has left child NULL and right child 
NULL
( 4 ( 2 ( 1 ( ) ( ) ) ( 3 ( ) ( ) ) ) ( 6 ( ) ( ) ) ) 
b 4 //b returns balanceheight of the node containing data=4
1 //output to b
d 3 // delete
3
p
( ) 

When i debug the program with these inputs,it always stops in delete(d 3) function with the unknown stopping event occurred at free function.Can anyone help me understand what the problem is??The function for search,insert,balance works perfectly.I am unable to post reat of the code in the question.The problem occurs with the delete function.

  • 1
    Your code is missing essential parts (includes, definitin of `struct node`, and possibly more). Please show a [mcve]. – Jabberwocky Feb 15 '22 at 08:15
  • "always stop s in delete" - what does this mean? – littleadv Feb 15 '22 at 08:16
  • struct node contain int data;struct node* left(left child),struct node* right.The rest of the functions like insert,search and createnewnode works as it normally should in a BST.Whenever i try to delete and then print,it shows empty bracket which implies that i am deleting entire BST(this is what i meant by always stops in delete).That is while debugging it the function stops at free().This is my first question in stackoverflow and i am not used to writing the question,so please do tell me if i have missed any important aspects in my question. – vishnuvinodh nair Feb 15 '22 at 09:07
  • 1
    @vishnuvinodhnair don't post code in comments. As you can see it's unreadable. Instead [edit] your question. Take the [tour] and read this: [ask]. Don't describe your code. Show it. Read why we need a [mcve] – Jabberwocky Feb 15 '22 at 09:19

1 Answers1

0

Please set the pointer variable to NULL after free(). The free() function only releases the memory pointed by the pointer variable and does not set it to NULL.

These coding habits will make your software safer and prevent unintended behavior.

linuxias
  • 306
  • 2
  • 10
  • Sorry,that was a mistake on my part,but even after i changed the code,it still doesnt work. I did replace it with temp=root;free(root);root=NULL and in else function *root=*temp;free(temp) but when i use delete it is erasing entire BST. – vishnuvinodh nair Feb 15 '22 at 08:57