Types of Tries

A trie is a tree-like information retrieval data structure whose nodes store the letters of an alphabet. It is also known as a digital tree or a radix tree or prefix tree. Tries are classified into three categories:

  1. Standard Trie
  2. Compressed Trie
  3. Suffix Trie

Standard Trie A standard trie have the following properties:

  1. A Standard Trie has the below structure:
    class Node {
       
        // Array to store the nodes of a tree
        Node[] children = new Node[26];
    
        // To check for end of string
        boolean isWordEnd;
    }
    
  2. It is an ordered tree like data structure.
  3. Each node(except the root node) in a standard trie is labeled with a character.
  4. The children of a node are in alphabetical order.
  5. Each node or branch represents a possible character of keys or words.
  6. Each node or branch may have multiple branches.
  7. The last node of every key or word is used to mark the end of word or node.

Below is the illustration of the Standard Trie:

Compressed Trie A Compressed trie have the following properties:



  1. A Compressed Trie has the below structure:
    class Node {
    
        // Array to store the nodes of tree
        Node[] children = new Node[26];
    
        // To store the edgeLabel
        StringBuilder[] edgeLabel = new StringBuilder[26];
    
        // To check for end of string
        boolean isEnd;
    }
    
  2. A Compressed Trie is an advanced version of the standard trie.
  3. Each nodes(except the leaf nodes) have atleast 2 children.
  4. It is used to achieve space optimization.
  5. To derive a Compressed Trie from a Standard Trie, compression of chains of redundant nodes is performed.
  6. It consists of grouping, re-grouping and un-grouping of keys of characters.
  7. While performing the insertion operation, it may be required to un-group the already grouped characters.
  8. While performing the deletion operation, it may be required to re-group the already grouped characters.
  9. A compressed trie T storing s strings(keys) has s external nodes and O(s) total number of nodes.

Below is the illustration of the Compressed Trie:

Suffix Trie A Suffix trie have the following properties:

  1. A Compressed Trie has the below structure:
    struct SuffixTreeNode { 
    
        // Array to store the nodes
        struct SuffixTreeNode *children[256]; 
       
        //pointer to other node via suffix link 
        struct SuffixTreeNode *suffixLink; 
       
        // (start, end) interval specifies the edge,
        // by which the node is connected to its 
        // parent node
        int start; 
        int *end; 
       
        // For leaf nodes, it stores the index of 
        // Suffix for the path  from root to leaf
        int suffixIndex; 
    }
    
  2. A Suffix Trie is an advanced version of the compressed trie.
  3. The most common application of suffix trie is Pattern Matching.
  4. While performing the insertion operation, both the word and its suffixes are stored.
  5. A suffix trie is also used in word matching and prefix matching.
  6. To generate a suffix trie, all the suffixes of given string are considered as individual words.
  7. Using the suffixes, compressed trie is built.

Below is the illustration of the Suffix Trie:

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.