Trie Data Structure in C++

In this article, we will discuss the trie data structure in C++ with its properties, operations, and examples.

Trie data structure is a type of multi-way tree that is used for storing different strings. Each string consists of characters that are stored in a tree-like structure, i.e., Trie data structure. It is also called a radix tree or prefix tree, or digital tree. Basically, the word "trie" comes from the word "retrieval", which means retrieving or getting back something. It is used for various tasks such as spell-checking, searching words, auto-completion, etc.

Properties of Trie data structure:

There are various properties of the trie data structure. Some main properties of the trie data structure are as follows:

  • Trie data structure is the form of a tree structure where each node represents a single character of a string, and the path covered from root to node represents a specific string.
  • The trie data structure starts from the root node, which is always empty and represents a null character. From the root node, various other branches emerge that hold other characters of a string.
  • The end of a string in a trie data structure is called a leaf node. Each leaf node may contain extra information.

It can store and share a common initial part of a string which refers to the shared prefix. Sharing a common prefix makes it easier to search a set of strings efficiently.

Trie Data Structure in C++

Operations in a Trie data structure:

There are three operations that can be accomplished in a trie data structure:

1. Insert operation:

This operation is used to add a new node, i.e., a new string, into the Trie.

2. Search operation:

This operation is used to find a specific string and check if it exists in the Trie data structure.

3. Delete operation:

This operation is used to remove a string that is present in the Trie data structure.

Example:

Let's take an example to implement trie data structures in C++ to perform insert, search, and delete operations.

Code:

Output:

Trie Data Structure in C++

Explanation of the above C++ program:

  • Firstly, C++ libraries are included in the program.
  • We have assumed a Marco "CHAR_SIZE", which has 128 characters.
  • The class called "Trie" is created, which has four member functions: insert, search, haveChildren, and deletion.
  • The "insert" function is used to add a string to a Trie.
  • The "search" function is used to search for a string in the Trie and returns true if the string is discovered.
  • The "haveChildren" function checks for non-zero children in a given Trie node.
  • The "delete" function is a recursive function that delete a string from the Trie.
  • Various strings, which are "java", "javatpoint", "javac", and "j" are inserted using the "insert"
  • The presence of various strings is checked using the "search" When the string "java" is searched, it returns true. Similarly, when the string "javatpoint" is searched, it returns true. When the string "javaa" is searched, it returns false because this string is not present in the Trie. When the string "javac" is searched, it returns true, and when the string "j" is searched, it returns true.
  • After that, the strings, which are "java", "javatpoint", "javac" and "j" are deleted using the "deletion"
  • After the deletion operation, the "search" method is called again to verify that the strings have been successfully deleted from the Trie.
  • If the search operation is successful, it returns 1 when the string is found and returns 0 when the string is not found.
  • The final output will show that the Trie is empty. It prints "Empty!!" and returns O.

Conclusion:

In this article, we have understood the Trie data structure, a tree-like structure that stores a collection of strings. It has various applications like word search, spell-check, auto-completion, etc. We have understood the various operations of the Trie data structure, which performs insert, search, and delete operations.






Latest Courses