# LeetCode Implement Trie (Prefix Tree)

Description:

Implement a trie with insertsearch, and startsWith methods.

Solution:

Trie树是字符串常用的一个数据结构，AC自动机也会用到。这里需要注意的是，prefix和search功能的不一样，也就要求了要在每个节点中添加一个标记，记录当前节点是否表示一个字符串的结束。

public class Trie {
private TrieNode root;

public Trie() {
root = new TrieNode();
}

// Inserts a word into the trie.
public void insert(String word) {
TrieNode temp = root, next;
int ch;
for (int i = 0; i < word.length(); i++) {
ch = word.charAt(i) - 'a';
next = temp.nextNodes[ch];
if (next == null) {
next = new TrieNode();
temp.nextNodes[ch] = next;
}
temp = next;
}
temp.isEnd = true;
}

// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode temp = root, next;
int ch;

for (int i = 0; i < word.length(); i++) {
ch = word.charAt(i) - 'a';
next = temp.nextNodes[ch];
if (next == null)
return false;
temp = next;
}

return temp.isEnd;
}

// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode temp = root, next;
int ch;

for (int i = 0; i < prefix.length(); i++) {
ch = prefix.charAt(i) - 'a';
next = temp.nextNodes[ch];
if (next == null)
return false;
temp = next;
}
return true;
}

public static void main(String[] args) {
Trie t = new Trie();
t.insert("abb");
t.insert("c");
System.out.println(t.search("a"));
System.out.println(t.startsWith("a"));
}
}

class TrieNode {
TrieNode[] nextNodes = new TrieNode[26];
boolean isEnd;

TrieNode() {

}

}


• 本文已收录于以下专栏：

## Leetcode 208: Implement Trie (Prefix Tree)

Implement Trie (Prefix Tree) Total Accepted: 601 Total Submissions: 2396 Implement a tri...
• xudli
• 2015年05月09日 06:59
• 3980

## LeetCode 208: Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a...
• sunao2002002
• 2015年06月27日 15:32
• 3213

## Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods.Note: You may assume that all inputs a...
• u010786672
• 2015年05月08日 21:32
• 872

## [LeetCode]Implement Trie(Prefix Tree)，解题报告

• zinss26914
• 2015年05月15日 14:12
• 4556

## [LeetCode 208] Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all in...
• sbitswc
• 2015年08月24日 13:09
• 616

## LeetCode208:Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods.Note: You may assume that all inputs a...
• u012501459
• 2015年07月03日 15:44
• 808

## LeetCode208 Implement Trie (Prefix Tree) Java

• l294265421
• 2016年04月22日 21:59
• 562

## Implement Trie (Prefix Tree) LeetCode Java

Implement a trie with insert, search, and startsWith methods.Note: You may assume that all inputs a...
• zuoyexingchennn
• 2015年06月01日 19:50
• 199

## leetcode[208]:Implement Trie (Prefix Tree)

【题目】 Implement a trie with insert, search, and startsWith methods.Note: You may assume that all i...
• sunyangwei1993
• 2016年08月20日 15:06
• 287

## leetcode:Implement Trie (Prefix Tree)

Implement Trie (Prefix Tree) Total Accepted: 2015 Total Submissions: 8299 My Submissions Question So...
• chanrenyuan
• 2015年05月14日 00:13
• 213

举报原因： 您举报文章：LeetCode Implement Trie (Prefix Tree) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)