Skip to main content
8-Gravel
February 22, 2023
Question

Binary Search Tree

  • February 22, 2023
  • 1 reply
  • 1116 views

Thus I wrote a binary search tree in C that uses this struct:

 

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

 

My primary 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, in essence, this generates a Binary search tree with three components (32,50,60). According to this post, my searchnode function is intended to move a reference to the "50" so that I may delete it afterwards. My searchnode Method, on the other hand, only provides the pointer if the element I'm looking for 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);
 }
}

 

 

Perhaps you can assist me.

 

1 reply

16-Pearl
February 28, 2023

This seems to be a C coding question. Is above code related with any Thingworx functionality?