Thread: Can anyone spot the error in my code

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    54

    Can anyone spot the error in my code

    quick note first: the code seams to work fine and for all purposes does what its meant to, but when i list the node contents after removing the node i get some garbage, i've looked at the list code and its definately not that

    heres the code -

    /////////////////////////////////////////////////
    //Function that removes the selected information
    //from the tree
    //

    struct PTRnode* removeFromList(PTRnode* root, datatype &data)
    {
    PTRnode *tempnode; //temporary node to hold root information
    PTRnode *TheSuccessor; //a pointer to the next node after the selected node

    if(strcmpi(KEY, PRIM_KEY)<0)//check if key is less than prim_key
    {

    if(root->left!=NULL) //check if the left pointer is not equal to NULL
    {
    removeFromList(root->left); //call function and go left
    }

    }
    else
    {
    if(strcmpi(KEY, PRIM_KEY)==0)//check if the two keys match
    {
    if(root->left==NULL) //check if the left pointer is equal to NULL
    {
    if(root->right==NULL) //check if the right pointer is equal to NULL
    {
    //node is an empty leaf
    free(root); //free the memory used by root
    root=NULL; //assign a value of NULL to root
    }
    else
    {
    //only a right pointer exists
    tempnode = root; //store root contents into tempnode
    root = root->right; //move contents of root->right into root
    free(tempnode); //free memory of tempnode
    }
    }
    else
    {
    if(root->right==NULL) //check if the right pointer is equal to NULL
    {
    //only a left pointer exists
    tempnode = root; //store root contents into tempnode
    root=root->left; //move contents of root->left into root
    free(tempnode); //free memory of tempnode
    }
    else
    {
    //both left and right pointers are not equal to NULL
    //call to inordertraversal function that will check the next
    //node and store the contents into TheSuccessor
    TheSuccessor = inOrderTraversal(root->right, data);
    root->data = TheSuccessor->data; //store the information in thesuccessor
    //into root
    removeFromList(root->right, TheSuccessor->data); //call function and remove
    //the successor of the node
    }
    }
    }
    else
    {
    if(strcmpi(KEY, PRIM_KEY)>0)//check if key is bigger than prim_key
    {
    if(root->right!=NULL) //check if the right pointer is not equal to NULL
    {
    removeFromList(root->right, data); //call function and go right
    }
    }
    }
    }
    return root;
    }

    //End removeFromList function
    /////////////////////////////////////////////////

    //KEY is defined earlier as data.artist
    //PRIM_KEY is defined as root->data.artist




    /////////////////////////////////////////////////
    //Function that checks the items in the tree
    //in an in order traversal
    //

    struct PTRnode* inOrderTraversal(PTRnode* root, datatype &data)
    {
    if(root!=NULL) //check if root->left is not equal to NULL
    {
    root->left = inOrderTraversal(root->left, data); //call function and go left

    root->right = inOrderTraversal(root->right, data); //call function and go right
    }
    return root;
    }

    //End inOrderTraversal function
    /////////////////////////////////////////////////

    //before removal of CD - pink floyd
    ARTIST TITLE
    ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~

    boney m babylon
    frankie goes to hollywood welcome to the pleasuredome
    klf the white room
    madness complete
    madonna music
    pink floyd wish you were here
    queen greatest hits


    //after removal of CD
    ARTIST TITLE
    ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~

    boney m babylon
    frankie goes to hollywood welcome to the pleasuredome
    klf the white room
    madness complete
    madonna music
    queen greatest hits
    |¡@ greatest hits //whats this?????



    any help will be much appreciated

    thanx

  2. #2
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    Is this homework?

    If so, solve by your own knowledge, then you can learn.
    Yoshi

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I would think this would become painfully obvious if you tested this with a tree containing just two nodes, and you stepped through the code using the debugger.

    Seems obvious enough just reading the code....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM