I have tried to create a binary tree structure for storing data, but I can't seem to get the program to compile. It flags up an error when returning a from the function createtree and when assigning this result to a in the main program.
They are both data type tree which is created at the start, and both are pointers. I cannot understand why the program cannot return/assign these two values.
It is probably something very simple but I would be very grateful for the help.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tree{
char thisnode[50];
struct tree *leftnode;
struct tree *rightnode;
}Tree;
Tree *makenode(char *in, Tree *l, Tree *r){
Tree *t = malloc(sizeof(Tree));
t->leftnode = l;
t->rightnode = r;
strncpy(t->thisnode, in, 9);
return t;
}
Tree *insertnode(Tree *root, char *data){
if (root == NULL) {
root = makenode(data, NULL, NULL);}
else if(strcmp(data, root->thisnode)<0){
root->leftnode = insertnode(root->leftnode, data);}
else{
root->rightnode = insertnode(root->rightnode, data);}
return root;
}
char *search(Tree *root, char *number){
if (root == NULL){
return "Not Found";}
else if(strcmp(number, root->thisnode) == 0){
return root->thisnode;}
else if(strcmp(number, root->thisnode) < 0){
return search(root->leftnode, number);}
else {
return search(root->rightnode, number);
}
}
Tree createtree(){
char i[25];
int j;
Tree *a = NULL;
while (i[0] != '.'){
for(j = 0; j < 25; j++){
i[j] = getchar();
a = insertnode(a,i);
}
}
return a;
}
int main()
{
Tree *a = NULL;
a = createtree();
if(a != 0) {
printf("\n");
while(a->leftnode != 0) {
printf("%s", a->thisnode);
a = a->leftnode;
}
printf("%s", a->thisnode);
}
return 0;
}