Leetcode[208] Implement Trie (Prefix Tree)
17 Nov 2015###Task1 Implement a trie with insert, search, and startsWith methods.
Note: You may assume that all inputs are consist of lowercase letters a-z.
###Python
class TrieNode(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.children = [None for i in range(26)]
self.saved = False
class Trie(object):
def __init__(self):
self.root = TrieNode()
def insert(self, word):
"""
Inserts a word into the trie.
:type word: str
:rtype: void
"""
cur = self.root
for ch in word:
if not cur.children[ord(ch) - ord('a')]:
cur.children[ord(ch) - ord('a')] = TrieNode()
cur = cur.children[ord(ch) - ord('a')]
cur.saved = True
def search(self, word):
"""
Returns if the word is in the trie.
:type word: str
:rtype: bool
"""
cur = self.root
for ch in word:
if not cur.children[ord(ch) - ord('a')]:
return False
cur = cur.children[ord(ch) - ord('a')]
return cur.saved
def startsWith(self, prefix):
"""
Returns if there is any word in the trie
that starts with the given prefix.
:type prefix: str
:rtype: bool
"""
cur = self.root
for ch in prefix:
if not cur.children[ord(ch) - ord('a')]:
return False
cur = cur.children[ord(ch) - ord('a')]
return True
# Your Trie object will be instantiated and called as such:
# trie = Trie()
# trie.insert("somestring")
# trie.search("key")
###Java ####Contains bool
class TrieNode {
// Initialize your data structure here.
TrieNode[] children;
boolean fill;
public TrieNode() {
fill = false;
children = new TrieNode[26];
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode node = root;
for (char c: word.toCharArray()) {
if (node.children[c - 'a'] == null) {
node.children[c - 'a'] = new TrieNode();
}
node = node.children[c - 'a'];
}
node.fill = true;
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode node = root;
for (char c: word.toCharArray()) {
if (node.children[c - 'a'] == null) {
return false;
}
node = node.children[c - 'a'];
}
return node.fill;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode node = root;
for (char c: prefix.toCharArray()) {
if (node.children[c - 'a'] == null) {
return false;
}
node = node.children[c - 'a'];
}
return true;
}
}
// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
####Contains value
class TrieNode {
// Initialize your data structure here.
TrieNode[] children;
String val;
public TrieNode() {
val = "";
children = new TrieNode[26];
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode node = root;
for (char c: word.toCharArray()) {
if (node.children[c - 'a'] == null) {
node.children[c - 'a'] = new TrieNode();
}
node = node.children[c - 'a'];
}
node.val = word;
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode node = root;
for (char c: word.toCharArray()) {
if (node.children[c - 'a'] == null) {
return false;
}
node = node.children[c - 'a'];
}
return node.val.equals(word);
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode node = root;
for (char c: prefix.toCharArray()) {
if (node.children[c - 'a'] == null) {
return false;
}
node = node.children[c - 'a'];
}
return true;
}
}
// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
###Points
- Can be implemented thru dict or arr