# Trie (Prefix Tree)

May 21, 2023

Trie, also known as Prefix Tree, is a tree-based data structure that is commonly used for efficient string searching, prefix matching, and auto-completion. The word “Trie” comes from the word “retrieval,” which accurately describes its primary function. This algorithm enables developers to store and search for strings and related data in a way that is both fast and memory-efficient.

## History and Development

The Trie data structure was first mentioned in a paper published in 1960 by Edward Fredkin. Over time, the data structure was further developed and improved by various researchers, including Renato Cerqueira and Fredson Vieira. In 1998, Google utilized Trie to build their famous search engine, which helped the algorithm gain widespread popularity.

## Key Concepts and Principles

### Nodes

A Trie is made up of nodes, represented by a structure that includes a value and a set of child nodes. Each node represents a character in the string, with the root node representing the empty string.

### Edges

The edges represent the characters that connect the nodes. Each edge is labeled with a character, and each node has a set of edges that link it to its children.

### Terminating Nodes

The terminating node represents the end of a string in the Trie. It is marked by an additional flag on the node, and it indicates that a string ends at that node.

### Prefix Matching

One of the most significant advantages of Trie is its efficiency in providing prefix matches. This algorithm searches the Trie for a matching prefix, and it returns all strings with that prefix.

## Pseudocode and Implementation Details

### Insertion

Insertion is one of the primary operations performed in a Trie. It involves traversing the Trie and inserting each character of the string as a new node until the end of the string is reached. If the string already exists in the Trie, it is not inserted again.

``````def insert(root, word):
for char in word:
if char not in root:
root[char] = {}
root = root[char]
root["#"] = {} # marking the end of the string
``````

Searching a Trie involves traversing the Trie and following edges that correspond to each character in the string. If the search reaches a terminating node, it returns True, indicating that the string exists in the Trie; otherwise, it returns False.

``````def search(root, word):
for char in word:
if char not in root:
return False
root = root[char]
return "#" in root
``````

### Prefix Matching

Prefix matching involves traversing the Trie and following edges that correspond to each character in the prefix. When the end of the prefix is reached, all the strings below that node are collected and returned.

``````def prefix_matching(root, prefix):
for char in prefix:
if char not in root:
return []
root = root[char]
return find_all_strings(root)

def find_all_strings(root):
result = []
if "#" in root:
result.append("")
for char, child_node in root.items():
if char != "#":
child_strings = find_all_strings(child_node)
for child_string in child_strings:
result.append(char + child_string)
return result
``````

## Examples and Use Cases

### Autocomplete

Trie is particularly useful for implementing autocomplete functionality in search engines, messaging applications, and text editors. As users type, the Trie is searched for all strings that begin with the entered text, and these options are presented for selection.

### Spellcheck

Trie can be used for spellchecking, where it checks if a given word is present in the Trie or not. For example, if the word “spelling” is not present in the Trie, it may suggest similar words like “spelling,” “pelling,” or “swelling.”

### IP Routing

Trie can be used in IP routing, where it stores the routing table and searches for the best match to a given IP address.

• Trie is fast and efficient for string searching and prefix matching.
• It can quickly retrieve all strings that match a given prefix, making it an excellent choice for autocomplete.
• It can store a large number of strings using minimal memory.

• The Trie data structure can be memory-intensive, depending on the number of strings stored.
• Managing a Trie can be challenging, particularly when dealing with large numbers of strings.
• In situations where strings have a large number of characters, the Trie can be slower than other search algorithms.

### Compressed Trie

Compressed Trie is a variation of Trie that compresses common prefixes into a single node. This compression reduces the memory requirements of the Trie and increases its speed.

### Suffix Trie

Suffix Trie is a Trie that stores all the suffixes of a given string. It is useful in pattern matching and identifying common substrings in a set of strings.

### Ternary Search Tree

Ternary Search Tree is a variation of Trie that uses a binary search tree to store the children of each node. This design allows for an even more efficient search than the regular Trie.