I haven't written code in C for long time. Im trying to do a multi-leaf tree.Im trying to convert a C# trie implementation to C in order to run it on GPU with CUDA.But i've stuck on this.Can you help me?
My node implementation as follows:
struct Node2 {
char *Key;
char *ConsAlterKey;
char *MasterKey;
bool VowelDeletion;
char *Data;
char *MasterData;
Node2 *Childs;
int ChildCount;
};
And here is my function that adds nodes to trie:
void AddAsChildren2(Node2 *Trie,int count)
{
//Trie->Childs=new Node2[count];
Trie->Childs=(Node2 *)malloc(sizeof(Node2)*count);
for(int i=0;i<count;i++)
{
Trie->Childs[i].Key= GetKey();
Trie->Childs[i].ConsAlterKey=GetConsAlterKey();
Trie->Childs[i].MasterKey=GetMasterKey();
Trie->Childs[i].VowelDeletion=GetVowelDeletion();
Trie->Childs[i].Data=GetData();
Trie->Childs[i].MasterData=GetMasterData();
Trie->Childs[i].ChildCount=GetChildCount();
if(Trie->Childs[i].ChildCount> 0)
{
AddAsChildren2(&(Trie->Childs[i]),Trie->Childs[i].ChildCount);
}
}
}
which i call it from main function like this:
Node2 NodeNew;
.
.
.
AddAsChildren2(&NodeNew,NodeNew->ChildCount);
TraverseTree2(&NodeNew);
But when i try to traverse tree i get wrong values and sometimes exceptions.(Possibly memory allocation problem of child nodes)
What am i doing wrong here?
Ps:First node has child nodes and I have no problem with assigning values so ignore "getter" functions.I have changed them inorder to simplfy code.My problem is that i lose values after code completes this part.
Thanks for fast response.I read values from a file.
The structure of file looks like this.
< Key ConsAlterKey MasterKey VowelDeletion Data MasterData ChildCount or >
If a line/item has no child nodes it ends with ">" else it ends with ChildCount value.And here "-" character presents NULL value.
< root - - - - - 2
< a - - False - - 4
< aö - - False 184 - >
< dfı - - False 188 - >
< et ed - False 189 - 3
< aö - - False 184 - >
< dfı - - False 188 - >
< k ğ - False 191 - >
>
< k ğ - False 191 - >
>
< a - - False - - 4
< aö - - False 184 - >
< dfı - - False 188 - >
< et ed - False 189 - 3
< aö - - False 184 - >
< dfı - - False 188 - >
< k ğ - False 191 - >
>
< k ğ - False 191 - >
>
>
And not simplified version of my code as follows:
void AddAsChildren2(Node2 *Trie,FILE *fp,int count)
{
char string[50];
char *line=NULL;
char *Temp;
Trie->Childs=(Node2 *)malloc(sizeof(Node2)*count);
//Trie->Childs=new Node2[count];
for(int i=0;i<count;i++)
{
if(fgets(string,50,fp))
{
line=strtok(string," ");
if(strcmp (line,"<")==0)
{
line=strtok( NULL, " ");
Trie->Childs[i].Key= (strcmp(line,"-")==0?"":line);
line=strtok( NULL, " ");
Trie->Childs[i].ConsAlterKey=(strcmp(line,"-")==0?"":line);
line=strtok( NULL, " ");
Trie->Childs[i].MasterKey=(strcmp(line,"-")==0?"":line);
line=strtok( NULL, " ");
Trie->Childs[i].VowelDeletion=(strcmp(line,"-")==0?"":line);
line=strtok( NULL, " ");
Trie->Childs[i].Data=(strcmp(line,"-")==0?"":line);
line=strtok( NULL, " ");
Trie->Childs[i].MasterData=(strcmp(line,"-")==0?"":line);
Temp = strtok( NULL, " ");
if(strcmp(Temp,">")==0)
{
//ends with >
Trie->Childs[i].ChildCount=0;
}
else if((strcmp(Temp,"\n")!=0)&&(strlen(Temp)> 0))
{
//ends with childcount value so it have childs
Trie->Childs[i].ChildCount=atoi(Temp);
AddAsChildren2(&(Trie->Childs[i]),fp,Trie->Childs[i].ChildCount);
}
}
}
}
}
Traverse function as follows:
void traversetree2(Node2 *tree)
{
printf("Key %s\n",tree->Key);
printf("ConsAlterKey %s\n",tree->ConsAlterKey);
printf("MasterKey %s\n",tree->MasterKey);
printf("Data %s\n",tree->Data);
printf("MasterData %s\n",tree->MasterData);
if(tree->ChildCount>0)
{
for(int i=0;i<tree->ChildCount;i++)
{
traversetree2(&(tree->Childs[i]));
}
}
}
And output is:
Key root
ConsAlterKey
MasterKey
Data
MasterData
Key ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
ConsAlterKey
MasterKey
Data
MasterData
Key ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠░÷E
ConsAlterKey
MasterKey
Data ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠░÷E
MasterData
Key ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠░÷E
ConsAlterKey
MasterKey
Data ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠░÷E
MasterData
Key ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠░÷E
ConsAlterKey ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠░÷E
MasterKey
Data ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠░÷E
MasterData
Key ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠§E
ConsAlterKey
MasterKey
Data ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠§E
MasterData
Key ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠§E
ConsAlterKey
MasterKey
Data ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠§E
MasterData
Key ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠§E
ConsAlterKey ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠§E
MasterKey
Data ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠§E
MasterData