Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print all words in a Trie?

I am trying to create a Trie Implementation in C++. I cannot figure out how to print all words stored in the Trie.

This is how I've implemented the TrieNode.

struct TrieNode{
  bool isWord;
  int data; //Number of times Word Occured
  TrieNode *Child[ALPHABET_SIZE]; //defined as 26
};

I know I could store a pointer to the parent node, Depth-First Search for all nodes where isWord==True and recursively print each word from those nodes.

But I'm wondering is there a way to print out each word in the Trie with my implementation of a TrieNode.

Thanks for any help.

like image 483
theUser Avatar asked Dec 03 '12 14:12

theUser


2 Answers

Here is a reasonably efficient version of Konrad Rudolph, without assuming data is a character. I also removed the O(n^2) total memory allocated in Konrad's version at the cost of using a std::string&. The idea is to pass down the prefix and modify it at each recursion, pushing characters onto the end and poping it afterwards, ends up being slightly more efficient than copying it madly.

void traverse(std::string& prefix, TrieNode const& node) {
  if (node.isWord)
    print(prefix);

  for (char index = 0; index < ALPHABET_SIZE; ++index) {
    char next = 'a'+index;
    TrieNode const* pChild = node.Child[index];
    if (pChild) {
      prefix.push_back(next);
      traverse(prefix, *pChild);
      prefix.pop_back();
    }
  }
}
like image 153
Yakk - Adam Nevraumont Avatar answered Sep 19 '22 12:09

Yakk - Adam Nevraumont


You don’t need your parent node, your code readily accommodates traversal via recursion. Pseudo-code:

void traverse(string prefix, TrieNode const& n) {
    prefix += static_cast<char>(n.data);

    if (n.isWord)
        print(prefix);

    for (auto const next : n.Child)
        if (next)
            traverse(prefix, *next);
}

This is more or less valid C++. Just define print appropriately.

EDIT In response to Yakk’s comment and your clarification, here’s a version which doesn’t assume that data contains the current character (bad slip on my part!):

void traverse(string const& prefix, TrieNode const& n) {
    if (n.isWord)
        print(prefix);

    for (std::size_t i = 0; i < ALPHABET_SIZE; ++i)
        if (n.child[i])
            traverse(prefix + ('a' + i), *n.child[i]);
}

I’ll leave the more efficient implementation to Yakk’s answer.

like image 34
Konrad Rudolph Avatar answered Sep 18 '22 12:09

Konrad Rudolph