码迷,mamicode.com
首页 > 其他好文 > 详细

Implement Trie (Prefix Tree)

时间:2015-06-28 20:02:22      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/implement-trie-prefix-tree/

Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

解题思路:

https://en.wikipedia.org/wiki/Trie

首先复习一下,什么叫做Trie树。Trie树的每个节点通常用来保存字符,root节点为空,下面每个父节点到子节点的指针位置,由子节点所代表的字符值决定。所以,Trie树中,通常没有value这个field。

这样,对于Trie树中的某个节点,它的所有子节点拥有相同的字符串前缀。可以看出,Trie树不是一个二叉树,因为对于任意节点,可能它有26个子节点,或者更多(在不限定为小写字母,或者其他字符的情况下)。

Trie树通常被用来做字符串搜索,或者搜索时的字符串输入提示。

本题中,search方法用来查找已经插入Trie树的字符串。要注意,不是只有到叶节点的路径才是已经插入的字符串word,也有可能先插入abcd,再插入abc。所以,对于每个节点,需要一个boolean的值来表示,这个节点到底是不是一个word,还是仅仅是一个前缀。

可以看到,Trie树中的搜索,或者一个字符串,永远是从root开始。不存在从任意节点到任意节点的word。

Trie树的实现,可以用TrieNode[],也可以用HashMap。前者的问题是,可能有很多哦sparse matrix。

下面是一个数组的实现,有了上面的思路后,代码还是比较清楚的。

class TrieNode {
    // Initialize your data structure here.
    boolean isWord;
    TrieNode[] next;
    public TrieNode() {
        next = new TrieNode[26];
        isWord = false;
    }
}

public class Trie {
    private TrieNode root;

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

    // Inserts a word into the trie.
    public void insert(String word) {
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++) {
            if(cur.next[word.charAt(i) - ‘a‘] == null) {
                TrieNode next = new TrieNode();
                cur.next[word.charAt(i) - ‘a‘] = next;
            }
            cur = cur.next[word.charAt(i) - ‘a‘];
        }
        cur.isWord = true;
    }

    // Returns if the word is in the trie.
    public boolean search(String word) {
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++) {
            cur = cur.next[word.charAt(i) - ‘a‘];
            if(cur == null) {
                return false;
            }
        }
        return cur.isWord;
    }

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    public boolean startsWith(String prefix) {
        TrieNode cur = root;
        for(int i = 0; i < prefix.length(); i++) {
            cur = cur.next[prefix.charAt(i) - ‘a‘];
            if(cur == null) {
                return false;
            }
        }
        return true;
    }
}

// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");

用hashMap也一样,代码如下

class TrieNode {
    // Initialize your data structure here.
    boolean isWord;
    Map<Character, TrieNode> next;
    public TrieNode() {
        next = new HashMap<Character, TrieNode>();
        isWord = false;
    }
}

public class Trie {
    private TrieNode root;

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

    // Inserts a word into the trie.
    public void insert(String word) {
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++) {
            if(cur.next.get(word.charAt(i)) == null) {
                TrieNode next = new TrieNode();
                cur.next.put(word.charAt(i), next);
            }
            cur = cur.next.get(word.charAt(i));
        }
        cur.isWord = true;
    }

    // Returns if the word is in the trie.
    public boolean search(String word) {
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++) {
            cur = cur.next.get(word.charAt(i));
            if(cur == null) {
                return false;
            }
        }
        return cur.isWord;
    }

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

// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");

 

Implement Trie (Prefix Tree)

标签:

原文地址:http://www.cnblogs.com/NickyYe/p/4605997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!