Summary: Trie Data Structure
Implement a Trie Data Structure, and search() & insert() function:
we need to implement both Class Trie and Class TrieNode
Class Trie:
import java.util.ArrayList;
import java.util.List; public class Trie
{
private TrieNode root; /**
* Constructor
*/
public Trie()
{
root = new TrieNode();
} /**
* Adds a word to the Trie
* @param word
*/
public void addWord(String word)
{
root.addWord(word.toLowerCase());
} /**
* Get the words in the Trie with the given
* prefix
* @param prefix
* @return a List containing String objects containing the words in
* the Trie with the given prefix.
*/
public List getWords(String prefix)
{
//Find the node which represents the last letter of the prefix
TrieNode lastNode = root;
for (int i=0; i<prefix.length(); i++)
{
lastNode = lastNode.getNode(prefix.charAt(i)); //If no node matches, then no words exist, return empty list
if (lastNode == null) return new ArrayList();
} //Return the words which eminate from the last node
return lastNode.getWords();
}
}
Class TrieNode:
import java.util.ArrayList;
import java.util.List; public class TrieNode
{
private TrieNode parent;
private TrieNode[] children;
private boolean isLeaf; //Quick way to check if any children exist
private boolean isWord; //Does this node represent the last character of a word
private char character; //The character this node represents /**
* Constructor for top level root node.
*/
public TrieNode()
{
children = new TrieNode[26];
isLeaf = true;
isWord = false;
} /**
* Constructor for child node.
*/
public TrieNode(char character)
{
this();
this.character = character;
} /**
* Adds a word to this node. This method is called recursively and
* adds child nodes for each successive letter in the word, therefore
* recursive calls will be made with partial words.
* @param word the word to add
*/
protected void addWord(String word)
{
isLeaf = false;
int charPos = word.charAt(0) - 'a'; if (children[charPos] == null)
{
children[charPos] = new TrieNode(word.charAt(0));
children[charPos].parent = this;
} if (word.length() > 1)
{
children[charPos].addWord(word.substring(1));
}
else
{
children[charPos].isWord = true;
}
} /**
* Returns the child TrieNode representing the given char,
* or null if no node exists.
* @param c
* @return
*/
protected TrieNode getNode(char c)
{
return children[c - 'a'];
} /**
* Returns a List of String objects which are lower in the
* hierarchy that this node.
* @return
*/
protected List getWords()
{
//Create a list to return
List list = new ArrayList(); //If this node represents a word, add it
if (isWord)
{
list.add(toString());
} //If any children
if (!isLeaf)
{
//Add any words belonging to any children
for (int i=0; i<children.length; i++)
{
if (children[i] != null)
{
list.addAll(children.getWords()); } } } return list; } /** * Gets the String that this node represents. * For example, if this node represents the character t, whose parent * represents the charater a, whose parent represents the character * c, then the String would be "cat". * @return */ public String toString() { if (parent == null) { return ""; } else { return parent.toString() + new String(new char[] {character}); } } }
Summary: Trie Data Structure的更多相关文章
- [Algorithm] Trie data structure
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...
- (Data structure)Implement Trie && Add and Search Word
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- [leetcode trie]211. Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
随机推荐
- PHP5.4安装xhprof扩展[不要去pecl下载]
HP5.3或之前版本可以去pecl(http://pecl.php.net)下载xhprof扩展安装. 但pecl上的版本不支持PHP5.4 可以到github上的xhprof库中下载:https:/ ...
- 关于jQuery学习
≡[..]≡≡[..]≡ 所有的实例都位于document.ready里面--为了防止文档在未完全加载之前就运行函数导致操作失败. $(document).ready(function(){ --- ...
- Lazarus -Pascal常量
2.常量 2.1.普通常量 仅仅下面类型可以被定义为常量Ordinal类型Set类型指针类型 (but the only allowed value is Nil). real类型 Char, Str ...
- 【php学习】图片操作
前两天要对一张图片进行处理,其实很简单,就是在图片上加上字符串,一个图片而已,但是自己如同得了短暂性失忆似的,图片操作的函数一个都想不起来.所以就抽空整理了一下图片操作函数. 图片处理三步走: 创建画 ...
- HBase学习笔记-高级(一)
HBase1. hbase.id记录了集群的唯一标识:hbase.version记录了文件格式的版本号2. split和.corrupt目录在日志分裂过程中使用,以便保存一些中间结果和损坏的日志在表目 ...
- sphinx续4-coreseek的工作原理
原文地址:http://blog.itpub.net/29806344/viewspace-1399621/ 在分析sphix原理之前,我先澄清一下为什么经常出现coreseek这个词? 因为sphi ...
- iOS面试题 02
在面试的时候,面试官问我,“你对内存管理了解的多吗?” 我忘了当时是怎么回答的了,但是,肯定是一时没想起来怎么回答. 1.谁创建谁释放 2.autoreleasepool 3.retain,copy, ...
- Java学习-012-文件删除实例及源代码
此文源码主要为应用 Java 创建文件的源代码.若有不足之处,敬请大神指正,不胜感激! 文件删除源代码如下: /** * @function 文件操作:删除文件.若文件存在且未被占用,则删除文件:若文 ...
- Selenium2学习-000-Selenium2初识
什么是 Selenium?Selenium 是 ThoughtWorks 专门为 Web 应用程序编写的一个验收测试工具,是一种 Web 测试框架,开拓了验证 Web 应用程序的新方案,使您可在 We ...
- A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.
图解: 此题过程分为三个阶段,分别是 1.负责后面一个节点,并且将这个节点插入到原来链表中 2.复制后面一个节点的random指针. 3 拆分组合链表为两部分. 第一部分代码: while(curr ...