user2250804
user2250804

Reputation:

Binary Search Tree, delete node

I am writing a program that reads in a "student record" if you will and then separates it into 4 binary search trees based on the data. I am attempting to delete a node, but instead of actually removing it, I would just like to set a flag within the struct which basically lets me know that it has been "deleted". This is my code and it is giving me several errors:

 void deleteNode( TreeNodePtr *treePtr, SREC R, unsigned long key)/*ADD HOW*/
 {
    printf("I got into the delete function.\n");

     /*empty*/
    if(*treePtr != NULL)
    {
        if(R.SID<(*treePtr)->SID)/*Not empty*/
        {
            printf("less than\n");
            deleteNode((*treePtr)->leftPtr, R, key);
        }
        else if(R.SID>(*treePtr)->SID)
        {
            printf("more than.\n");
            deleteNode((*treePtr)->rightPtr, R, key);
        }
        else
        {
            treePtr->exists = 1;
            printf("Are we deleted yet?\n");
        }
    }
    else
    {
         fprintf(stderr,"Could not locate student with ID.\n");
    }
 }

The errors are : note: expected ‘struct treeNode **’ but argument is of type ‘struct treeNode *’ error: ‘struct treeNode’ has no member named ‘SID’. I am sure I am just missing some little thing but I don't know what it is. Any ideas?

Upvotes: 0

Views: 822

Answers (2)

Jim Balter
Jim Balter

Reputation: 16406

The errors are : note: expected ‘struct treeNode **’ but argument is of type ‘struct treeNode *’ error: ‘struct treeNode’ has no member named ‘SID’

These errors mean just what they say. The first parameter of deleteNode is of type TreeNodePtr *, aka struct treeNode **, but you're passing (*treePtr)->leftPtr, which is of type TreeNodePtr, aka struct treeNode *. And you reference (*treePtr)->SID but there's no such member of struct treeNode.

You should also be getting an error for

treePtr->exists = 1;

because treePtr points to a pointer to a struct treeNode, but pointers don't have members. And why is your "deleted" flag called "exists"? That's backwards. Either call it deleted, or set it to 0 and initialize it to 1. The former is better.

And what's your key parameter for? You never use it.

Here's something that might work, assuming that TreeNode contains an SREC called R, and a deleted flag:

void markDeleted(TreeNode* pnode, SREC R)
{
    if (pnode == NULL)
    {
        fprintf(stderr,"Could not locate student with ID.\n");
    }
    else if (R.SID < pnode->R.SID)
    {
        /* search left branch */
        markDeleted(pnode->leftPtr, R);
    }
    else if (R.SID > pnode->R.SID)
    {
        /* search right branch */
        markDeleted(pnode->rightPtr, R);
    }
    else
    {
        /* match */
        pnode->deleted = 1;
    }
}

Upvotes: 0

Per Johansson
Per Johansson

Reputation: 6877

Change

deleteNode((*treePtr)->leftPtr, R, key);

into

deleteNode(&(*treePtr)->leftPtr, R, key);

Same for right. It's questionable if you really want the function to take a ** though. a * would work just as well when you're not actually deleting.

For the second error, you most likely have a member in struct treeNode of type SREC. You have to infix that member name, e.g. (*treePtr)->R.SID.

Upvotes: 1

Related Questions