problem in deletion in binary search tree

This is a discussion on problem in deletion in binary search tree within the C Programming forums, part of the General Programming Boards category; I wrote a function to delete a node in the binary search tree. The code works well in case of ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    18

    problem in deletion in binary search tree

    I wrote a function to delete a node in the binary search tree. The code works well in case of node having no child but fails when the node has one or two child.
    Code:
    void deleteNode(bstree **rootref,int delnum)
    {
        bstree *temp,*parent,*succ;
        if(search(*rootref,delnum)==0)
        printf("Deletion denied, entered number not in the tree\n");
        temp=*rootref;
        while(temp&&((temp->data)!=delnum))
        {
                parent=temp;
                if(temp->data<delnum) temp=temp->right;
                else temp=temp->left;
        }
        if(!temp) printf("the element not present,deletion denied\n");
        if(!(temp->left)&&!(temp->right))
        {
            if(parent->left==temp) parent->left=NULL;
            else parent->right=NULL;
        }
        else if((temp->left)&&!(temp->right))
        {
            if(parent->left=temp) parent->left=temp->left;
            else parent->right=temp->left;
        }
        else if((temp->right)&&!(temp->left))
           {
               if(parent->left=temp) parent->left=temp->right;
               else parent->right=temp->right;
           }
        else
           {
               succ=temp->right;
               while(succ->left) succ=succ->left;
               temp->data=succ->data;
               temp=succ;
           }
           free(temp);
    
    
     }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > if(parent->left=temp) parent->left=temp->left;
    > if(parent->left=temp) parent->left=temp->right;
    This has been mentioned elsewhere in your other threads.
    You're mixing up = and ==

    Which compiler are you using?

    A better compiler would warn you of such mistakes.
    Code:
    $ gcc -c -Wall foo.c
    foo.c:30:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    foo.c:35:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32
    I wrote a BST program recently which seems to work fine. Its here: Need help with BST Program

    Salem, if you could verify the delete operation in my program.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree problem (too)
    By ariadne in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 07:46 AM
  2. Binary Search Tree problem
    By aim4sky in forum C Programming
    Replies: 30
    Last Post: 04-11-2008, 12:41 AM
  3. Binary Search Tree deletion
    By gflores in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 06:01 PM
  4. Binary Search tree problem
    By berryski in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2005, 12:47 PM
  5. Problem with binary search tree :(
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-01-2002, 10:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21