How to implement a multifaceted tree structure in C

For a long time I did not write code in C. I am trying to make a multi-leaved tree. I am trying to convert a C # trie implementation to C in order to run it on a GPU with CUDA. But I'm stuck on this. Can you help me?

My node implementation is 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 the 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 the main function as follows:

 Node2 NodeNew; . . . AddAsChildren2(&NodeNew,NodeNew->ChildCount); TraverseTree2(&NodeNew); 

But when I try to cross the tree, I get the wrong values ​​and sometimes exceptions. (Perhaps the memory allocation problem of the child nodes)

What am I doing wrong here?

Ps: First node has child nodes, and I have no problem assigning values, so ignore the getter functions. I changed them in order to simple code. My problem is that I lose values ​​after the code completes this part.

Thanks for the quick response. I am reading the values ​​from a file.

The file structure is as follows.

<Key ConsAlterKey MasterKey VowelDeletion Data MasterData ChildCount or>

If the line / element does not have child nodes, it ends with ">" else, it ends with the value of ChildCount. And here the "-" character represents a 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 a simplified version of my code:

 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); } } } 

}

}

Trace function:

 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 the conclusion:

 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 
+4
source share
1 answer

Ah, with a lot of code, this is easy: Your problem is that char string[50] is a local variable, and you save a lot of pointers in this local array, which gets lost when AddAsChildren2 returns. Depending on whether you need to free this structure, you could strdup() whole line, and then save the tokens from it or strdup() each individual token (to make it easier to free).

+3
source

All Articles