leetcode208 Implement Trie (Prefix Tree)--java实现
来源:互联网 发布:fiji软件 编辑:程序博客网 时间:2024/04/28 21:21
Trie,又称单词查找树或键树,是一种树形结构。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。
Trie树的三个基本性质:
1.根节点不包含字符,除根节点外每一个节点都只包含一个字符。
2.从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
3.每个节点的所有子节点包含的字符都不相同。
java程序如下:
import java.util.LinkedList;class TrieNode{// Initialize your data structure here.char content; // the character in the nodeboolean isEnd; // whether the end of the wordsint count; // the number of words sharing this characterLinkedList<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 class Trie {private TrieNode root;public Trie(){root = new TrieNode(' ');}// Inserts a word into the trie.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++;}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));}}if(current.isEnd==true){return true;}else{return false;}}// Returns if there is any word in the trie // that starts with the given prefix.public boolean startsWith(String prefix) {TrieNode current = root;for(int i=0;i<prefix.length();i++){if(current.subNode(prefix.charAt(i))==null){return false;}else{current=current.subNode(prefix.charAt(i));}}return true; }}用心仔细看,真的没有那么困难。
0 0
- leetcode208 Implement Trie (Prefix Tree)--java实现
- LeetCode208 Implement Trie (Prefix Tree) Java
- Leetcode208-Implement Trie (Prefix Tree)
- LeetCode208:Implement Trie (Prefix Tree)
- LeetCode208 --- Implement Trie (Prefix Tree)
- LeetCode208 Implement Trie (Prefix Tree)
- LeetCode208. Implement Trie (Prefix Tree)实现前缀树
- LeetCode208—Implement Trie (Prefix Tree)
- java 前缀树实现 leetcode 208: Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- OC -- 文件学习
- jsp中的三种Scriptlet写法及其区别
- 黑马程序员IOS基础---C语言学习之static和extern关键字对函数以及变量的作用
- 南阳oj(nyoj) 791 Color the fence
- Ubuntu 14.04 环境下配置Symfony 2.3
- leetcode208 Implement Trie (Prefix Tree)--java实现
- Spring学习笔记(20)----------Schema初步学习
- JavaScript特效实例003-通过按钮创建窗口
- php 设计模式
- 实现DFS之“骨头的诱惑”
- python编程入门读书笔记1
- "iOS push全方位解析(二)【译文】"——生成OpenSSL证书,Provisioning Profile
- 笔记win7+centos6.5双系统安装
- 实现DFS之“油田”