What are different types of Tries?


Introduction

In this tutorial, we will understand different types of tries and their uses. Tries are tree-like data structures that are mostly used for operations like string searching. There are various types of trie and they are used as per the task requirement. Generally, trie tries are of three types: Standard trie, compressed trie, and suffix trie. We elaborate on the meaning of each type of trie.

What is Trie

A trie is a sorted binary tree also called a digital tree or a prefix tree. It has nodes that are used to store data or alphabets. Each node can have a single or multiple children or no children. Most commonly, Trie is used for applications like IP routing and auto search.

Types of Tries

Tries are broadly classified into three types −

Standard Trie

  • Standard trie is a sorted or ordered trie which includes roots and nodes. Except for the root node, every node in the standard trie represents the alphabet of the string.

  • All children are alphabetically sorted and the path for tracing a string is from node to root. The last child of a node is the end of the string.

  • In standard trie, a string can be traced by following the path from the node to its last child.

  • Example − String S = {cat, car, dog, doll }.

  • Standard trie has three operations − delete, update, and insert. The complexities of these operations are as −

  • Time Complexity O(dm)

  • Space Complexity O(n)

    • Where,

    • n = string size

    • m = size of the alphabet

    • d = operation string parameter size

  • Standard trie is mostly used for string applications like word matching, and prefix matching. Word matching is finding the similar words of a set in a string and prefix matching is matching the repetition of the prefix of a word.

Compressed Trie

  • It is a compressed or compact form of the standard trie. It merges similar prefix nodes of standard trie to a single child.

  • The nodes of compressed trie have a minimum of 2 children.

  • The compression of reductant nodes helps in memory management. This type of trie is used with applications that need space management.

  • The space complexity of a compressed trie is O(n).

  • Its two basic operations are insertion and deletion. For inserting a new alphabet to a node, ungroup the already grouped alphabets.

  • For deleting any character from the tree, re-group the inserted character after deletion.

  • For example, String S = {bear, bull, boy, stop, sell}

Suffix Trie

  • Suffix trie is a compressed form of a trie that is used in applications like bioinformatics, and pattern matching.

  • Unlike other tries it compresses all the suffixes in a single child while excluding some prefixes.

  • The children store words instead of characters.

  • A compressed trie can be constructed using a suffix trie.

  • It is an efficient approach for substring matching.

  • The path from the root to its leaf denotes a suffix.

  • The end of a path is represented by a $ symbol.

  • Example − string S = {ban, apple}

Difference between Standard trie, Compact trie, and Suffix trie

S. No

Standard Trie

Compressed Trie

Suffix Trie

1

It is the most basic form of trie.

It is an advanced form of standard trie.

It is a completely different trie type with strings stored in compressed form.

2

Each node with its children represent alphabets

Reductand nodes are compressed.

It is for inserting suffixes in a node.

3

Last alphabet is represented by children.

Last alphabet is represented by children.

$ symbol represents the end of the node path.

4

It supports operations like insertion, deletion, and searching.

It supports operations like insertion and deletion with grouping and ungrouping of already formed groups.

It supports operations for suffix matching and searching.

5

A node can have one or more or no children.

Each node has at least 2 children.

Each node has a suffix of words.

6

It is a general purpose trie for storing individual character of a word.

It helps in optimizing the space while merging the reductant nodes.

It is a special trie type that helps in retrieval of suffix/

Conclusion

We have reached the end of this tutorial. In this tutorial, we discuss three major types of trie: standard trie, compressed trie, and suffix trie. A compressed trie is an advanced form of standard trie that helps in memory management. Each trie has nodes and children to represent the string. All 3 types of tries are used for string-matching applications. We also discussed the difference between these three types of tries.

Updated on: 04-Oct-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements