tire tree 实现
定义:(wiki)
In computer science, a trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is a kind of search tree—an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings. Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are not necessarily associated with every node. Rather, values tend only to be associated with leaves, and with some inner nodes that correspond to keys of interest. For the space-optimized presentation of prefix tree, see compact prefix tree.
In the example shown, keys are listed in the nodes and values below them. Each complete English word has an arbitrary integer value associated with it. A trie can be seen as a tree-shapeddeterministic finite automaton. Each finite language is generated by a trie automaton, and each trie can be compressed into a deterministic acyclic finite state automaton.
Though tries are usually keyed by character strings, they need not be. The same algorithms can be adapted to serve similar functions of ordered lists of any construct, e.g. permutations on a list of digits or shapes. In particular, a bitwise trie is keyed on the individual bits making up any fixed-length binary datum, such as an integer or memory address.
LeetCode :208. Implement Trie (Prefix Tree)
Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
Subscribe to see which companies asked this question.
class Trie {
public:
/** Initialize your data structure here. */
Trie() {
root = new Trie_node;
}
/** Inserts a word into the trie. */
void insert(string word) {
if(root == NULL) root = new Trie_node;
int position=0; // indexes letters of new Trie
char next_char;
Trie_node * location = root; // moves through the trie
while(location != NULL &&
(next_char = key_letter(word, position)) !=0){
//Terminate for a NULL location or a blank in the target
int next_position = alphabetic_order(next_char);
if(location->branch[next_position] == NULL){
location->branch[next_position] = new Trie_node;
}
location = location->branch[next_position];
position++;
}
if(location->data == NULL) {
location->data = new string(word);
}
}
/** Returns if the word is in the trie. */
bool search(string word) {
int position = 0;
char next_char;
Trie_node * location = root;
while(location != NULL &&
(next_char = key_letter(word, position)) != 0){
location = location->branch[alphabetic_order(next_char)];
//move down the appropriate branch of the trie
position++; //next character of the target
}
if(location!=NULL && location->data != NULL){
return true;
}
else return false;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
int position = 0;
char next_char;
Trie_node * location = root;
while(location != NULL &&
(next_char = key_letter(prefix, position)) != 0){
location = location->branch[alphabetic_order(next_char)];
//move down the appropriate branch of the trie
position++; //next character of the target
}
if(location != NULL)return true;
else return false;
}
struct Trie_node {
string *data;
Trie_node * branch[26];
Trie_node(){
data = NULL;
for(int i=0;i<26;i++){
branch[i]=NULL;
}
}//all null
};
private:
Trie_node * root;
int alphabetic_order(char symbol){
return (int)symbol - 97;
}
char key_letter(string data,int position){
if(data.length()<position) return 0;
else return data[position];
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* bool param_2 = obj.search(word);
* bool param_3 = obj.startsWith(prefix);
*/