Блог пользователя Jim_Moriarty_

Автор Jim_Moriarty_, история, 4 года назад, По-английски

please suggest me some articles that explain implementation of trie using 2D arrays. Not using pointers. I Have found one article but I am unable to understand it from there. Please do help. Thanks in advance.

  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +11 Проголосовать: не нравится

Here's a pretty neat code I use.

#define MAX_SUM_S 1000006

int tr[26][MAX_SUM_S] ,root ,nn;
void add(string&s ,int&p=root ,int i=0){
    if(!p) p = ++nn;
    if(i == s.size())
        return;
    add(s ,tr[s[i]-'a'][p] ,i+1);
}
bool check(string&s ,int&p=root ,int i=0){
    if(!p) return false;
    if(i == s.size())
        return true;
    return check(s ,tr[s[i]-'a'][p] ,i+1);
}

Features:

  • MAX_SUM_S is number of nodes in the tree -conveniently, the sum of all string sizes inserted to the trie.
  • Simply add a string by calling add(str), and check for a prefix by check(str).
  • To store more information (end of a string bool; number of strings with same prefix; dp; etc) for each node, create an new array of size MAX_SUM_S or increase the size of first dimension of tr.
  • You can have multiple trie trees by maintaining an array of roots. e.g. add(str ,root[x])-initially all roots are set to 0.
  • It can easily be converted to persistent trie using an array of roots for every version.
»
4 года назад, # |
  Проголосовать: нравится -10 Проголосовать: не нравится

HERE IS A SIMPLE AND EASY TO UNDERSTAND IMPLEMENTATION USING MAP IN C++.

struct trie
{   map<char,trie*> children;
    int prefixes;
    bool endofword;
    trie()
    {prefixes = 0;endofword = false;}
};

void insert(trie*root , string word )
{trie* current  = root;
    for(int i=0; i<word.size(); i++)
    {
        char ch = word[i];
        trie* node = current->children[ch];
        if(!node)
        {
            node = new trie();
            current->children[ch] = node;
        }
        current = node;
        current->prefixes++;
    }
    current->endofword = true;
}


bool search(trie* root , string word)
{
    trie* current = root;
    for(int i=0; i<word.size(); i++)
    {
        char ch = word[i];
        trie* node = current->children[ch];
        if(!node)
            return false;
        current = node;
    }
    return current->endofword;
}

It is mainly used for string related queries. It is more space efficient.

»
4 года назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

Here is a simple code written in C++ that i use for trie. This code insert string into trie.

int tri[1000005][26]; //Total char in input file,Number of distinct char
bool flag[1000005]; //Indicate where string finishes
int id=1;

int main()
{
    string str;
    cin >> str;
    int r=1;
    REP(i,str.size())
    {
        int x=str[i]-'a'; // It maybe '0'/'A'. According to input type
        if(!tri[r][x])
        {
            tri[r][x]=++id;
        }
        r=tri[r][x];
    }
    flag[r]=true;

    return 0;
}