2016-12-05 21 views
0

プログラミングはそれほどではありませんが、JavaでTrie構造を実装するのを助けようとしています。実装hereが見つかり、IDEA IntelliJで実行しようとしました。Javaの再帰でシンボルを見つけることができません

私は受信エラーは、私は、このエラーのトラブルシューティングを開始する場所がわからないんだけど、

Error:(87, 41) java: cannot find symbol 
    symbol: method getWords() 
    location: variable children of type com.company.TrieNode[] 

です。私はそれがループと関係しているかもしれないと感じていますか?私はそれを分析する前にコードを実行したいが、このエラーはハードルです。

私はすでにIntelliJ内でFile > Invalidate Caches/Restartを試しましたが、同じエラーが発生します。私はこれがJavaの問題かもしれないと思う。

次は

、私は(上記のリンクにクレジット)持っているものであるMain.java

package com.company; 

public class Main { 

    public static void main(String[] args) { 
     Trie myTrie = new Trie(); 
     myTrie.addWord("Khalid"); 
    } 
} 

Trie.java

package com.company; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Khalid on 5 December. 
*/ 
public class Trie 
{ 
    private TrieNode root; 

    /** 
    * Constructor 
    */ 
    public Trie() 
    { 
     root = new TrieNode(); 
    } 

    /** 
    * Adds a word to the Trie 
    * @param word 
    */ 
    public void addWord(String word) 
    { 
     root.addWord(word.toLowerCase()); 
    } 

    /** 
    * Get the words in the Trie with the given 
    * prefix 
    * @param prefix 
    * @return a List containing String objects containing the words in 
    *   the Trie with the given prefix. 
    */ 
    public List getWords(String prefix) 
    { 
     //Find the node which represents the last letter of the prefix 
     TrieNode lastNode = root; 
     for (int i=0; i<prefix.length(); i++) 
     { 
      lastNode = lastNode.getNode(prefix.charAt(i)); 

      //If no node matches, then no words exist, return empty list 
      if (lastNode == null) return new ArrayList(); 
     } 

     //Return the words which eminate from the last node 
     return lastNode.getWords(); 
    } 
} 

TrieNode.java

package com.company; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Khalid on 5 December. 
*/ 
public class TrieNode { 
    private TrieNode parent; 
    private TrieNode[] children; 
    private boolean isLeaf;  //Quick way to check if any children exist 
    private boolean isWord;  //Does this node represent the last character of a word 
    private char character;  //The character this node represents 

    /** 
    * Constructor for top level root node. 
    */ 
    public TrieNode() { 
     children = new TrieNode[26]; 
     isLeaf = true; 
     isWord = false; 
    } 

    /** 
    * Constructor for child node. 
    */ 
    public TrieNode(char character) { 
     this(); 
     this.character = character; 
    } 

    /** 
    * Adds a word to this node. This method is called recursively and 
    * adds child nodes for each successive letter in the word, therefore 
    * recursive calls will be made with partial words. 
    * 
    * @param word the word to add 
    */ 
    protected void addWord(String word) { 
     isLeaf = false; 
     int charPos = word.charAt(0) - 'a'; 

     if (children[charPos] == null) { 
      children[charPos] = new TrieNode(word.charAt(0)); 
      children[charPos].parent = this; 
     } 

     if (word.length() > 1) { 
      children[charPos].addWord(word.substring(1)); 
     } else { 
      children[charPos].isWord = true; 
     } 
    } 

    /** 
    * Returns the child TrieNode representing the given char, 
    * or null if no node exists. 
    * 
    * @param c 
    * @return 
    */ 
    protected TrieNode getNode(char c) { 
     return children[c - 'a']; 
    } 

    /** 
    * Returns a List of String objects which are lower in the 
    * hierarchy that this node. 
    * 
    * @return 
    */ 
    protected List getWords() { 
     //Create a list to return 
     List list = new ArrayList(); 

     //If this node represents a word, add it 
     if (isWord) { 
      list.add(toString()); 
     } 

     //If any children 
     if (!isLeaf) { 
      //Add any words belonging to any children 
      for (int i = 0; i < children.length; i++) { 
       if (children[i] != null) 
        list.addAll(children.getWords()); 
      } 
     } 
     return list; 
    } 

    /** 
    * Gets the String that this node represents. 
    * For example, if this node represents the character t, whose parent 
    * represents the charater a, whose parent represents the character 
    * c, then the String would be "cat". 
    * 
    * @return 
    */ 

    public String toString() { 
     if (parent == null) { 
      return ""; 
     } else { 
      return parent.toString() + new String(new char[]{character}); 
     } 

    } 
} 
+2

エラーはかなり明確です。あなたは 'TrieNode []'のインスタンスである 'children'に対して' getWords() 'を呼び出すことを望んでおり、そのようなメソッドはありません。 – shmosel

+0

'TrieNode'クラス内で' getWords() 'が定義されていませんか? –

+0

'TrieNode []'は 'TrieNode'と同じではありません。 – shmosel

答えて

1

あなたのコードはTrieNode配列である、childrengetWords()を呼び出そうとしています。おそらく、各子ノードの結果を収集することが意図されています。すでにループしているので、現在のアイテムを参照するようにコードを更新するだけで済みます:

list.addAll(children[i].getWords()); 
関連する問題