Implementation of Trie data structure accommodating varying number of children I've used the Trie data structure to solve the following problem:


  Given an array of words, print all anagrams together. For example, if the given array is {"cat", "dog", "tac", "god", "act"}, then output may be "cat tac act dog god".


The following is my C++ implementation which can accommodate varying number of children. 

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;list&gt;
#include &lt;algorithm&gt;
using namespace std;

struct Trie_node_base
{
    vector&lt;Trie_node_base*&gt; child;
    int num_of_child;

    Trie_node_base(int n)
    {
        num_of_child = n;
        for(int i = 0; i &lt; n; ++i)
            child.push_back(NULL);
    }   
};

struct Trie_node : public Trie_node_base
{
    bool is_word;
    vector&lt;vector&lt;string&gt;::const_iterator&gt; head;

    Trie_node(int num):Trie_node_base(num), is_word(false){} 
};

class Trie
{
public:
    Trie(int n=26)
    {
        root = new Trie_node(n);
    }
    ~Trie()
    {
        destroy(root);
    }

    void insert_trie_node(const string&amp; w, vector&lt;string&gt;::const_iterator ind);
    Trie_node* get_root_node()
    {
        return root;
    }

private:
    Trie_node* root;
    void destroy(Trie_node* root);
};

void Trie::insert_trie_node(const string&amp; w, vector&lt;string&gt;::const_iterator ind)
{
    Trie_node* r = root;

    string::const_iterator runner = w.begin();
    for(; runner!=w.end(); ++runner)
    {
        char cur_char = *runner;
        int i = cur_char - 'a';

        if(r-&gt;child[i] == NULL)
        {
            r-&gt;child[i] = new Trie_node(r-&gt;num_of_child);
        }
        r = (Trie_node*)r-&gt;child[i];            
    }

    if(!r-&gt;is_word)
        r-&gt;is_word = true;
    r-&gt;head.push_back(ind);
}

void Trie::destroy(Trie_node* root)
{
    vector&lt;Trie_node_base*&gt;::iterator it = (root-&gt;child).begin();

    for(; it != (root-&gt;child).end(); ++it)
    {
        if(*it != NULL)
            destroy((Trie_node*)(*it));
    }

    delete root;
}


void traversal_trie(const vector&lt;string&gt;&amp; word_arr, Trie_node* r)
{
    size_t i;

    if(r-&gt;is_word)
    {        
        vector&lt;vector&lt;string&gt;::const_iterator&gt;::iterator it = (r-&gt;head).begin();
        for(; it != (r-&gt;head).end(); ++it)
        {
            cout &lt;&lt; *(*it) &lt;&lt; endl;
        }        
    }


    for(i = 0; i &lt; r-&gt;num_of_child; ++i)
    {
        if(r-&gt;child[i] != NULL)
            traversal_trie(word_arr, (Trie_node*)r-&gt;child[i]);
    }    
}

void cluster_anagrams(const vector&lt;string&gt;&amp; word_arr, size_t size)
{
    Trie* trie = new Trie(26);

    vector&lt;string&gt;::const_iterator it = word_arr.begin();       
    for(; it != word_arr.end(); ++it)
    {
        string cur_str = *it;
        sort(cur_str.begin(), cur_str.end());
        trie-&gt;insert_trie_node(cur_str, it);        
    }     
    traversal_trie(word_arr, trie-&gt;get_root_node());
}

int main()
{
    const char* word_arr[] = {"cat", "dog", "tac", "god", "act", "gdo"};
    size_t size = sizeof(word_arr) / sizeof(word_arr[0]);    
    vector&lt;string&gt; word_arr1(word_arr, word_arr+size);


    cluster_anagrams(word_arr1, size);    

    system("pause");
    return 0;
}
Your code does several things more complex than required. In no particular order:


What purpose does Trie_node_base serve? You don’t need a base class here since you only inherit from it once.
Worse, your base class doesn’t have a virtual destructor.
To create an array of size n, you use a loop and push_back. Much easier would be to use the proper initialiser:

Trie_node_base(int n) :
    num_of_child(n) ,
    child(n)
    { }

In fact, you generally fail to use initialisers, and instead re-assign values inside the constructor.
Consider proper naming. In the above code snippet, it should really be number_of_children and children (plural!).
The num_of_child member is redundant – the same information is stored in the vector.
The tree node root doesn’t need to, and shouldn’t be, a pointer.
In general, your use of pointers and manual memory management makes the code vastly more complex and brittle (and requires the existence of the destroy method). If you need to use pointers, use smart pointers (std::unique_ptr in this case).
Better yet, omit pointers entirely. They are utterly unnecessary here. Unfortunately though, you cannot use std::vector with an incomplete class (the standard screwed up here). But you can (and should!) use boost::container::vector. Thus your tree node implementation becomes:

struct Trie_node
{
    boost::container::vector&lt;Trie_node&gt; children;
    bool is_word;

    Trie_node(int n)
        : children(n)
        , is_word(false)
    { }
};



… There’s more but these are the essential points that will help reduce code complexity.