I have this implementation of a binary search tree in C; I have to do 3 routes in it: preorder, inbound and bidder. The problem is that I do not print anything and the code has no errors, I guess what is wrong is my logic, but I can not find the fault:
#include <stdio.h>
#include <stdlib.h>
typedef struct t_tree{
int value;
t_tree* leftChild;
t_tree* rightChild;
} BinarySearchTree;
void insert(BinarySearchTree* root, int x){
if(root == NULL){
root = (BinarySearchTree*) malloc(sizeof(BinarySearchTree));
root->value = x;
root->leftChild = NULL;
root->rightChild = NULL;
} else {
if(root->value == x){
printf("\n\tEse valor ya existe en el arbol");
return;
}
if(x < root->value)
insert(root->leftChild, x);
else
insert(root->rightChild, x);
}
}
BinarySearchTree* search(BinarySearchTree* root, int x){
if(root == NULL){
printf("\n\tEl elemento no existe");
return NULL;
} else {
if(root->value == x){
printf("\n\tEl valor si existe");
return root;
}
if(x < root->value)
search(root->leftChild, x);
else
search(root->rightChild, x);
}
}
void update(BinarySearchTree* root, int x, int newValue){
BinarySearchTree* theChosenOne = search(root, x);
if(theChosenOne != NULL)
theChosenOne->value = newValue;
}
void remove(BinarySearchTree* root, int x){
BinarySearchTree* theChosenOne = search(root, x);
if(theChosenOne != NULL){
if(theChosenOne->leftChild==NULL && theChosenOne->rightChild==NULL)
free(theChosenOne);
else {
BinarySearchTree* explorer;
if(theChosenOne->leftChild){
explorer = theChosenOne->leftChild;
while(explorer->rightChild)
explorer = explorer->rightChild;
theChosenOne->value = explorer->value;
free(explorer);
} else if(theChosenOne->rightChild){
explorer = theChosenOne->rightChild;
while(explorer->leftChild)
explorer = explorer->leftChild;
theChosenOne->value = explorer->value;
free(explorer);
}
}
}
}
void preOrder(BinarySearchTree* root){
if(root == NULL)
return;
printf("%d ", root->value);
preOrder(root->leftChild);
preOrder(root->rightChild);
}
void inOrder(BinarySearchTree* root){
if(root == NULL)
return;
inOrder(root->leftChild);
printf("%d ", root->value);
inOrder(root->rightChild);
}
void postOrder(BinarySearchTree* root){
if(root == NULL)
return;
postOrder(root->leftChild);
postOrder(root->rightChild);
printf("%d ", root->value);
}
int main(){
BinarySearchTree* ROOT = NULL;
insert(ROOT, 5);
insert(ROOT, 1);
insert(ROOT, 9);
insert(ROOT, 1);
insert(ROOT, 8);
insert(ROOT, 50);
printf("\nRecorrido en pre-orden: ");
preOrder(ROOT);
printf("\nRecorrido en in-orden: ");
inOrder(ROOT);
printf("\nRecorrido en post-orden: ");
postOrder(ROOT);
free(ROOT);
return 0;
}