问题
I've been busy trying to code a trie ordered tree data structure in C. My program reads in words in a sentence one at a time from a .txt, then it stores each word in a trie without duplicates. It then grabs all other words in that sentence and stores them in a subtrie of the word that was stored. For example if we had the following sentence: "contribute to open source. " My code does the following...
root
ab'c'defghijklmn'o'pqr's''t'uvwxyz
'o' 'p' 'o''o'-subtrie-> "contribute", "open", "source"
'n' 'e' 'u'
't' 'n' 'r'
'r' 'c'-subtrie->"contribute", "to", "open",
'i'
'b'
'u'
't'
'e'-subtrie-> "to", "open", "source"
I have successfully been able to insert words into the trie and also the subtries. I have thoroughly tested this so I am pretty confident everything works the way it was intended to. However, i cant seem to figure out the algorithem to print the trie and subtrie alphabetically.
Here's the struct i am using
typedef struct TrieNode
{
// number of times this string occurs in the corpus
int count;
// 26 TrieNode pointers, one for each letter of the alphabet
struct TrieNode *children[26];
// the co-occurrence subtrie for this string
struct TrieNode *subtrie;
} TrieNode;
here is the function i wrote to insert tries. The parameters are the root of the trie, a char array of the word i want to insert, size of the word i am inserting, z = -1 initially.
TrieNode *trieInsert(TrieNode *root, char *wordArray, int sizeOfWord, int z){
z++;
int x1, j, index;
char c1 = wordArray[z];
//INSERT char second level
// do alphaNum conversions and check uper or lower case for lev1Char
x1 = char2Num(c1);
if(x1 >26 ){
printf("ERRRRRRRRRRRRRRRRRRrr:line475");
return root;
}
//base case
if( sizeOfWord == z )
return root;
//check to see if we already inserted this
if( root->children[x1] == NULL ){
//we insert second letter
root->children[x1] = malloc(sizeof(struct TrieNode) );
root->children[x1]->subtrie = NULL;
root->children[x1]->count = 0;
//set children of newly malloced to null
for(j = 0; j < 27; j++)
root->children[x1]->children[j] = NULL;
}
//increment word count on last char of word
if((sizeOfWord - 1) == z)
root->children[x1]->count++;
return trieInsert(root->children[x1], wordArray, sizeOfWord, z);
}
Here is the code i cant figure out. It was to print the trie alphabetically, however, it's output is incorrect.
void printTrieAlefBet( TrieNode *root ){
int i;
if( root->subtrie != NULL){
printf(" (%d)", root->count);
return;
}
for( i = 0; i < 27; i++)
if( root->children[i] != NULL){
printTrieAlefBet(root->children[i]);
printf("%c", num2Char(i, 0) );
}
}
Any thoughts would be greatly appreciated!
回答1:
Got it to work. Here's the code.
Special thanks to prof Sean Szumlanski!
// Helper function called by `printTrie()`.
void printTrieHelper(TrieNode *root, char *buffer, int k)
{
int i;
if (root == NULL)
return;
if (root->count > 0)
printf("%s (%d)\n", buffer, root->count);
buffer[k + 1] = '\0';
for (i = 0; i < 26; i++)
{
buffer[k] = 'a' + (i - 1);
printTrieHelper(root->children[i], buffer, k + 1);
}
buffer[k] = '\0';
}
// If printing a subtrie, the second parameter should be 1; otherwise, 0.
void printTrie(TrieNode *root, int useSubtrieFormatting)
{
char buffer[1026];
if (useSubtrieFormatting)
{
strcpy(buffer, "- ");
printTrieHelper(root, buffer, 2);
}
else
{
strcpy(buffer, 'and');
printTrieHelper(root, buffer, 0);
}
}
来源:https://stackoverflow.com/questions/17843628/algorithm-to-print-trie-alphabetically