Javatpoint Logo
Javatpoint Logo

Auto-complete feature using Trie

Introduction

These days, auto-complete functions are commonplace in digital environments. You have probably come across auto-complete suggestions that simplify your life when you are typing on your smartphone, sending an email, or conducting a Google search. By anticipating and completing their input, these recommendations assist users, making their experience faster and more effective.

The Trie data structure is one of the core technologies underlying auto-complete capabilities. Pronounce it "try"; a Trie is a tree-like data structure that stores a dynamic set of strings, which makes it ideal for quickly finding and retrieving words. This post will explain the idea of auto-complete and show you how to use a Trie to put it into practice.

Understanding Tries

It's crucial to comprehend the Trie data structure before we start developing an auto-complete feature.

A trie is a tree structure in which each node represents a word's characters. The following character in a word is represented by a level down the tree, with the root node normally being an empty string. You create words by following the flow of characters from the root node to the leaf nodes as you move through the Trie. Every Trie node includes:

  • A dictionary or array that uses keys to represent characters and values to represent references to child nodes in order to store references to child nodes.
  • A binary indicator that indicates if the node signifies the conclusion of a word.

It is quite effective at storing and finding words, thanks to the Trie structure. It has word suggestions, partial matching, and fast lookups-all necessary functions for an auto-complete system.

Building a Trie

You must first establish a Trie data structure in order to implement an auto-complete feature. Let's go over how to construct a trie:

Step 1: Trie Node Definition

For each character in a word to be represented, a TrieNode class is required. A flag to denote the end of a word and attributes for kids (to represent the following character) should be included in this class.

The TrieNode class in Python can be defined as follows:

The is_end_of_word boolean flag is set to True when a node indicates the end of a word, and the children attribute holds pointers to child nodes.

Step 2: Create the Trie

Let's now construct the Trie class, which has methods for adding words to the Trie and the root node.

Words are inserted into the Trie via the insert method, which is part of the Trie class, which has a root node. Iterating through each character in the word, it adds new nodes as necessary and marks the word's conclusion when the full word has been added.

Auto-Complete with Trie

Having established a Trie data structure, we can now utilize it to incorporate the auto-complete functionality. Our goal is to identify every term that shares a certain prefix. The steps to do this are as follows:

Step 1: Search for the Prefix

Matching characters from the prefix begins by working your way through the Trie from the root. In order to complete this operation, every character in the prefix must be verified as a child node. If a character is absent, the prefix is absent from all words.

Step 2: Find Auto-Complete Suggestions

To locate all words that share the same prefix, we can perform a depth-first search (DFS) until we get to the last node of the prefix. The DFS constructs words as it travels, investigating every avenue that could lead from the present node.

The recursive function finds words in this code, builds words, and adds them to the suggestions list whenever it reaches the end of a word. It does this by examining every path that could lead from the current node. This procedure makes sure that every word containing the specified prefix is included.

Code

Output:

Auto-complete feature using Trie

Code Explanation

TrieNode Class

  • Represents a node in the Trie data structure.
  • Attributes: children (dictionary to store child nodes) and is_end_of_word (boolean indicating if the node marks the end of a word).

Trie Class

  • Represents a Trie with a root node.
  • Methods: insert (to insert a word into the Trie) and find_words_with_prefix (to find words with a given prefix).

Insert Method

  • Inserts a word into the Trie by iterating through its characters and creating nodes as needed.

Find Words with Prefix Method

  • Returns a list of words with a given prefix by traversing the Trie and collecting suggestions.

Main Block

  • Creates a Trie (trie) and inserts a list of words.
  • Calls find_words_with_prefix with the prefix "app" and prints the suggestions: ['apple', 'appetizer'].






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA