1

我一直忙于尝试在 C 中编写一个 trie 有序树数据结构。我的程序从 .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 以及 subtries 中。我已经对此进行了彻底的测试,因此我非常有信心一切都按预期的方式工作。但是,我似乎无法弄清楚按字母顺序打印 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) );
        }


}

任何想法将不胜感激!

4

1 回答 1

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);
    }
}
于 2013-07-25T03:55:44.687 回答