我一直在忙着尝试在C语言中编写一个有序的树数据结构。我的程序从.txt一次一个地读出一个句子中的单词,然后它将每个单词存储在一个没有重复的trie中。然后它抓取该句子中的所有其他单词并将它们存储在存储的单词的子集中。例如,如果我们有以下句子:“贡献开源。”我的代码执行以下操作......
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"
我已经成功地将单词插入到trie和子句中。我已经对此进行了彻底的测试,因此我非常有信心一切都按照预期的方式进行。但是,我似乎无法弄清楚algorithem按字母顺序打印trie和subtrie。
这是我正在使用的结构
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;
这是我写的插入尝试的函数。参数是trie的根,我要插入的单词的char数组,我插入的单词的大小,最初z = -1。
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);
}
这是我无法弄清楚的代码。这是按字母顺序打印trie,然而,它的输出是不正确的。
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) );
}
}
任何想法都将不胜感激!
答案 0 :(得分:3)
让它发挥作用。这是代码。
特别感谢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);
}
}