0

so i coded a binary search tree in C which looks with this struct:

struct tnode {
    int content;
    struct tnode *left; /* left tree part */
    struct tnode *right; /* right tree part */
};

My main method:

int main() {

    struct tnode *Baum = NULL;
    struct tnode *tmpPos = NULL;
    Baum = addelement (Baum, 32);
    Baum = addelement(Baum, 50);
    Baum = addelement(Baum, 60);
    tmpPos = searchnode(Baum,50);
}

So basicly this creates me a Binary search tree with 3 elements (32,50,60). My searchnode method is supposed to move a pointer to the "50" so i can delete it afterwards. However my searchnode Method only returns the pointer if the element im searching is the root of my binary search tree.

searchnode:

struct tnode *searchnode(struct tnode *p, int nodtodelete) {
    if (p == NULL) {
        printf("Baum ist leer oder Element nicht vorhanden \n");
    }

    if ( p -> content == nodtodelete) {
        return p;
    }

    if (p->content > nodtodelete) {
        searchnode (p->right, p->content);
    }
    if (p->content < nodtodelete) {
        searchnode(p->left, p->content);
    }
}

Maybe you guys can help me.

6
  • 2
    Don't you have the left/right subtree decisions reversed? if (p->content > nodtodelete) you should be taking the left path. Commented Jul 14, 2016 at 18:35
  • Welcome to the site! Check out the tour for more about asking questions that will attract quality answers. Have you already confirmed that addelement() works correctly? Commented Jul 14, 2016 at 18:35
  • searchnode returns nothing in 3 of 4 ifs. Commented Jul 14, 2016 at 18:37
  • addelement works correctly for sure Commented Jul 14, 2016 at 18:38
  • 1
    Please don't change the question after answers has been posted. Ask a new question if you have one. stackoverflow.com/questions/ask Commented Jul 14, 2016 at 19:47

2 Answers 2

2

Your function has undefined behavior since it doesn't have any return statements in the recursive calls.

Also, the recursive calls need to be fixed to use the right input.

struct tnode *searchnode(struct tnode *p, int nodtodelete)
{
   if (p == NULL)
   {
      printf("Baum ist leer oder Element nicht vorhanden \n");
      return NULL;
   }

   if ( p -> content == nodtodelete)
   {
      return p;
   }

   // This is copied from OP's question but it seems suspect.
   // It should probably be:
   // if (p->content < nodtodelete)
   if (p->content > nodtodelete)
   {
      return searchnode (p->right, nodtodelete);
   }

   return searchnode(p->left, nodtodelete);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Posted an aswer with another question related to the Binary Search tree, maybe you can spot the mistake here aswell.
@RobinSchmidt, please post another question instead of asking another question in an answer.
@RSahu -Is left/right decision reversed? -uh you commented that!
@WeatherVane, Yes, I think it is reversed.
2

You should pass the value you're searching for, not the value of the node:

searchnode (p->right, nodtodelete);
                      ^

Make the same change to the other recursive call.

searchnode(p->left, nodtodelete);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.