I tried binary tree data structure but found it to be not working and giving an error. Please correct my code. Thanks!
It gives warning but with the inputs in main
it stops running .
#include<stdlib.h>
#include<stdio.h>
typedef struct
{
int item;
struct node * leftc;
struct node * rightc;
}node;
void create(int key, node **tree )
{
if(*tree ==0)
{
(*tree)= (node *)malloc(sizeof(node *));
(*tree)->item=key;
(*tree)->leftc=((*tree)->rightc)=NULL;
}
else
{
if(key >= (*tree)->item )
{
create(key, &((*tree)->rightc));
}
else if(key<(*tree)->item)
{
create(key, &((*tree)->leftc));
}
}
}
node * search(int key, node * tree)
{
if(tree !=NULL)
{
if(key == tree->item)
return tree;
else if(key > tree->item)
search(key, tree->rightc);
else
search(key, tree->leftc);
}
return NULL;
}
void cut(node * tree)
{
if(tree != NULL)
{
cut(tree->leftc);
cut(tree->rightc);
free(tree);
}
}
void print_preorder(node * tree)
{
if (tree) {
printf("%d\n",tree->item);
print_preorder(tree->leftc);
print_preorder(tree->rightc);
}
}
int main()
{
node * root=NULL;
create(9,&root);
create(16,&root);
create(24,&root);
create(6,&root);
return 0;
}