Traversing a trie to get all words?

To fix this, you need to pass some more context into your recursive sub. Something like this: sub process { my ($prefix, $trie) = @_; for my $letter (sort keys %$trie) { if ( @{ $trie->{$letter} } ) { for my $branch (@{ $trie->{$letter} }) { process("$prefix$letter", $branch); } } else { print "$prefix$letter\n"; } } } print("\n List of words\n"); process('', \%mainhash) This doesn't print arc, because you provide no way to tell in your datastructure that arc is a word but e.g. Boi is not. The value for each letter needs to provide two things: a boolean indicator that this is the end of a word, and a list of possible following letters and their sub-trie.

To fix this, you need to pass some more context into your recursive sub. Something like this: sub process { my ($prefix, $trie) = @_; for my $letter (sort keys %$trie) { if ( @{ $trie->{$letter} } ) { for my $branch (@{ $trie->{$letter} }) { process("$prefix$letter", $branch); } } else { print "$prefix$letter\n"; } } } print("\n List of words\n"); process('', \%mainhash); This doesn't print arc, because you provide no way to tell in your datastructure that arc is a word but e.g. Boi is not. The value for each letter needs to provide two things: a boolean indicator that this is the end of a word, and a list of possible following letters and their sub-trie.

That works fine, I missed to append the prefix everytime before recursing. I figured out that am missing an end of the word flag. Thanks – TGV May 18 at 11:36.

I have written Perl code to actually create a Trie datastructure given a set of words in an array. Now I have problems traversing and printing the words. Also pasted the Dumper output of the Datastructure created.

The final set of words after traversal doesn't seem to be right since the traversal logic is certainly missing something. But the trie creation is fine and works fast. Can someone help me here?

If you see the first word in the output.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions