Hi.
I do not why the leftchild and rightchild of the root are null.
why?
#include <stdlib.h>
#include <stdio.h>
struct node_b
{
int key;
struct node_b *leftchild;
struct node_b *rightchild;
Quote:
};
typedef struct node_b node;
node *create(int data)
{
node *root;
root=malloc(sizeof(node));
root->key=data;
root->leftchild=NULL;
root->rightchild=NULL;
return root;
Quote:
}
void insert(int data, node *b_node)
{
if(b_node==NULL)
{
b_node=malloc(sizeof(node));
b_node->key=data;
b_node->leftchild=NULL;
b_node->rightchild=NULL;
}
else
{
if(data<b_node->key)
{
insert(data, b_node->leftchild);
}
else
{
insert(data, b_node->rightchild);
}
}
Quote:
}
void print_node(int low, int high, node *b_node)
{
if(b_node!=NULL)
{
print_node(low, high, b_node->leftchild);
print_node(low, high, b_node->rightchild);
print_node(low, high, b_node);
if(b_node->key<=high&&b_node->key>=low)
{
printf(" %d", b_node->key);
}
}
Quote:
}
void freetree(node *b_node)
{
if(b_node!=NULL)
{
freetree(b_node->leftchild);
freetree(b_node->rightchild);
free(b_node);
}
Quote:
}
int main(int argc, char *argv[])
{
int i=0;
int indata;
int numOperand=0;
node *root_node;
int low_key=0;
int up_key=0;
FILE *infile;
char DataIn[6];
strcpy(DataIn, argv[1]);
infile=fopen(DataIn, "r");
if(infile==NULL)
{
printf("Cannot open file!\n");
exit(1);
}
fscanf(infile, "%d\n", &numOperand);
for(i=1;i<=numOperand;i++)
{
fscanf(infile, "%d", &indata);
if(i==1)
{
root_node=create(indata);
}
else
{
insert(indata, root_node);
}
}
fscanf(infile, "\n%d", &low_key);
fscanf(infile, "%d", &up_key);
printf("The nodes within %d ", low_key);
printf("to %d ", up_key);
printf("are \n");
print_node(low_key, up_key, root_node);
fclose(infile);
freetree(root_node);
return 0;
Quote:
}