#include "common.h"
#include "toy-dict.h"


typedef struct {
    char c;
    unsigned long index;
    unsigned int level;
} STACK_NODE;

int main(int ac, char **av)
{
    char word[BUF_LEN << 10], c;
    unsigned int level;
    int i;
    unsigned long top = 1, index, count;
    STACK_NODE *stack, *sptr;

    if (NULL == (stack = Malloc(100, STACK_NODE)))
        ERR_MESG("trie_dfs: out of memory");
    sptr = stack;
    sptr->c = '\0';
    sptr->index = 0;
    sptr->level = 0;
    while (top > 0) {
        sptr = stack + top - 1;
        top--;
        c = sptr->c;
        index = sptr->index;
        level = sptr->level;
        count = dict[index][NUM_SYMS];
        if (level) {
            assert(level > 0 && c < NUM_SYMS);
            word[level - 1] = chars[c];
        }
        if (count > 0) {
            word[level] = '\0';
            printf("%s\n", word);
        }
        for (i = NUM_SYMS - 1; i >= 0; i--) {
            if (dict[index][i] > 0) {
                sptr = stack + top;
                sptr->c = i;
                sptr->index = dict[index][i];
                sptr->level = level + 1;
                top++;
            }
        }
    }

    return 0;
}
