The Wayback Machine - https://web.archive.org/web/20241207064028/https://www.geeksforgeeks.org/trie-insert-and-search/
Open In App

Trie Data Structure | Insert and Search

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Trie data structure is a tree-like data structure used for storing a dynamic set of strings. It is commonly used for efficient retrieval and storage of keys in a large dataset. The structure supports operations such as insertion, search, and deletion of keys, making it a valuable tool in fields like computer science and information retrieval. In this article we are going to explore insertion and search operation in Trie Data Structure.

Trie data structure

Trie data structure

Representation of of Trie Node:

A Trie data structure consists of nodes connected by edges. Each node represents a character or a part of a string. The root node, the starting point of the Trie, represents an empty string. Each edge emanating from a node signifies a specific character. The path from the root to a node represents the prefix of a string stored in the Trie.

A simple structure to represent nodes of the English alphabet can be as follows.

C++
struct TrieNode {

    // pointer array for child nodes of each node
    TrieNode* child[26];

    // Used for indicating ending of string
    bool wordEnd;

    TrieNode()
    {
        // constructor
        // initialize the wordEnd variable with false
        // initialize every index of childNode array with
        // NULL
        wordEnd = false;
        for (int i = 0; i < 26; i++) {
            child[i] = NULL;
        }
    }
};
C
// Trie node structure
struct TrieNode {
    struct TrieNode* child[26];
    bool isEndOfWord;
};

// Function to create a new Trie node
struct TrieNode* getNode() {
    struct TrieNode* node = 
      (struct TrieNode*)malloc(sizeof(struct TrieNode));
    node->isEndOfWord = false;
    for (int i = 0; i < 26; i++) {
        node->child[i] = NULL;
    }
    return node;
}
Java
public class TrieNode {
    
    // Array for child nodes of each node
    TrieNode[] child;
    
    // Used for indicating the end of a string
    boolean wordEnd;

    // Constructor
    public TrieNode() {
      
        // Initialize the wordEnd 
        // variable with false
        wordEnd = false;

        // Initialize every index of 
        // the child array with null
        // In Java, we do not have to 
        // explicitely assign null as 
        // the values are by default 
        // assigned as null 
        child = new TrieNode[26];
    }
}
Python
class TrieNode:
    def __init__(self):
        self.child = [None] * 26
        self.wordEnd = False
C#
class TrieNode
{
    public TrieNode[] Child = new TrieNode[26];
    public bool WordEnd = false;
}
JavaScript
class TrieNode {
    constructor() {
    
        // Initialize the child Node
        // array with 26 nulls
        this.child = Array(26).fill(null);
        
        // Initialize wordEnd to the false
        // indicating that no word ends here yet
        this.wordEnd = false;
    }
}

Insertion in Trie Data Structure:

Let’s walk through the process of inserting the words into a Trie data structure. We have already cover the basics of Trie and its node structure.

Here’s a visual representation of inserting the words “and” and “ant” into a Trie data structure:


Insert Operation in Trie Data Structure


Inserting “and” in Trie data structure:

  • Start at the root node: The root node has no character associated with it and its wordEnd value is 0, indicating no complete word ends at this point.
  • First character “a”: Calculate the index using ‘a’ – ‘a’ = 0. Check if the child[0] is null. Since it is, create a new TrieNode with the character “a“, wordEnd set to 0, and an empty array of pointers. Move to this new node.
  • Second character “n”: Calculate the index using ‘n’ – ‘a’ = 13. Check if child[13] is null. It is, so create a new TrieNode with the character “n“, wordEnd set to 0, and an empty array of pointers. Move to this new node.
  • Third character “d”: Calculate the index using ‘d’ – ‘a’ = 3. Check if child[3] is null. It is, so create a new TrieNode with the character “d“, wordEnd set to 1 (indicating the word “and” ends here).

Inserting “ant” in Trie data structure:

  • Start at the root node: Root node doesn’t contain any data but it keep track of every first character of every string that has been inserted.
  • First character “a”: Calculate the index using ‘a’ – ‘a’ = 0. Check if the child[0] is null. We already have the “a” node created from the previous insertion. so move to the existing “a” node.
  • First character “n”: Calculate the index using ‘n’ – ‘a’ = 13. Check if child[13] is null. It’s not, so move to the existing “n” node.
  • Second character “t”: Calculate the index using ‘t’ – ‘a’ = 19. Check if child[19] is null. It is, so create a new TrieNode with the character “t“, wordEnd set to 1 (indicating the word “ant” ends here).

Below is the implementation of insertion of strings in Trie data structure:

Time Complexity: O(number of words * maxLengthOfWord)
Auxiliary Space: O(number of words * maxLengthOfWord)

Searching in Trie Data Structure:

Searching for a key in Trie data structure is similar to its insert operation. However, It only compares the characters and moves down. The search can terminate due to the end of a string or lack of key in the trie. 

Steps by step approach for searching in Trie Data structure:

  • Start at the root node. This is the starting point for all searches within the Trie.
  • Traverse the Trie based on the characters of the word you are searching for. For each character, follow the corresponding branch in the Trie. If the branch doesn’t exist, the word is not present in the Trie.
  • If you reach the end of the word and the wordEnd flag is set to 1, the word has been found.
  • If you reach the end of the word and the wordEnd flag is 0, the word is not present in the Trie, even though it shares a prefix with an existing word.

Below is the implementation of the above approach:

C++
// Method to insert a key into the Trie
void insertKey(TrieNode* root, const string& key) {
  
    // Initialize the curr pointer with the root node
    TrieNode* curr = root;

    // Iterate across the length of the string
    for (char c : key) {
      
        // Check if the node exists for the 
        // current character in the Trie
        if (curr->child[c - 'a'] == nullptr) {
          
            // If node for current character does 
            // not exist then make a new node
            TrieNode* newNode = new TrieNode();
          
            // Keep the reference for the newly
            // created node
            curr->child[c - 'a'] = newNode;
        }
      
        // Move the curr pointer to the
        // newly created node
        curr = curr->child[c - 'a'];
    }

    // Mark the end of the word
    curr->wordEnd = true;
}
C
// Function to insert a key into the Trie
void insert(struct TrieNode* root, const char* key) {
    struct TrieNode* curr = root;
    while (*key) {
        int index = *key - 'a';
        if (!curr->child[index]) {
            curr->child[index] = getNode();
        }
        curr = curr->child[index];
        key++;
    }
    curr->isEndOfWord = true;
}
Java
// Method to insert a key into the Trie
static void insertKey(TrieNode root, String key)
{

    // Initialize the curr pointer with the root node
    TrieNode curr = root;

    // Iterate across the length of the string
    for (char c : key.toCharArray()) {

        // Check if the node exists for the
        // current character in the Trie
        if (curr.child[c - 'a'] == null) {

            // If node for current character does
            // not exist then make a new node
            TrieNode newNode = new TrieNode();

            // Keep the reference for the newly
            // created node
            curr.child[c - 'a'] = newNode;
        }

        // Move the curr pointer to the
        // newly created node
        curr = curr.child[c - 'a'];
    }

    // Mark the end of the word
    curr.wordEnd = true;
}
Python
# Method to insert a key into the Trie
def insert_key(root, key):

    # Initialize the curr pointer with the root node
    curr = root

    # Iterate across the length of the string
    for c in key:

        # Check if the node exists for the
        # current character in the Trie
        index = ord(c) - ord('a')
        if curr.child[index] is None:

            # If node for current character does
            # not exist then make a new node
            new_node = TrieNode()

            # Keep the reference for the newly
            # created node
            curr.child[index] = new_node

        # Move the curr pointer to the
        # newly created node
        curr = curr.child[index]

    # Mark the end of the word
    curr.word_end = True
C#
// Method to insert a key into the Trie
public static void InsertKey(TrieNode root, string key)
{
    // Initialize the curr pointer with the root node
    TrieNode curr = root;

    // Iterate across the length of the string
    foreach(char c in key)
    {
        // Check if the node exists for the current
        // character in the Trie
        if (curr.Child[c - 'a'] == null) {
            // If node for current character does
            // not exist then make a new node
            TrieNode newNode = new TrieNode();

            // Keep the reference for the newly created node
            curr.Child[c - 'a'] = newNode;
        }

        // Move the curr pointer to the newly created node
        curr = curr.Child[c - 'a'];
    }

    // Mark the end of the word
    curr.WordEnd = true;
}
JavaScript
// Method to insert a key into the Trie
function insertKey(root, key) {

    // Initialize the curr pointer with the root node
    let curr = root;

    // Iterate across the length of the string
    for (let c of key) {

        // Check if the node exists for the 
        // current character in the Trie
        let index = c.charCodeAt(0) - 'a'.charCodeAt(0);
        if (curr.child[index] === null) {

            // If node for current character does 
            // not exist then make a new node
            let newNode = new TrieNode();

            // Keep the reference for the newly
            // created node
            curr.child[index] = newNode;
        }

        // Move the curr pointer to the
        // newly created node
        curr = curr.child[index];
    }

    // Mark the end of the word
    curr.wordEnd = true;
}

Output
Query String: do
The query string is present in the Trie
Query String: geek
The query string is present in the Trie
Query String: bat
The query string is not present in the Trie

Here’s a visual representation of searching word “dad” in Trie data structure:

Let’s assume that we have successfully inserted the words “and“, “ant“, and “dad” into our Trie, and we have to search for specific words within the Trie data structure. Let’s try searching for the word “dad“:


Search Operation in Trie Data Structure


  • We start at the root node.
  • We follow the branch corresponding to the character ‘d’.
  • We follow the branch corresponding to the character a’.
  • We follow the branch corresponding to the character ‘d’.
  • We reach the end of the word and wordEnd flag is 1. This means that “dad” is present in the Trie.

Below is the implementation of searching strings in Trie Data Structure:

C++
// Method to search a key in the Trie
bool searchKey(TrieNode* root, const string& key) {
  
    // Initialize the curr pointer with the root node
    TrieNode* curr = root;

    // Iterate across the length of the string
    for (char c : key) {
      
        // Check if the node exists for the 
        // current character in the Trie
        if (curr->child[c - 'a'] == nullptr) 
            return false;
        
        // Move the curr pointer to the 
        // already existing node for the 
        // current character
        curr = curr->child[c - 'a'];
    }

    // Return true if the word exists 
    // and is marked as ending
    return curr->wordEnd;
}
C
// Function to search a key in the Trie
bool search(struct TrieNode* root, const char* key) {
    struct TrieNode* curr = root;
    while (*key) {
        int index = *key - 'a';
        if (!curr->child[index]) {
            return false;
        }
        curr = curr->child[index];
        key++;
    }
    return (curr != NULL && curr->isEndOfWord);
}
Java
// Method to search a key in the Trie
static boolean searchKey(TrieNode root, String key)
{

    // Initialize the curr pointer with the root node
    TrieNode curr = root;

    // Iterate across the length of the string
    for (char c : key.toCharArray()) {

        // Check if the node exists for the
        // current character in the Trie
        if (curr.child[c - 'a'] == null)
            return false;

        // Move the curr pointer to the
        // already existing node for the
        // current character
        curr = curr.child[c - 'a'];
    }

    // Return true if the word exists
    // and is marked as ending
    return curr.wordEnd;
}
Python
# Method to search a key in the Trie
def search_key(root, key):

    # Initialize the curr pointer with the root node
    curr = root

    # Iterate across the length of the string
    for c in key:

        # Check if the node exists for the 
        # current character in the Trie
        index = ord(c) - ord('a')
        if curr.child[index] is None:
            return False

        # Move the curr pointer to the 
        # already existing node for the 
        # current character
        curr = curr.child[index]

    # Return true if the word exists 
    # and is marked as ending
    return curr.word_end
C#
// Method to search a key in the Trie
public static bool SearchKey(TrieNode root, string key)
{
    // Initialize the curr pointer with the root node
    TrieNode curr = root;

    // Iterate across the length of the string
    foreach(char c in key)
    {
        // Check if the node exists for the current
        // character in the Trie
        if (curr.Child[c - 'a'] == null)
            return false;

        // Move the curr pointer to the already
        // existing node for the current character
        curr = curr.Child[c - 'a'];
    }

    // Return true if the word exists and
    // is marked as ending
    return curr.WordEnd;
}
JavaScript
// Method to search a key in the Trie
function searchKey(root, key) {

    // Initialize the curr pointer with the root node
    let curr = root;

    // Iterate across the length of the string
    for (let c of key) {

        // Check if the node exists for the 
        // current character in the Trie
        let index = c.charCodeAt(0) - 'a'.charCodeAt(0);
        if (curr.child[index] === null) 
            return false;

        // Move the curr pointer to the 
        // already existing node for the 
        // current character
        curr = curr.child[index];
    }

    // Return true if the word exists 
    // and is marked as ending
    return curr.wordEnd;
}

Time Complexity: O(number of words * maxLengthOfWord)
Auxiliary Space: O(number of words * maxLengthOfWord)

Implementation of Insert and Search Operations in Trie Data Structure:

Steps-by-step approach:

  • Create a root node with the help of TrieNode() constructor.
  • Store a collection of strings that have to be inserted in the trie in a vector of strings say, arr.
  • Inserting all strings in Trie with the help of the insert_key() function,
  • Search strings with the help of search_key() function.

Below is the implementation of the above approach:

C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct TrieNode {
  
    // Array for child nodes of each node
    TrieNode* child[26];

    // for end of word
    bool wordEnd;

    TrieNode() {
        wordEnd = false;
        for (int i = 0; i < 26; i++) {
            child[i] = nullptr;
        }
    }
};

// Method to insert a key into the Trie
void insertKey(TrieNode* root, const string& key) {
  
    // Initialize the curr pointer with the root node
    TrieNode* curr = root;

    // Iterate across the length of the string
    for (char c : key) {
      
        // Check if the node exists for the 
        // current character in the Trie
        if (curr->child[c - 'a'] == nullptr) {
          
            // If node for current character does 
            // not exist then make a new node
            TrieNode* newNode = new TrieNode();
          
            // Keep the reference for the newly
            // created node
            curr->child[c - 'a'] = newNode;
        }
      
        // Move the curr pointer to the
        // newly created node
        curr = curr->child[c - 'a'];
    }

    // Mark the end of the word
    curr->wordEnd = true;
}

// Method to search a key in the Trie
bool searchKey(TrieNode* root, const string& key) {
  
    // Initialize the curr pointer with the root node
    TrieNode* curr = root;

    // Iterate across the length of the string
    for (char c : key) {
      
        // Check if the node exists for the 
        // current character in the Trie
        if (curr->child[c - 'a'] == nullptr) 
            return false;
        
        // Move the curr pointer to the 
        // already existing node for the 
        // current character
        curr = curr->child[c - 'a'];
    }

    // Return true if the word exists 
    // and is marked as ending
    return curr->wordEnd;
}

int main() {
  
    // Create am example Trie
    TrieNode* root = new TrieNode();
    vector<string> arr = 
      {"and", "ant", "do", "geek", "dad", "ball"};
    for (const string& s : arr) {
        insertKey(root, s);
    }

    // One by one search strings
    vector<string> searchKeys = {"do", "gee", "bat"};
    for (string& s : searchKeys) {
        cout << "Key : " << s << "\n";
        if (searchKey(root, s)) 
            cout << "Present\n";
        else 
            cout << "Not Present\n";        
    }
  
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define ALPHABET_SIZE 26

// Trie node structure
struct TrieNode {
    struct TrieNode* child[ALPHABET_SIZE];
    bool isEndOfWord;
};

// Function to create a new Trie node
struct TrieNode* getNode() {
    struct TrieNode* node = 
      (struct TrieNode*)malloc(sizeof(struct TrieNode));
    node->isEndOfWord = false;
    for (int i = 0; i < ALPHABET_SIZE; i++) {
        node->child[i] = NULL;
    }
    return node;
}

// Function to insert a key into the Trie
void insert(struct TrieNode* root, const char* key) {
    struct TrieNode* curr = root;
    while (*key) {
        int index = *key - 'a';
        if (!curr->child[index]) {
            curr->child[index] = getNode();
        }
        curr = curr->child[index];
        key++;
    }
    curr->isEndOfWord = true;
}

// Function to search a key in the Trie
bool search(struct TrieNode* root, const char* key) {
    struct TrieNode* curr = root;
    while (*key) {
        int index = *key - 'a';
        if (!curr->child[index]) {
            return false;
        }
        curr = curr->child[index];
        key++;
    }
    return (curr != NULL && curr->isEndOfWord);
}

// Main function to test the Trie implementation
int main() {
    // Create an example Trie
    char* keys[] = {"and", "ant", "do", "geek", "dad", "ball"};
    int n = sizeof(keys) / sizeof(keys[0]);

    struct TrieNode* root = getNode();
    for (int i = 0; i < n; i++) {
        insert(root, keys[i]);
    }

    // One by one search strings
    char* searchKeys[] = {"do", "gee", "bat"};
    int m = sizeof(searchKeys) / sizeof(searchKeys[0]);

    for (int i = 0; i < m; i++) {
        printf("Key : %s\n", searchKeys[i]);
        if (search(root, searchKeys[i])) {
            printf("Present\n");
        } else {
            printf("Not Present\n");
        }
    }

    return 0;
}
Java
import java.util.Arrays;
import java.util.List;

class TrieNode {

    // Java Makes sure that all 26 are null
    TrieNode[] child = new TrieNode[26];

    // for end of word
    boolean wordEnd = false;
}

class GfG {

    // Method to insert a key into the Trie
    static void insertKey(TrieNode root, String key) {

        // Initialize the curr pointer with the root node
        TrieNode curr = root;

        // Iterate across the length of the string
        for (char c : key.toCharArray()) {

            // Check if the node exists for the 
            // current character in the Trie
            if (curr.child[c - 'a'] == null) {

                // If node for current character does 
                // not exist then make a new node
                TrieNode newNode = new TrieNode();

                // Keep the reference for the newly
                // created node
                curr.child[c - 'a'] = newNode;
            }

            // Move the curr pointer to the
            // newly created node
            curr = curr.child[c - 'a'];
        }

        // Mark the end of the word
        curr.wordEnd = true;
    }

    // Method to search a key in the Trie
    static boolean searchKey(TrieNode root, String key) {

        // Initialize the curr pointer with the root node
        TrieNode curr = root;

        // Iterate across the length of the string
        for (char c : key.toCharArray()) {

            // Check if the node exists for the 
            // current character in the Trie
            if (curr.child[c - 'a'] == null) 
                return false;

            // Move the curr pointer to the 
            // already existing node for the 
            // current character
            curr = curr.child[c - 'a'];
        }

        // Return true if the word exists 
        // and is marked as ending
        return curr.wordEnd;
    }

    public static void main(String[] args) {

        // Create an example Trie
        TrieNode root = new TrieNode();
        List<String> arr = Arrays.asList(
            "and", "ant", "do", "geek", "dad", "ball");
        for (String s : arr) {
            insertKey(root, s);
        }

        // One by one search strings
        List<String> searchKeys = 
              Arrays.asList("do", "gee", "bat");
        for (String s : searchKeys) {
            System.out.println("Key : " + s);
            if (searchKey(root, s)) 
                System.out.println("Present");
            else 
                System.out.println("Not Present");        
        }
    }
}
Python
class TrieNode:

    def __init__(self):
      
        # Array for child nodes of each node
        self.child = [None] * 26
        
        # for end of word
        self.word_end = False

# Method to insert a key into the Trie
def insert_key(root, key):

    # Initialize the curr pointer with the root node
    curr = root

    # Iterate across the length of the string
    for c in key:

        # Check if the node exists for the 
        # current character in the Trie
        index = ord(c) - ord('a')
        if curr.child[index] is None:

            # If node for current character does 
            # not exist then make a new node
            new_node = TrieNode()

            # Keep the reference for the newly
            # created node
            curr.child[index] = new_node

        # Move the curr pointer to the
        # newly created node
        curr = curr.child[index]

    # Mark the end of the word
    curr.word_end = True

# Method to search a key in the Trie
def search_key(root, key):

    # Initialize the curr pointer with the root node
    curr = root

    # Iterate across the length of the string
    for c in key:

        # Check if the node exists for the 
        # current character in the Trie
        index = ord(c) - ord('a')
        if curr.child[index] is None:
            return False

        # Move the curr pointer to the 
        # already existing node for the 
        # current character
        curr = curr.child[index]

    # Return true if the word exists 
    # and is marked as ending
    return curr.word_end

# Create an example Trie
root = TrieNode()
arr = ["and", "ant", "do", "geek", "dad", "ball"]
for s in arr:
    insert_key(root, s)

# One by one search strings
search_keys = ["do", "gee", "bat"]
for s in search_keys:
    print(f"Key : {s}")
    if search_key(root, s):
        print("Present")
    else:
        print("Not Present")
C#
using System;
using System.Collections.Generic;

class TrieNode
{
    public TrieNode[] Child = new TrieNode[26];
    public bool WordEnd = false;
}

public class GfG
{
    // Method to insert a key into the Trie
    public static void InsertKey(TrieNode root, string key)
    {
        // Initialize the curr pointer with the root node
        TrieNode curr = root;

        // Iterate across the length of the string
        foreach (char c in key)
        {
            // Check if the node exists for the current 
            // character in the Trie
            if (curr.Child[c - 'a'] == null)
            {
                // If node for current character does
                // not exist then make a new node
                TrieNode newNode = new TrieNode();

                // Keep the reference for the newly created node
                curr.Child[c - 'a'] = newNode;
            }

            // Move the curr pointer to the newly created node
            curr = curr.Child[c - 'a'];
        }

        // Mark the end of the word
        curr.WordEnd = true;
    }

    // Method to search a key in the Trie
    public static bool SearchKey(TrieNode root, string key)
    {
        // Initialize the curr pointer with the root node
        TrieNode curr = root;

        // Iterate across the length of the string
        foreach (char c in key)
        {
            // Check if the node exists for the current character in the Trie
            if (curr.Child[c - 'a'] == null)
                return false;

            // Move the curr pointer to the already 
            // existing node for the current character
            curr = curr.Child[c - 'a'];
        }

        // Return true if the word exists and 
        // is marked as ending
        return curr.WordEnd;
    }

    public static void Main(string[] args)
    {
        // Create an example Trie
        TrieNode root = new TrieNode();
        List<string> arr = 
          new List<string> { "and", "ant", "do", "geek", "dad", "ball" };
        foreach (string s in arr)
        {
            InsertKey(root, s);
        }

        // One by one search strings
        List<string> searchKeys = 
             new List<string> { "do", "gee", "bat" };
        foreach (string s in searchKeys)
        {
            Console.WriteLine("Key : " + s);
            if (SearchKey(root, s))
                Console.WriteLine("Present");
            else
                Console.WriteLine("Not Present");
        }
    }
}
JavaScript
class TrieNode {
    constructor() {
    
        // Array for child nodes of each node
        this.child = new Array(26).fill(null);
        
        // for end of word
        this.wordEnd = false;
    }
}

// Method to insert a key into the Trie
function insertKey(root, key) {

    // Initialize the curr pointer with the root node
    let curr = root;

    // Iterate across the length of the string
    for (let c of key) {

        // Check if the node exists for the 
        // current character in the Trie
        let index = c.charCodeAt(0) - 'a'.charCodeAt(0);
        if (curr.child[index] === null) {

            // If node for current character does 
            // not exist then make a new node
            let newNode = new TrieNode();

            // Keep the reference for the newly
            // created node
            curr.child[index] = newNode;
        }

        // Move the curr pointer to the
        // newly created node
        curr = curr.child[index];
    }

    // Mark the end of the word
    curr.wordEnd = true;
}

// Method to search a key in the Trie
function searchKey(root, key) {

    // Initialize the curr pointer with the root node
    let curr = root;

    // Iterate across the length of the string
    for (let c of key) {

        // Check if the node exists for the 
        // current character in the Trie
        let index = c.charCodeAt(0) - 'a'.charCodeAt(0);
        if (curr.child[index] === null) 
            return false;

        // Move the curr pointer to the 
        // already existing node for the 
        // current character
        curr = curr.child[index];
    }

    // Return true if the word exists 
    // and is marked as ending
    return curr.wordEnd;
}

// Create an example Trie
let root = new TrieNode();
let arr = 
   ["and", "ant", "do", "geek", "dad", "ball"];
for (let s of arr) {
    insertKey(root, s);
}

// One by one search strings
let searchKeys = ["do", "gee", "bat"];
for (let s of searchKeys) {
    console.log("Key :", s);
    if (searchKey(root, s)) 
        console.log("Present");
    else 
        console.log("Not Present");        
}

Output
Query String: do
The query string is present in the Trie
Query String: geek
The query string is present in the Trie
Query String: bat
The query string is not present in the Trie

Complexity Analysis of Trie Data Structure:

OperationTime Complexity
InsertionO(n) Here n is the length of the string inserted
SearchingO(n) Here n is the length of the string searched

Related Articles: 

Practice Problems: 



Next Article

Similar Reads

three90RightbarBannerImg