Skip to content
Related Articles

Related Articles

Improve Article

Sorting array of strings (or words) using Trie

  • Difficulty Level : Medium
  • Last Updated : 17 Sep, 2021
Geek Week

Given an array of strings, print them in alphabetical (dictionary) order. If there are duplicates in input array, we need to print them only once.
Examples: 
 

Input : "abc", "xy", "bcd"
Output : abc bcd xy         

Input : "geeks", "for", "geeks", "a", "portal", 
        "to", "learn", "can", "be", "computer", 
        "science", "zoom", "yup", "fire", "in", "data"
Output : a be can computer data fire for geeks
         in learn portal science to yup zoom

 

Trie is an efficient data structure used for storing data like strings. To print the string in alphabetical order we have to first insert in the trie and then perform preorder traversal to print in alphabetical order.
 

CPP




// C++ program to sort an array of strings
// using Trie
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_CHAR = 26;
 
struct Trie {
 
    // index is set when node is a leaf
    // node, otherwise -1;
    int index;
 
    Trie* child[MAX_CHAR];
 
    /*to make new trie*/
    Trie()
    {
        for (int i = 0; i < MAX_CHAR; i++)
            child[i] = NULL;
        index = -1;
    }
};
 
/* function to insert in trie */
void insert(Trie* root, string str, int index)
{
    Trie* node = root;
 
    for (int i = 0; i < str.size(); i++) {
 
        /* taking ascii value to find index of
          child node */
        char ind = str[i] - 'a';
 
        /* making new path if not already */
        if (!node->child[ind])
            node->child[ind] = new Trie();
 
        // go to next node
        node = node->child[ind];
    }
 
    // Mark leaf (end of word) and store
    // index of word in arr[]
    node->index = index;
}
 
/* function for preorder traversal */
bool preorder(Trie* node, string arr[])
{
    if (node == NULL)
        return false;
 
    for (int i = 0; i < MAX_CHAR; i++) {
        if (node->child[i] != NULL) {
 
            /* if leaf node then print key*/
            if (node->child[i]->index != -1)
                cout << arr[node->child[i]->index] << endl;
 
            preorder(node->child[i], arr);
        }
    }
}
 
void printSorted(string arr[], int n)
{
    Trie* root = new Trie();
 
    // insert all keys of dictionary into trie
    for (int i = 0; i < n; i++)
        insert(root, arr[i], i);
 
    // print keys in lexicographic order
    preorder(root, arr);
}
 
// Driver code
int main()
{
    string arr[] = { "abc", "xy", "bcd" };
    int n = sizeof(arr) / sizeof(arr[0]);
    printSorted(arr, n);
    return 0;
}

Java




// Java program to sort an array of strings using Trie
 
// Author : Rohit Jain
// GFG user_id : @rj03012002
 
import java.util.*;
 
class GFG {
 
    // Alphabet size
    static final int MAX_CHAR = 26;
 
    // trie node
    static class Trie {
 
        // index is set when node is a leaf
        // node, otherwise -1;
        int index;
 
        Trie child[] = new Trie[MAX_CHAR];
 
        /*to make new trie*/
        Trie()
        {
 
            for (int i = 0; i < MAX_CHAR; i++)
                child[i] = null;
            index = -1;
        }
    }
 
    /* function to insert in trie */
    static void insert(Trie root, String str, int index)
    {
 
        Trie node = root;
 
        for (int i = 0; i < str.length(); i++) {
            /* taking ascii value to find index of
          child node */
            int ind = str.charAt(i) - 'a';
 
            /* making new path if not already */
            if (node.child[ind] == null)
                node.child[ind] = new Trie();
 
            // go to next node
            node = node.child[ind];
        }
 
        // Mark leaf (end of word) and store
        // index of word in arr[]
        node.index = index;
    }
 
    /* function for preorder traversal */
    static boolean preorder(Trie node, String arr[])
    {
 
        if (node == null) {
 
            return false;
        }
 
        for (int i = 0; i < MAX_CHAR; i++) {
 
            if (node.child[i] != null) {
 
                /* if leaf node then print key*/
                if (node.child[i].index != -1) {
 
                    System.out.print(
                        arr[node.child[i].index] + " ");
                }
 
                preorder(node.child[i], arr);
            }
        }
        return false;
    }
 
    static void printSorted(String arr[], int n)
    {
 
        Trie root = new Trie();
 
        // insert all keys of dictionary into trie
        for (int i = 0; i < n; ++i) {
 
            insert(root, arr[i], i);
        }
 
        // print keys in lexicographic order
        preorder(root, arr);
    }
 
    public static void main(String[] args)
    {
 
        String arr[] = { "abc", "xy", "bcd" };
 
        int n = arr.length;
 
        printSorted(arr, n);
    }
}

Output: 
 

abc bcd xy   

This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :