Thread: BST Removal causes program to exit

  1. #1
    Registered User StevenGarcia's Avatar
    Join Date
    Nov 2006
    Posts
    22

    BST Removal causes program to exit

    Hello. I am having some trouble with removing a node from my bst tree. The program simply exits if I try to remove more than one node from the tree, or when I try to remove a node and then display the contents of the tree. All the other tree functions seem to work without problems. I've been going through all the tutorials I've been able to find online, and while it has greatly helped me see/implement better ways to code some of the tree functions, I'm still getting the same problems. Perhaps there is something I have overlooked? Thanks for any suggestions.



    Code:
    void tree::w_remove(int data)
    {
    	
    	remove(data, root);
    }
    
    //============================================
    
    void tree::remove(int data, node * leaf)
    {
    	search(data, leaf);
    
    	if(!leaf)
    	{
    		return;
    	}
    	else
    	{
    		deletenode(leaf);
    	}
    }
    
    //=============================================
    
    node * tree::search(int data, node * leaf)
    {
    	while(leaf)
    	{
    		if(data == leaf->data)
    		{
    			return leaf;
    		}
    		else if(data < leaf->data)
    		{
    			return search(data, leaf->left);
    		}
    		else
    		{
    			return search(data, leaf->right);
    	       	}
    	}
    
    	return leaf;
    }
    
    //==============================================
    
    void tree::deletenode(node * leaf)
    {
    	//starting from scratch
    }
    Last edited by StevenGarcia; 12-11-2006 at 11:27 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    try to run the program with gdb, it can help you track down the point of crash.

    clearly, there is something wrong with the memory management here, thus, if you don't want to use gdb or some other debugging utility, you have to think very thoroughly about this

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    there are A LOT of problems in the program

    search(data, leaf); returns pointer to the node to be removed, this value is ignored

    while(leaf) can be easely replaced by if(leaf) in this function

    deletenode has even more problems in it...

    you should draw the tree on the paper and see what do you do with it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User StevenGarcia's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Quote Originally Posted by vart
    search(data, leaf); returns pointer to the node to be removed, this value is ignored
    It's ignored? I thought I was passing the pointer to deletenode()? Am I way off base in thinking this?

    Thanks for the feedback guys. I'm trying to use the debugger now. I think I see what's going wrong in my deletenode() function.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    pointer (even if it would be changed in the search funtion) is not changed in the calling function because in the C arguments are passed by value...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User StevenGarcia's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Doh! Unfortunately for me windows crashed shortly after my last post so I get to start from scratch on a clean install. Thanks for the feedback.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM