java - Java递归中找不到符号

标签 java recursion

我正在尝试帮助一个 friend 用Java实现Trie结构,尽管我的编程水平不是很好,更不用说我的Java了。我找到了一个实现here并尝试在 IDEA IntelliJ 上运行它。

我收到的错误是,

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});
        }

    }
}

最佳答案

您的代码正在尝试调用 children 上的 getWords(),它是 TrieNode数组。大概的目的是收集每个子节点的结果。由于您已经处于循环中,因此您只需更新代码以引用当前项目:

list.addAll(children[i].getWords());

关于java - Java递归中找不到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40985558/

相关文章:

Java 包 - 引用来自不同包的类

java - 如何在 for 循环中自动递增变量名?

java - 覆盖 Object.equals VS 重载它

java - Java中连续将小文件读入对象的最有效方法

c - 如何调试合并排序?

c++ - 我的方法不反转字符数组

java - 在 Swing 中打印度数符号 (°) 时出现问题

java - 如何递归地找到直方图中的最大矩形面积?

haskell - 尾递归和保护递归有什么区别?

java - 如何修复递归字符串检查