Количество слов в три - PullRequest
       15

Количество слов в три

0 голосов
/ 26 апреля 2020
 int count=0;
     public  int countwords(TrieNode root){

        if (root.isTerminating==true)
            count++;

        for (int i=0;i<26;i++){
            if (root.children[i]!=null)
        countwords(root.children[i]);
        }

        return count;
    }

эта функция используется для подсчета количества слов в tr ie она дает мне неправильный ответ, что здесь не так? Я использовал isTerminating, чтобы отличать guish слово от другого.

1 Ответ

1 голос
/ 26 апреля 2020
public int countwords(TrieNode root) {
   // variable localized because it is a recursive call
   // also because we add to this variable in the loop
   int count = 0;

   // if condition simplified
   if (root.isTerminating)
       count++;

   for (int i = 0; i < 26; i++) {
       if (root.children[i] != null)
           // you need to save the result of the recursive call
           count += countwords(root.children[i]);
    }
    return count;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...