Implement Trie (Prefix Tree)
2016-06-29 04:02
323 查看
关注Trie 这种结构已经很久,Trie有一个很有趣的用途,那就是自动提示。而且,前不久在一次面试里,也需要用Trie来解答。所以,在此对这个数据结构进行总结。
Trie,又称单词查找树或键树,是一种树形结构。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。
它有3个基本性质:
根节点不包含字符,除根节点外每一个节点都只包含一个字符。
从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
每个节点的所有子节点包含的字符都不相同。
下面这个图就是Trie的表示,每一条边表示一个字符,如果结束,就用星号表示。在这个Trie结构里,我们有下面字符串,比如do, dork, dorm等,但是Trie里没有ba, 也没有sen,因为在a, 和n结尾,没有结束符号(星号)。
有了这样一种数据结构,我们可以用它来保存一个字典,要查询改字典里是否有相应的词,是否非常的方便呢?我们也可以做智能提示,我们把用户已经搜索的词存在Trie里,每当用户输入一个词的时候,我们可以自动提示,比如当用户输入 ba, 我们会自动提示 bat 和 baii.
现在来讨论Trie的实现。
首先,我们定义一个TrieNode。
现在我们来看这个Trie类的具体实现。
时间复杂度分析:
对于insert, 如果被插入的String长度是 k, 每对一个字符进行查询,我们最多在child linkedlist里面查询26次(最多26个字母),所以,复杂度为O(26*k) = O(k). 对于 search, 复杂度是一样的。
本文代码来自:http://www.technicalypto.com/2010/04/trie-in-java.html
转载请注明出处:http://blog.csdn.net/beiyeqingteng
Trie,又称单词查找树或键树,是一种树形结构。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。
它有3个基本性质:
根节点不包含字符,除根节点外每一个节点都只包含一个字符。
从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
每个节点的所有子节点包含的字符都不相同。
下面这个图就是Trie的表示,每一条边表示一个字符,如果结束,就用星号表示。在这个Trie结构里,我们有下面字符串,比如do, dork, dorm等,但是Trie里没有ba, 也没有sen,因为在a, 和n结尾,没有结束符号(星号)。
有了这样一种数据结构,我们可以用它来保存一个字典,要查询改字典里是否有相应的词,是否非常的方便呢?我们也可以做智能提示,我们把用户已经搜索的词存在Trie里,每当用户输入一个词的时候,我们可以自动提示,比如当用户输入 ba, 我们会自动提示 bat 和 baii.
现在来讨论Trie的实现。
首先,我们定义一个TrieNode。
class TrieNode { // Initialize your data structure here. 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<TrieNode> childList; // the child list public TrieNode(char c) { childList = new LinkedList<TrieNode>(); isEnd = false; content = c; count = 0; } public TrieNode subNode(char c) { if (childList != null) { for (TrieNode eachChild : childList) { if (eachChild.content == c) { return eachChild; } } } return null; } public TrieNode() { childList = new LinkedList<TrieNode>(); isEnd = false; content = ' '; count = 0; } }
现在我们来看这个Trie类的具体实现。
public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { if (search(word) == true) return; TrieNode current = root; for (int i = 0; i < word.length(); i++) { TrieNode child = current.subNode(word.charAt(i)); if (child != null) { current = child; } else { current.childList.add(new TrieNode(word.charAt(i))); current = current.subNode(word.charAt(i)); } current.count++; } // Set isEnd to indicate end of the word current.isEnd = true; } // Returns if the word is in the trie. public boolean search(String word) { TrieNode 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; return false; } // Returns if there is any word in the trie // that starts with the given prefix. public boolean startsWith(String word) { TrieNode 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)); } return true; } public void deleteWord(String word){ if(search(word) == false) return; TrieNode current = root; for(char c : word.toCharArray()) { TrieNode child = current.subNode(c); if(child.count == 1) { current.childList.remove(child); return; } else { child.count--; current = child; } } current.isEnd = false; } }
时间复杂度分析:
对于insert, 如果被插入的String长度是 k, 每对一个字符进行查询,我们最多在child linkedlist里面查询26次(最多26个字母),所以,复杂度为O(26*k) = O(k). 对于 search, 复杂度是一样的。
本文代码来自:http://www.technicalypto.com/2010/04/trie-in-java.html
转载请注明出处:http://blog.csdn.net/beiyeqingteng
相关文章推荐
- 怎么才能有写程序的思路
- 项目-第六次全网备份上机考试
- 354. Russian Doll Envelopes
- 总结java.awt.Robot类
- SQL数据库开发中的一些经典代码
- SQL数据库开发中的一些经典代码
- jQuery函数学习
- jQuery函数学习
- 各种头声明
- Leetcode 368. Largest Divisible Subset
- Word Search I & II
- 使用Python计算物品的流行度和用户所评物品的平均流行度
- [LC352]Data Stream as Disjoint Intervals
- 由浅入深多线程开发概念
- 由浅入深多线程开发概念
- myeclipse添加server library
- Intent和IntentFilter详解
- 今夜无月色
- iOS极光推送SDK的使用流程
- iOS极光推送SDK的使用流程