Bottom-up traversal of a Trie - GeeksforGeeks

28 Dec, 2021
Trie is an efficient information retrieval data structure. Using Trie, search complexities can be brought to an optimal limit (key length). Given a trie. The task is to print the characters in a bottom-up mannerBottom-up traversal:  
First print string of left most subtree(from bottom to top) then print string of second left subtree(from bottom to top) then print for third left subtree and so on.
It is similar to post-order traversal of a treeExample:  
    
Input :
                                          root
                                         /    \    
                                         a     t     
                                         |     |     
                                         n     h     
                                         | \   |   
                                         s  y  e     
                                         |     |  \ 
                                         w     i   r
                                         |     |   |
                                         e     r   e
                                         |        
                                         r        
Output : r, e, w, s, y, n, a, r, i, e, r, e, h, t  

Input : 
                                           root
                                          /     \    
                                         c       t     
                                         |       |     
                                         a       h     
                                         | \     |     
                                         l  t    e     
                                         |       |  \ 
                                         l       i   r
                                         | \     |   |
                                         e  i    r   e
                                         |  |
                                         r  n
                                            |
                                            g 

Output : r, e, g, n, i, l, l, t, a, c, r, i, e, r, e, h, t
 
Explanation : In the first example, the root has two parts. First part contains strings: “answer” and “any”. the second part with strings “their” and “there”.  
Now first we got to left subtree containing strings “answer” and “any” which separates by character ‘n’. Now ‘n’ separates two-part of characters ‘s’, ‘w’, ‘e’, ‘r’ and ‘y’. so print ‘s’, ‘w’, ‘e’, ‘r’ in reverse order then print ‘y’ and go up and print ‘n'(which separates string) then go up and print ‘a’. Now first left subtree has printed in bottom up manner ‘r’, ‘e’, ‘w’, ‘s’, ‘y’, ‘n’, ‘a’.
Do the same thing for another subtree of the root which contains strings “their” and “there” which is separated by character ‘e’.
Approach : The idea to do this is to start traversing from the root node of the trie, whenever we find a NON-NULL child node, we recursively move ahead when we get “NULL” we return simply and print the value of current node and same goes un till we find the node which is a leaf node, which actually marks the end of the string.Below is the implementation of the above approach : 
C++
Java
Python3
C#
Javascript
// CPP program to traverse in bottom up manner#include <bits/stdc++.h>using namespace std;#define CHILDREN 26#define MAX 100  // Trie nodestruct trie {    trie* child[CHILDREN];    // endOfWord is true if the node represents    // end of a word    bool endOfWord;}; // Function will return the new node(initialized to NULLs)trie* createNode(){    trie* temp = new trie();    temp->endOfWord = false;    for (int i = 0; i < CHILDREN; i++) {        // Initialise all child to the null, initially        temp->child[i] = NULL;    }         // Return newly created node    return temp;}// Function will insert the string in a trie recursivelyvoid insertRecursively(trie* itr, string str, int i){    if (i < str.length()) {        int index = str[i] - 'a';        if (itr->child[index] == NULL) {            // Assigning a newly created node            itr->child[index] = createNode();        }        // Recursive call for insertion        // of a string in a trie        insertRecursively(itr->child[index], str, i + 1);    }    else {        // Make the endOfWord true which represents        // the end of string        itr->endOfWord = true;    }}// Function call to insert a stringvoid insert(trie* itr, string str){    // Function call with necessary arguments    insertRecursively(itr, str, 0);}// Function to print traverse// the tree in bottom to top mannervoid printPattern(trie* itr){    // Base condition    if (itr == NULL)        return;     // Loop for printing t a value of character    for (int i = 0; i < CHILDREN; i++) {                 // Recursive call for print pattern        printPattern(itr->child[i]);        if (itr->child[i] != NULL) {            char ch = (i + 97);            cout << ch << ", "; // Print character        }    }} // Driver codeint main(){    trie* root = createNode();         // Function to insert a string    insert(root, "their");    insert(root, "there");    insert(root, "answer");    insert(root, "any");              // Function call for printing a pattern    printPattern(root);     return 0;}
// Java program to traverse in bottom up mannerpublic class Main{    static int CHILDREN = 26;         // Trie node    static class trie {                 public boolean endOfWord;        public trie[] child;                 public trie()        {            endOfWord = false;                         // endOfWord is true if the node represents            // end of a word            child = new trie[CHILDREN];        }    }         // Function will return the new node(initialized to NULLs)    static trie createNode()    {        trie temp = new trie();        temp.endOfWord = false;        for (int i = 0; i < CHILDREN; i++)        {                         // Initialise all child to the null, initially            temp.child[i] = null;        }                    // Return newly created node        return temp;    }         // Function will insert the string in a trie recursively    static void insertRecursively(trie itr, String str, int i)    {        if (i < str.length()) {            int index = str.charAt(i) - 'a';            if (itr.child[index] == null)            {                                 // Assigning a newly created node                itr.child[index] = createNode();            }                         // Recursive call for insertion            // of a string in a trie            insertRecursively(itr.child[index], str, i + 1);        }        else        {                         // Make the endOfWord true which represents            // the end of string            itr.endOfWord = true;        }    }         // Function call to insert a string    static void insert(trie itr, String str)    {                 // Function call with necessary arguments        insertRecursively(itr, str, 0);    }         // Function to print traverse    // the tree in bottom to top manner    static void printPattern(trie itr)    {                 // Base condition        if (itr == null)            return;                // Loop for printing t a value of character        for (int i = 0; i < CHILDREN; i++) {                            // Recursive call for print pattern            printPattern(itr.child[i]);            if (itr.child[i] != null) {                char ch = (char)(i + 97);                System.out.print(ch + ", "); // Print character            }        }    }         public static void main(String[] args) {        trie root = createNode();                // Function to insert a string        insert(root, "their");        insert(root, "there");        insert(root, "answer");        insert(root, "any");                                // Function call for printing a pattern        printPattern(root);    }} // This code is contributed by divyesh072019.
# Python3 program to traverse in bottom up mannerCHILDREN = 26MAX = 100 # Trie nodeclass trie:     def __init__(self):        self.child = [None for i in range(CHILDREN)]                 # endOfWord is true if the node represents        # end of a word        self.endOfWord = False # Function will return the new node(initialized to NULLs)def createNode():     temp = trie()    return temp # Function will insert the string in a trie recursivelydef insertRecursively(itr, str, i):     if (i < len(str)):        index = ord(str[i]) - ord('a')                 if (itr.child[index] == None):                       # Assigning a newly created node            itr.child[index] = createNode()                 # Recursive call for insertion        # of a string in a trie        insertRecursively(itr.child[index], str, i + 1)         else:        # Make the endOfWord true which represents        # the end of string        itr.endOfWord = True     # Function call to insert a stringdef insert(itr, str):     # Function call with necessary arguments    insertRecursively(itr, str, 0) # Function to print traverse# the tree in bottom to top mannerdef printPattern(itr):     # Base condition    if(itr == None):        return     # Loop for printing t a value of character    for i in range(CHILDREN):         # Recursive call for print pattern        printPattern(itr.child[i])        if (itr.child[i] != None):            ch = chr(i + 97)                         # Print character            print(ch,end=', ')             # Driver codeif __name__=='__main__':     root = createNode()     # Function to insert a string    insert(root, "their")    insert(root, "there")    insert(root, "answer")    insert(root, "any")     # Function call for printing a pattern    printPattern(root)     # This code is countributed by rutvik_56
// C# program to traverse in bottom up mannerusing System;class GFG {         static int CHILDREN = 26;         // Trie node    class trie {                public bool endOfWord;        public trie[] child;                public trie()        {            endOfWord = false;                       // endOfWord is true if the node represents            // end of a word            child = new trie[CHILDREN];        }    }          // Function will return the new node(initialized to NULLs)    static trie createNode()    {        trie temp = new trie();        temp.endOfWord = false;        for (int i = 0; i < CHILDREN; i++)        {                       // Initialise all child to the null, initially            temp.child[i] = null;        }                  // Return newly created node        return temp;    }       // Function will insert the string in a trie recursively    static void insertRecursively(trie itr, string str, int i)    {        if (i < str.Length) {            int index = str[i] - 'a';            if (itr.child[index] == null)            {                               // Assigning a newly created node                itr.child[index] = createNode();            }                       // Recursive call for insertion            // of a string in a trie            insertRecursively(itr.child[index], str, i + 1);        }        else        {                       // Make the endOfWord true which represents            // the end of string            itr.endOfWord = true;        }    }       // Function call to insert a string    static void insert(trie itr, string str)    {               // Function call with necessary arguments        insertRecursively(itr, str, 0);    }       // Function to print traverse    // the tree in bottom to top manner    static void printPattern(trie itr)    {               // Base condition        if (itr == null)            return;              // Loop for printing t a value of character        for (int i = 0; i < CHILDREN; i++) {                          // Recursive call for print pattern            printPattern(itr.child[i]);            if (itr.child[i] != null) {                char ch = (char)(i + 97);                Console.Write(ch + ", "); // Print character            }        }    }   static void Main() {    trie root = createNode();          // Function to insert a string    insert(root, "their");    insert(root, "there");    insert(root, "answer");    insert(root, "any");                // Function call for printing a pattern    printPattern(root);  }} // This code is contributed by mukesh07.
<script>    // Javascript program to traverse in bottom up manner    let CHILDREN = 26;         // Trie node    class trie    {        constructor()        {                    // endOfWord is true if the node represents           // end of a word           this.child = new Array(CHILDREN);           this.endOfWord = false;        }    }         // Function will return the new node(initialized to NULLs)    function createNode()    {        let temp = new trie();        temp.endOfWord = false;        for (let i = 0; i < CHILDREN; i++)        {                        // Initialise all child to the null, initially            temp.child[i] = null;        }                   // Return newly created node        return temp;    }        // Function will insert the string in a trie recursively    function insertRecursively(itr, str, i)    {        if (i < str.length) {            let index = str[i].charCodeAt() - 'a'.charCodeAt();            if (itr.child[index] == null)            {                                // Assigning a newly created node                itr.child[index] = createNode();            }                        // Recursive call for insertion            // of a string in a trie            insertRecursively(itr.child[index], str, i + 1);        }        else        {                        // Make the endOfWord true which represents            // the end of string            itr.endOfWord = true;        }    }        // Function call to insert a string    function insert(itr, str)    {                // Function call with necessary arguments        insertRecursively(itr, str, 0);    }        // Function to print traverse    // the tree in bottom to top manner    function printPattern(itr)    {                // Base condition        if (itr == null)            return;               // Loop for printing t a value of character        for (let i = 0; i < CHILDREN; i++) {                           // Recursive call for print pattern            printPattern(itr.child[i]);            if (itr.child[i] != null) {                let ch = String.fromCharCode(i + 97);                document.write(ch + ", "); // Print character            }        }    }         let root = createNode();           // Function to insert a string    insert(root, "their");    insert(root, "there");    insert(root, "answer");    insert(root, "any");                  // Function call for printing a pattern    printPattern(root);         // This code is contributed by suresh07.</script>
Output:  
 r, e, w, s, y, n, a, e, r, e, r, e, i, h, t,
 
Akanksha_Rai
shubham_singh
rutvik_56
sumitgumber28
mukesh07
kapoorsagar226
rkbhola5
divyesh072019
suresh07
Trie
Advanced Data Structure
Recursion
Recursion
Trie
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Ordered Set and GNU C++ PBDS
2-3 Trees | (Search, Insert and Deletion)
Extendible Hashing (Dynamic approach to DBMS)
Suffix Array | Set 1 (Introduction)
Difference between Backtracking and Branch-N-Bound technique
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction