简体   繁体   English

打印特里树的单词

[英]Printing the words of a trie tree

I was trying to print the contents of a trie in C. However I'm not very sucessful. 我试图在C中打印出trie的内容。但是我不是很成功。 And also let me say right in the beginning that this something we are doing in school right now, and this is one exercise. 而且让我在一开始就说我们现在正在学校做这件事,这是一个练习。

This is how my trie looks like: 这就是我的特里的样子:

struct node{
    char letter; //holds the letter associated with that node
    int count; //its count
    struct node* child[26]; //each node can have 26 children
};

struct trie{
    struct node* root;
};

This print method has to traverse this trie and print the words in alphabetic order and the ones that have a count of 0 should not be printed. 此打印方法必须遍历此trie并按字母顺序打印单词,并且不应打印计数为0的单词。

I was thinking along recursion and this is what my code looks like: 我正在考虑递归,这就是我的代码:

void print(node* root) {
    char buffer[15];//array to store the letters
    if(root==NULL){ return;}//if the root is null return
    int i;
    int index=0; //index for the buffer
    int hasAChild=hasChild(root);

    if (hasAChild != 0) { //the root has children keep on going
        for (i=0;i<27;i++) {
            //go thru all the children and if they have children call print                         recursively
            if (hasChild(root->child[i])) {
                print(root->child[i]);
            }
            else {
                // if they have no more children add the letter to the buffer
                buffer[index++] = root->child[i]->letter;
            }
            // print the contents in the bufffer
            printf("%s: %d",root->child[i]->count);
        }
    }
}

// function to determine if a node has children, if so it returns their number if not,returns 0

int hasChild(root) {
    if(root==NULL){
        return 0;
    }

    int i;
    int count=0;
    for(i=0;i<27;i++){
        if(root->child[i]!=NULL){
            count++;
        }
    }
    return count;
}

This is what it would look like 这就是它的样子

Example of a trie: 特里的例子:

Root
  +--- a:2
  |     +--- t:4
  |
  +--- b:3
  |     +--- e:5
  |
  +--- c:0

we have 'a' 2 times, 'at' 4 times, 'b' 3 times and 'be' 5 times, only the words should be printed. 我们有'a'2次,'at'4次','b'3次'和'be'5次,只有字应该打印出来。 In this example I will have to print at: 4 be: 5 but not c: 0 since its count is 0 在这个例子中,我必须打印:4 be:5但不是c:0,因为它的计数是0

So i'm only suppose to print the words that are formed, not the letters that do not form a word. 所以我只想打印出形成的单词,而不是不形成单词的单词。 Any help or guidance would be greatly appreciated. 任何帮助或指导将不胜感激。 Thank you! 谢谢!

I see three issues here: 我在这里看到三个问题:

  • you append a letter to the current buffer only when the current node has no children. 只有当前节点没有子节点时才向当前缓冲区附加一个字母。 This means that you will only display the last letter of the words. 这意味着您只会显示单词的最后一个字母。

  • in addition, every time you enter the function, you start with an empty buffer. 此外,每次进入该函数时,都以空缓冲区开始。

  • your printf statement is missing a string argument. 你的printf语句缺少一个字符串参数。

You need to pass the length and the buffer, and make sure to null-terminate it correctly. 您需要传递长度和缓冲区,并确保正确地终止它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM