Thread: BST Deletion, rewrite for "none found"

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    BST Deletion, rewrite for "none found"

    I've got a function here that works perfect for deleting nodes that match a giving string.

    My issue is I can't seem to logically find out where to put a statement nor signify that nothing was found... I need some way of letting an outside function know if something was found or not. as I need to do different things if nothing is found.

    Thanks


    Code:
    struct Chapter *deletion(struct Chapter* p, char word [])
    {
    	// Empty Tree case
    	if ( p == NULL)
    		return p;
    	
    	// p->word BEFORE word
    	else if ( strcmp(p->word,word) > 0 )
    		p->left = deletion(p->left, word);
    	
    	else if ( strcmp(p->word,word) < 0 )
    		p->right = deletion(p->right, word);
    		
    	// found the node to be deleted
    	else if (p->left != NULL && p->right != NULL)
    	{
    	
    		//if: more than one of that word exists, don't delete, just decrese count
    		if(p->count > 0)
    			p->count--;
    			
    		// else: if only one exists, delete it
    		else{	
    			// the node has 2 children
    			// replace the node with word that is lowest in alphabet in right subtree
    			p = smallest(p->right);
    			
    			// now delete old copy of lowest element
    			p->right = deletion(p->right,p->word);
    		}
    	}
    	
    	// node has ONE child OR both are NULL
    	else
    	{
    		//if: more than one of that word exists, don't delete, just decrese count
    		if(p->count > 0)
    			p->count--;
    		
    		// else: if only one exists, delete it
    		else{
    			if(p->left != NULL)
    				p = p->left;
    			else
    				p = p->right;
    		}
    	}
    		
    	return p;
    	
    }
    Last edited by stormlifter; 12-03-2006 at 06:11 PM.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i don't really see any where that u are checking your key string with the node data. something like this

    Code:
    if(strcmp(p->data,key) == 0 )
    depending on that u delete something in the tree

    ssharish2005

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    I have the "greater than" and "less than" statements, the else is "equals", I know for a fact that if it makes it there that a word was found.
    My issue is I'm not sure how to tell an outside function that I found a word.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    if it didn't find anything return null

    Code:
    struct Chapter *temp;
    
    if ((temp = deletion(the_chapter, "word")) == NULL)
        printf("nothing\n");
    else
        the_chapter = temp;
    somthing along those lines maybe

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    this code is mean

    +------+------+------+------+------+------+------+------+
    ^
    p

    if p->word is equal with word, p is assinged p->left or p->right

    this is not remove p

    u must assign
    p->left->rigth = p->rigth;
    p->rigth->left = p->left;

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by jeremy75
    this code is mean

    +------+------+------+------+------+------+------+------+
    ^
    p

    if p->word is equal with word, p is assinged p->left or p->right

    this is not remove p

    u must assign
    p->left->rigth = p->rigth;
    p->rigth->left = p->left;
    My deletion code is working as far as I can tell, it deletes the word and counts down the frequency. I'm just trying to find out how to tell the function before it if I deleted something or not.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by sl4nted
    if it didn't find anything return null

    Code:
    struct Chapter *temp;
    
    if ((temp = deletion(the_chapter, "word")) == NULL)
        printf("nothing\n");
    else
        the_chapter = temp;
    somthing along those lines maybe
    I like where this is headed, I'm going to see if I can implement

    EDIT: it kind of works, problem is, the return p; actually never ends up returning a null as I run a check on the return and for some reason it never does. I'm looking into this.
    Last edited by stormlifter; 12-03-2006 at 08:08 PM.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    don't let it get down to the return p;
    once you know a word wasn't found do a return NULL;

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Figured it out, just one darn line that had me.
    Last edited by stormlifter; 12-03-2006 at 09:49 PM.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you call strcmp(p->word,word) twice with the same arguments...
    The same can be achived calling it once, storing the result value and comparing the result value in your if/else
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST node deletion help!
    By iunah in forum C Programming
    Replies: 4
    Last Post: 02-01-2009, 05:53 AM
  2. Problem with BST deletion
    By Mheso in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 11:23 PM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM