Search code examples
cprintingtrie

How to traverse a trie longitudinally?


i have a Trie and several functions modifiing it.

typedef struct node *pnode;

typedef struct node
{
    int element;
    pnode next;//same level, other element
    pnode subtree;//next level
} node;

Now, in order to debug and/or test my functions, I need to print out the tries.

I tried it recursively, but I cannot get the first level, than the second level...

What's a good way to do it?


Solution

  • Instead of using stack (or emulating it with recurrence), you have to use queue.

    http://en.wikipedia.org/wiki/Breadth-first_search