Trie有一个很有趣的用途,那就是自动提示。
Trie,又称字典树,单词查找树或键树,是一种树形结构。它来源于retrieval(检索)中取中间四个字符构成(读音同try)。用于存储大量的字符串以便支持快速模式匹配,检索领域主要应用在信息。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。
它有3个基本性质:
![](http://hi.csdn.net/attachment/201111/17/0_13215049168ly3.gif)
有了这样一种数据结构,我们可以用它来保存一个字典,要查询改字典里是否有相应的词,是否非常的方便呢?我们也可以做智能提示,我们把用户已经搜索的词存在Trie里,每当用户输入一个词的时候,我们可以自动提示,比如当用户输入 ba, 我们会自动提示 bat 和 baii.
现在来讨论Trie的实现。
首先,我们定义一个Abstract Trie,Trie 里存放的是一个Node。这个类里有两个操作,一个是插入,另一个是查询。具体实现放在后面。
Node 类的实现
- class Node {
- char content; // the character in the node
- boolean isEnd; // whether the end of the words
- int count; // the number of words sharing this character
- LinkedList<Node> childList; // the child list
- public Node(char c){
- childList = new LinkedList<Node>();
- isEnd = false;
- content = c;
- count = 0;
- }
- public Node subNode(char c){
- if(childList != null){
- for(Node eachChild : childList){
- if(eachChild.content == c){
- return eachChild;
- }
- }
- }
- return null;
- }
- }
现在我们来看这个Trie类的具体实现。
- public class Trie{
- private Node root;
- public Trie(){
- root = new Node(' ');
- }
- public void insert(String word){
- if(search(word) == true) return;
- Node current = root;
- for(int i = 0; i < word.length(); i++){
- Node child = current.subNode(word.charAt(i));
- if(child != null){
- current = child;
- } else {
- current.childList.add(new Node(word.charAt(i)));
- current = current.subNode(word.charAt(i));
- }
- current.count++;
- }
- // Set isEnd to indicate end of the word
- current.isEnd = true;
- }
- public boolean search(String word){
- Node current = root;
- for(int i = 0; i < word.length(); i++){
- if(current.subNode(word.charAt(i)) == null)
- return false;
- else
- current = current.subNode(word.charAt(i));
- }
- /*
- * This means that a string exists, but make sure its
- * a word by checking its 'isEnd' flag
- */
- if (current.isEnd == true) return true;
- else return false;
- }
- public void deleteWord(String word){
- if(search(word) == false) return;
- Node current = root;
- for(char c : word.toCharArray()) {
- Node child = current.subNode(c);
- if(child.count == 1) {
- current.childList.remove(child);
- return;
- } else {
- child.count--;
- current = child;
- }
- }
- current.isEnd = false;
- }
- public static void main(String[] args) {
- Trie trie = new Trie();
- trie.insert("ball");
- trie.insert("balls");
- trie.insert("sense");
- // testing deletion
- System.out.println(trie.search("balls"));
- System.out.println(trie.search("ba"));
- trie.deleteWord("balls");
- System.out.println(trie.search("balls"));
- System.out.println(trie.search("ball"));
- }
- }
Trie时间复杂度分析:
对于insert, 如果被插入的String长度是 k, 每对一个字符进行查询,我们最多在child linkedlist里面查询26次(最多26个字母),所以,复杂度为O(26*k) = O(k).
对于 search, 复杂度是一样的。
Trie空间复杂度分析:
空间复杂度是26的n次方,非常庞大(可采用双数组实现改善)。(详见参考3)
Tire树的应用:
1 字符串检索
2 字符串最大公共前缀
3 排序(Trie树是一颗多叉树,只要前序遍历整棵树,输出相应的字符串便是按字典排序的结果)
4 其它数据结构的辅助结构(后缀树、AC自动机)
红黑树的时间复杂度--空间复杂度
除了O(log n)的时间之外,红黑树的持久版本对每次插入或删除需要O(log n)的空间。
参考:
1 http://blog.csdn.net/beiyeqingteng
2 http://blog.csdn.net/wzb56/article/details/7902669
3 http://dongxicheng.org/structure/trietree/