(Translated by https://www.hiragana.jp/)
Trie | (Display Content) - GeeksforGeeks
Open In App

Trie | (Display Content)

Last Updated : 05 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Trie is an efficient information retrieval data structure. In our previous post on trie we have discussed about basics of trie and how to insert and search a key in trie. In this post we will discuss about displaying all of the content of a trie. That is, to display all of the keys present in the Trie.
Examples: 

Input: If Trie is      root
                    /   \    \
                    t   a     b
                    |   |     |
                    h   n     y
                    |   |  \  |
                    e   s  y  e
                 /  |   |
                 i  r   w
                 |  |   |
                 r  e   e
                        |
                        r
Output: Contents of Trie:
        answer
        any
        bye
        their
        there 

The idea to do this is to start traversing from the root node of trie, whenever we find a NON-NULL child node, we add parent key of child node in the "string str" at the current index(level) and then recursively call the same process for the child node and same goes on till we find the node which is a leafnode, which actually marks the end of the string. 

Below is the C++ implementation of the above idea: 

CPP
Java Python3 C# JavaScript

Output
Content of Trie: 
a
answer
any
by
bye
the
their
there

Time Complexity: O(N*M)  where N is the number of keys to be inserted in the Trie and M is the maximum length of a key.
Auxiliary Space: O(N*M)


NOTE: The above algorithm displays the content of Trie in Lexicographically Sorted order.
Some useful applications of Trie are: 
 


 


Similar Reads