Hi, this is my program
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
typedef struct record
{
char key[30];
int count;
}DATA;
typedef DATA* DATAPTR;
typedef struct tree
{
DATA *word;
struct tree *left;
struct tree *right;
}TREENODE, *TREE;
TREE insert_node(TREE , TREE, DATA *);
TREE root;
TREE current;
int main(int argc, char *argv[])
{
DATAPTR new_data;
DATAPTR root_data;
FILE * input;
input = fopen("bst.txt", "r");
new_data = (DATAPTR)malloc(sizeof(DATA));
while ((fscanf(input, "%s", new_data->key))!=EOF)
{
insert_node(root, current, new_data);
}
fclose(input);
getch();
return 0;
}
TREE insert_node(TREE root, TREE current, DATAPTR ins_data)
{
TREE temp;
if(!current)
{
current = (TREE)malloc(sizeof(TREENODE));
if (current)
{
current->word = ins_data;
current->left = NULL;
current->right = NULL;
}
}
if(!root) //empty tree
{
return current;
}
else
{
temp = root;
if ((strcmp(ins_data->key, temp->word->key))<0)
{
if(temp->left == NULL)
{
temp->left = current;
}
else
{
insert_node(temp->left, current, ins_data->key);
}
}
else if((strcmp(current->word, temp -> word->key))<0)
{
if (temp -> right == NULL)
{
temp->right = current;
}
else
{
insert_node(temp->right, current, ins_data->key);
}
}
else root->word->count++;
return root;
}
}
But i don't know why every time when it exits from the insert_node function, the values of both TREE root and TREE current changed back to NULL.
I've been spending the whole day on this problem. Can anyone help me please, thanks a lot