c_cpp 实施一个特里
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 实施一个特里相关的知识,希望对你有一定的参考价值。
/*
Advantages over a Hash Table
A trie can be used to replace a hash table, over which it has the following advantages:
Looking up data in a trie is faster in the worst case, O(m) time (where m is the length of a search string), compared to an imperfect hash table. An imperfect hash table can have key collisions. A key collision is the hash function mapping of different keys to the same position in a hash table. The worst-case lookup speed in an imperfect hash table is O(N) time, but far more typically is O(1), with O(m) time spent evaluating the hash.
There are no collisions of different keys in a trie.
Buckets in a trie which are analogous to hash table buckets that store key collisions are necessary only if a single key is associated with more than one value.
There is no need to provide a hash function or to change hash functions as more keys are added to a trie.
A trie can provide an alphabetical ordering of the entries by key.
Tries do have some drawbacks as well:
Tries can be slower in some cases than hash tables for looking up data, especially if the data is directly accessed on a hard disk drive or some other secondary storage device where the random-access time is high compared to main memory. ( Not in the Gibson case )
Some keys, such as floating point numbers, can lead to long chains and prefixes that are not particularly meaningful. Nevertheless a bitwise trie can handle standard IEEE single and double format floating point numbers. ( Not in the Gibson case )
Some tries can require more space than a hash table, as memory may be allocated for each character in the search string, rather than a single chunk of memory for the whole entry, as in most hash tables.
*/
struct Node {
char value; // the character value (a-z)
bool is_end; // whether this node completes a word
Node* children[26]; // represents the 26 letters in the alphabet
Node(char v) : value (v), is_end(false) {
for(int i=0; i<26; i++)
children[i] = NULL;
}
};
class Trie {
public:
Trie() {
root = new Node(' ');
root->is_end = true;
}
Node* getRoot() { return root; }
void add_word(const string& word) {
Node *curr = root;
for(int i=0; i<word.size(); i++) {
int j = word[i] - 'a';
if(curr->children[j] != NULL) {
curr = curr->children[j];
}
else {
Node *n = new Node(word[i]);
curr->children[j] = n;
curr = n;
}
if(i == word.size() - 1)
curr->is_end = true;
}
}
bool search_word(const string& word) {
Node* curr = root;
for(int i=0; i<word.size(); i++) {
int j = word[i] - 'a';
if(curr->children[j] != NULL)
curr = curr->children[j];
else
return false;
// make sure the last node is marked as an end
if(i == word.size()-1 && curr->is_end == false)
return false;
}
return true;
}
void delete_word(const string& word) {
Node *curr = root;
for(int i=0; i<word.size(); i++) {
int j = word[i] - 'a';
if(curr->children[j] != NULL)
curr = curr->children[j];
else //the current node doesn't have the current character which means the word is not in the trie
return;
if(i == word.size()-1 && curr->is_end == true)
curr->is_end = false;
}
}
private:
Node *root;
};
以上是关于c_cpp 实施一个特里的主要内容,如果未能解决你的问题,请参考以下文章