hello frends, i m havig a problem with the deletion of node int the tree plz help me to get my code debugged

void delete_node(struct node *temp,int data)
{
        struct node *temp2=NULL,*temp3;
       if(temp->data==data&&temp->left==NULL&&temp->right==NULL)
        {
                temp=NULL;
        }
        else
        {
                while(temp->data!=data)
                {
                printf("\n temp = %d",temp->data);
                temp2=temp;

                if(temp->data<data)
                {
                 temp=temp->right;
                }
                else
                {
                 temp=temp->left;
                }
                if(temp==NULL)
                break;
                }
                if(temp!=NULL)
                {
                if(temp->left==NULL&&temp->right==NULL)
                {
                 if(temp2->data<data)
                   temp2->right=NULL;
                   else
                   temp2->left=NULL;
                }
                else if(temp->left==NULL||temp->right==NULL)
                {
               printf("temp2 = %d",temp2->data);
                 if(temp2->data<data)
                    {
                      if(temp->right==NULL)
                        temp2->right=temp->left;
                    else
                        temp2->right=temp->right;
                    }
                    else
                    {
                    if(temp->right==NULL)
                        temp2->left=temp->left;
                    else
                        temp2->left=temp->right;
                    }
                free(temp);
            }
            else
            {

                //search largest left node
                temp3=temp->left;
                printf("\n temp 3  %d",temp3->data);
                while(temp3->right!=NULL)
                {
                    temp3=temp3->right;
                    printf("\ntemp3  %d",temp3->data);
                    getch();
                }
                //replace data of node to be deleted by largest left node
                temp->data=temp3->data;
                //delete largest left node
                delete_node(temp3,temp3->data);

            }
        }
        }
}

Recommended Answers

All 3 Replies

Care to give us specifics about the problem?

With the explanation you gave, the best answer is "then you did something wrong".

commented: ;) +2

If this is a balanced tree, then you need to rebalance when deleting the node. This is usually done recursively, which I don't think you are doing. Recursive algorithms are often MUCH simpler than trying to do the same thing in-line, though they may be a bit more resource intensive (stack memory and function call overhead mostly). Class exercise? Another approach is to walk the tree from left to right, creating a new tree, skipping the deleted node. If the tree isn't too big, this can be very effective, and possibly better (other than memory usage) than rebalancing the tree in-situ.

rubber how is rebuilding the tree could be any better than just deleting the node?
i thought deleting it in a non balanced tree would cost O(N)
and from a balanced (RB tree for instance) would cost O(logN)
whilst building a new tree is atleast NlogN

or did i missunderstodd the approach you are suggesting?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.