Representation of Trie Node

Every Trie node consists of a character pointer array or hashmap and a flag to represent if the word is ending at that node or not. But if the words contain only lower-case letters (i.e. a-z), then we can define Trie Node with an array instead of a hashmap.

C++
struct TrieNode {
    struct TrieNode* children[ALPHABET_SIZE];

    // This will keep track of number of strings that are
    // stored in the Trie from root node to any Trie node.
    int wordCount = 0;
};
Java
public class TrieNode {
    public TrieNode[] children;
    public int wordCount;

    public TrieNode()
    {
        children = new TrieNode[26];
        // This will keep track of number of strings that
        // are stored in the Trie from root node to any Trie
        // node.
        wordCount = 0;
    }
}
Python
# Python code
class TrieNode:

    # Trie node class
    def _init_(self):
        self.children = [None for _ in range(26)]

        # This will keep track of number of strings that are
        # stored in the Trie from root node to any Trie node.
        self.wordCount = 0
        
        # This code is contributed by ishankhandelwals.
C#
// Include namespace system
using System;


public class TrieNode
{
    public TrieNode[] children;
    public int wordCount;
    public TrieNode()
    {
        this.children = new TrieNode[26];
        // This will keep track of number of strings that
        // are stored in the Trie from root node to any Trie
        // node.
        this.wordCount = 0;
    }
}
Javascript
// JS code
class TrieNode {

    constructor() 
    {
        this.children = new Array(26);
        // This will keep track of number of strings that are
        // stored in the Trie from root node to any Trie node.
        this.wordCount = 0;
    }
}

// This code is contributed by ishankhandelwals.

Trie Data Structure Tutorial

The trie data structure, also known as a prefix tree, is a tree-like data structure used for efficient retrieval of key-value pairs. It is commonly used for implementing dictionaries and autocomplete features, making it a fundamental component in many search algorithms. In this article, we will explore all about Trie data structures in detail.

Trie Data Structure

Table of Content

  • What is Trie Data Structure?
  • What is need of Trie Data Structure?
  • Advantages of Trie Data Structure over a Hash Table
  • Properties of a Trie Data Structure
  • How does Trie Data Structure work?
  • Representation of Trie Node
  • Basic Operations on Trie Data Structure
  • Insertion in Trie Data Structure
  • Searching in Trie Data Structure
  • Deletion in Trie Data Structure
  • Implement Trie Data Structure?
  • Complexity Analysis of Trie Data Structure
  • Applications of Trie data structure
  • Advantages of Trie data structure
  • Disadvantages of Trie data structure
  • Top Interview problems on Trie data structure

Similar Reads

What is Trie Data Structure?

Trie data structure is defined as a Tree based data structure that is used for storing some collection of strings and performing efficient search operations on them. The word Trie is derived from reTRIEval, which means finding something or obtaining it....

What is need of Trie Data Structure?

A Trie data structure is used for storing and retrieval of data and the same operations could be done using another data structure which is Hash Table but Trie data structure can perform these operations more efficiently than a Hash Table. Moreover, Trie has its own advantage over the Hash table. A Trie data structure can be used for prefix-based searching whereas a Hash table can’t be used in the same way....

Advantages of Trie Data Structure over a Hash Table:

The A trie data structure has the following advantages over a hash table:...

Properties of a Trie Data Structure

Below are some important properties of the Trie data structure:...

How does Trie Data Structure work?

Trie data structure can contain any number of characters including alphabets, numbers, and special characters. But for this article, we will discuss strings with characters a-z. Therefore, only 26 pointers need for every node, where the 0th index represents ‘a’ and the 25th index represents ‘z’ characters....

Representation of Trie Node:

Every Trie node consists of a character pointer array or hashmap and a flag to represent if the word is ending at that node or not. But if the words contain only lower-case letters (i.e. a-z), then we can define Trie Node with an array instead of a hashmap....

Basic Operations on Trie Data Structure:

InsertionSearchDeletion...

1. Insertion in Trie Data Structure:

This operation is used to insert new strings into the Trie data structure. Let us see how this works:...

2. Searching in Trie Data Structure:

Search operation in Trie is performed in a similar way as the insertion operation but the only difference is that whenever we find that the array of pointers in curr node does not point to the current character of the word then return false instead of creating a new node for that current character of the word....

3. Deletion in Trie Data Structure

This operation is used to delete strings from the Trie data structure. There are three cases when deleting a word from Trie....

Implement Trie Data Structure?

Algorithm:...

Complexity Analysis of Trie Data Structure

OperationTime ComplexityAuxiliary SpaceInsertionO(n)O(n*m)SearchingO(n)O(1)DeletionO(n)O(1)...

Applications of Trie data structure:

1. Autocomplete Feature: Autocomplete provides suggestions based on what you type in the search box. Trie data structure is used to implement autocomplete functionality....

Advantages of Trie data structure:

Trie allows us to input and finds strings in O(l) time, where l is the length of a single word. It is faster as compared to both hash tables and binary search trees.It provides alphabetical filtering of entries by the key of the node and hence makes it easier to print all words in alphabetical order.Trie takes less space when compared to BST because the keys are not explicitly saved instead each key requires just an amortized fixed amount of space to be stored.Prefix search/Longest prefix matching can be efficiently done with the help of trie data structure.Since trie doesn’t need any hash function for its implementation so they are generally faster than hash tables for small keys like integers and pointers.Tries support ordered iteration whereas iteration in a hash table will result in pseudorandom order given by the hash function which is usually more cumbersome.Deletion is also a straightforward algorithm with O(l) as its time complexity, where l is the length of the word to be deleted....

Disadvantages of Trie data structure:

The main disadvantage of the trie is that it takes a lot of memory to store all the strings. For each node, we have too many node pointers which are equal to the no of characters in the worst case.An efficiently constructed hash table(i.e. a good hash function and a reasonable load factor) has O(1) as lookup time which is way faster than O(l) in the case of a trie, where l is the length of the string....

Top Interview problems on Trie data structure:

S.noProblemPractice1Implement Trie (Prefix Tree)Link2Word Break ProblemLink3BoggleLink4Longest Common Prefix using TrieLink5Find the maximum subarray XOR in a given arrayLink6Count of distinct substrings of a stringLink7Find shortest unique prefix for every word in a given list Link8Count inversions in an arrayLink...

Frequently asked questions (FAQs) about Trie Data Structure:

Is trie an advanced data structure?...

Conclusion:

Our discussion so far has led us to the conclusion that the Trie data structure is a Tree based data structure that is used for storing some collection of strings and performing efficient search operations on them and we have also discussed the various advantage and applications of trie data structure....