欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

leetcode 208. 实现 Trie (前缀树)

程序员文章站 2022-07-15 16:17:49
...

leetcode 208. 实现 Trie (前缀树)

class Trie {

    /** Initialize your data structure here. */
    private Node root;
    private int size;
    public Trie(){
        root=new Node();
        
    }

    private class Node{
        public boolean isWord;
        public TreeMap<Character,Node>next;//多叉树
        public Node(boolean isWord){
            this.isWord=isWord;
            next=new TreeMap<>();
        }
        public Node(){
            // this(false);相当于调用构造器NOde(boolen is Word)
            this.isWord=false;
            next=new TreeMap<>();
        }

    }





    
    /** Inserts a word into the trie. */

    public void insert(String word){
        Node cur=root;
        for(int i=0;i<word.length();i++){
            char c=word.charAt(i);
            if(cur.next.get(c)==null)
                cur.next.put(c,new Node());
            cur=cur.next.get(c);//走到c结点
        }
        //cur 走到了word最后一个字符,不一定是叶子结点,如panda(中pan 为一个单词)
        if(!cur.isWord)
        {
            cur.isWord=true;
        }
    }
    
    /** Returns if the word is in the trie. */

    public boolean search(String word){
        Node cur=root;
        for(int i=0;i<word.length();i++){
            char c=word.charAt(i);
            if(cur.next.get(c)==null) return false;
            cur=cur.next.get(c);
        }
        return cur.isWord;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */

     public boolean startsWith(String prefix){
        Node cur=root;
        for(int i=0;i<prefix.length();i++){
            char c=prefix.charAt(i);
            if(cur.next.get(c)==null) return false;
            cur=cur.next.get(c);
        }
        return true;

    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */

递归实现

class Trie {

    /** Initialize your data structure here. */
    private Node root;
    private int size;
    public Trie(){
        root=new Node();
        
    }

    private class Node{
        public boolean isWord;
        public TreeMap<Character,Node>next;//多叉树
        public Node(boolean isWord){
            this.isWord=isWord;
            next=new TreeMap<>();
        }
        public Node(){
            // this(false);相当于调用构造器NOde(boolen is Word)
            this.isWord=false;
            next=new TreeMap<>();
        }

    }





    
    /** Inserts a word into the trie. */

    public void insert(String word){
        _insert(root,0,word);
    }
    // 从cur 节点构建word[index:]求word[0:]
    public  void _insert(Node cur,int i,String word){
       if(i==word.length())
         {
             cur.isWord=true;
             return ;
         }
        //cur 走到了word最后一个字符,不一定是叶子结点,如panda(中pan 为一个单词)
        char c=word.charAt(i);
        if(cur.next.get(c)==null)
            cur.next.put(c,new Node());
        cur=cur.next.get(c);//走到c结点
        _insert(cur,i+1,word);
    
    }


    /** Returns if the word is in the trie. */

    public boolean search(String word){
       return _search(word,root,0);
    }
    public boolean _search(String word,Node cur,int i){
        if(i==word.length()) return cur.isWord;
            char c=word.charAt(i);
            if(cur.next.get(c)==null) return false;
            cur=cur.next.get(c);
         return _search(word, cur, i+1);
    }
    /** Returns if there is any word in the trie that starts with the given prefix. */

     public boolean startsWith(String prefix){
        return _startsWith(prefix,root,0);
    }
     public boolean _startsWith(String word,Node cur,int i){
        if(i==word.length()) return true;
            char c=word.charAt(i);
            if(cur.next.get(c)==null) return false;
            cur=cur.next.get(c);
        return _startsWith(word,cur,i+1);
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */
相关标签: leetcode