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的更多相关文章

  1. [Algorithm] Trie data structure

    For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...

  2. (Data structure)Implement Trie && Add and Search Word

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  3. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  4. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  5. 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  ...

  6. [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 ...

  7. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. A Realistic Evaluation of Memory Hardware Errors and Software System Susceptibility

    http://www.cs.rochester.edu/~kshen/papers/usenix2010-li.pdf Abstract Memory hardware reliability is ...

  2. ArcGIS Server 服务迁移、恢复

    服务迁移与恢复 如果服务可以迁移,那么备份迁移所需文件,即可恢复原服务,所以"服务的迁移与恢复"."服务的备份"问题可以汇总为一个问题"怎样迁移服务. ...

  3. hibernate多SessionFactory配置

    <bean id="aDataSource" class="org.apache.commons.dbcp.BasicDataSource" destro ...

  4. github中国版本coding.net 的部署和使用

    1.在coding.net注册帐号. 2.安装github,自己百度github软件然后安装. 3.打开coding.net 输入帐号后新建项目 创建项目 创建后,创建ssh公钥,如果不创建的话,在每 ...

  5. 日志案例分析(PV,UV),以及动态分区

    1.实现的流程 需求分析 时间:日,时段 分区表:两级 PV UV 数据清洗 2015-08-28 18:19:10 字段:id,url,guid,tracktime 数据分析 导出 2.新建源数据库 ...

  6. mssql全文索引

    在使用全文索引的时候例如: SELECT [PRID] ,[PRCode] ,[PRDesc] FROM [test1].[dbo].[PerformanceIssue] where contains ...

  7. 使用Areas分离ASP.NET MVC项目

    为什么需要分离? 我们知道MVC项目各部分职责比较清晰,相比较ASP.NET Webform而言,MVC项目的业务逻辑和页面展现较好地分离开来,这样的做法有许多优点,比如可测试,易扩展等等.但是在实际 ...

  8. 371. Sum of Two Integers

    不用加减法计算两个整数的和.这道题其实是考察一些基本的布尔代数知识.我们知道,二进制表示时: 0 + 0 = 00 1 + 0 = 01 0 + 1 = 01 1 + 1 = 10 所以,两个二进制整 ...

  9. Android模拟器disconnected问题

    具体原因不明,偶尔会出现 window -> Show Views -> device -> view menu -> Reset adb  一般可以解决该问题

  10. Tram---poj1847(简单最短路)

    题目链接:http://poj.org/problem?id=1847 题意:给了N个交叉口,每个交叉口有自己能转到的交叉口. 注意这里:First number in the i-th line, ...