首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用java实现Trie

用java实现Trie
EN

Stack Overflow用户
提问于 2020-04-21 17:00:44
回答 1查看 225关注 0票数 0

尝试实现trie,但它显示内存限制超过了。我试图预先形成的操作是insert、Search,并开始并实现这些函数,我已经为它创建了util函数。我试图在leetcode中提交此解决方案,但超出了鞋子内存限制。请查看这段代码,并帮助我理解问题,而且在许多我见过的程序中,他们都保留了count变量,其目的是什么。

代码语言:javascript
复制
class Trie {

    /** Initialize your data structure here. */
   Trie root;
    Trie []links;
    final int r=26;

    public boolean isEnd;

    public Trie() {

          links=new Trie[r];
        root=new Trie();
    }
    public boolean containsKey(char c)
    {
        return links[c-'a']!=null;
    }
    public Trie get(char ch)
    {
        return links[ch-'a'];
    }
    public void put(char ch,Trie node)
    {
        links[ch-'a']=node;
    }
    public void setEnd()
    {
        isEnd=true;
    }
    public boolean isEnd()
    {
        return isEnd;
    }

    /** Inserts a word into the trie. */
    public void insert(String word) {
        Trie node=root;
        for(int i=0;i<word.length();i++)
        {
            char ch=word.charAt(i);
            if(!node.containsKey(ch))
            {
                node.put(ch,new Trie());
            }
            node=node.get(ch);
        }
        node.setEnd();   
    }

    /** Returns if the word is in the trie. */
    public boolean search(String word) {           
        Trie node=searchPrefix(word);
      //    Trie node=null;
        return node!=null && node.isEnd();
    }
    public Trie searchPrefix(String word)
    {
        Trie node=root;
        for(int i=0;i<word.length();i++)
        {
            char ch=word.charAt(i);
            if(node.containsKey(ch))
            {
                node=node.get(ch);
            }
            else
                return null;
        }
        return node;
    }

    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
       Trie node=searchPrefix(prefix);
        //Trie node=null;
        return node!=null;

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-21 19:49:26

导致OutOfMemory错误的直接原因是Trie的构造函数中存在递归。

代码语言:javascript
复制
public Trie() {
    links=new Trie[r];
    root=new Trie();
}

您是否看到要创建一个Trie,必须创建一个Trie。这将继续下去,直到内存耗尽为止。

尽管如此,我认为您实际上需要稍微重构代码,将现有的功能划分为两个单独的类:Trie (作为一个整体的结构)和一个TrieNode (包含对子TrieNode的引用)。Trie将包含对根TrieNode的引用。

TrieNode类:

代码语言:javascript
复制
class TrieNode
{
    /** Initialize your data structure here. */
    TrieNode[] links;
    final int r = 26;

    public boolean isEnd;

    public TrieNode()
    {
        links = new TrieNode[r];
    }

    public boolean containsKey(char c)
    {
        return links[c - 'a'] != null;
    }

    public TrieNode get(char ch)
    {
        return links[ch - 'a'];
    }

    public void put(char ch, TrieNode node)
    {
        links[ch - 'a'] = node;
    }

    public void setEnd()
    {
        isEnd = true;
    }

    public boolean isEnd()
    {
        return isEnd;
    }
}

Trie类:

代码语言:javascript
复制
class Trie
{
    TrieNode root;

    public Trie()
    {
        root = new TrieNode();
    }

    /** Inserts a word into the trie. */
    public void insert(String word)
    {
        TrieNode node = root;
        for (int i = 0; i < word.length(); i++)
        {
            char ch = word.charAt(i);
            if (!node.containsKey(ch))
            {
                node.put(ch, new TrieNode());
            }
            node = node.get(ch);
        }
        node.setEnd();
    }

    /** Returns if the word is in the trie. */
    public boolean search(String word)
    {
        TrieNode node = searchPrefix(word);
        // Trie node=null;
        return node != null && node.isEnd();
    }

    public TrieNode searchPrefix(String word)
    {
        TrieNode node = root;
        for (int i = 0; i < word.length(); i++)
        {
            char ch = word.charAt(i);
            if (node.containsKey(ch))
            {
                node = node.get(ch);
            } else
                return null;
        }
        return node;
    }

    /**
     * Returns if there is any word in the trie that starts with the given prefix.
     */
    public boolean startsWith(String prefix)
    {
        TrieNode node = searchPrefix(prefix);
        // Trie node=null;
        return node != null;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61348981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档