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.
if (p->content > nodtodelete)you should be taking the left path.addelement()works correctly?searchnodereturns nothing in 3 of 4ifs.