I have problem in connecting nodes for my tree. In my program the function read() will be executed first then the load() function. In read() function all data from my external file will be stored in array then rebuild in load() function. Variables dat and i was declared globally. My problem is on the load() function. your help is really appreciated.By the way my data on the external file was arranged pre-orderly and I used “#” for NULL nodes.
void read()
{
string dat1;
fstream file;
file.open("data.txt", ios::in);
do
{
getline(file,dat1);
dat[i]=dat1;
i++;
}while(!file.eof());
file.close();
}
void load(node *root,int index)
{
node *nNode;
nNode=(node*)malloc(sizeof(node));
nNode->yes=NULL;
nNode->no=NULL;
nNode->data=dat[index];
if(index<i)
{
if(nNode->data!="#")
{
root=nNode;
load(root->yes,index+1);
load(root->no,index+1);
}
else
{
root=NULL;
return;
}
}
}
Well, one problem is that you’ve never actually increased
indexinload(). Another problem is that you never assign root->yes or root->no to anything, either. Instead, you assign the pass-by-value parameterroot. Perhaps you wanted this to be by reference as well.You likely want something more like: