Lesson 20 of 47
In Progress

# Trie Tree

A Trie Tree (or Prefix Tree) is a data structure used for storing strings in an efficient manner. It is an ordered tree-like structure that can be used for searching, inserting, and deleting data quickly. Trie Trees are used in many applications such as spell-checking, auto-complete, network routing, and graph algorithms. This article will cover the basics of a Trie Tree, its time and space complexity, and provide Python code examples to help illustrate the concepts.

## What is a Trie Tree?

A Trie Tree is a data structure used to store strings in an efficient manner. It is an ordered tree-like structure that can be used for searching, inserting, and deleting data quickly. Trie Trees are also known as digital trees, radix trees, or prefix trees. A Trie Tree consists of nodes, where each node stores a character of a string. The root node is the starting point, and each branch from this node represents a possible character of the string. The characters of the string are stored in the order in which they appear in the string.

For example, consider a Trie Tree that stores the strings “apple”, “app”, and “apply”. The root node will contain the character “a”. From the root node, there are two branches: one for the character “p” and one for the character “l”. The “p” branch will lead to another node which contains the character “p”. From this node, there will be two more branches: one for the character “l” and one for the character “e”. The “l” branch will lead to a node containing the character “l”, and the “e” branch will lead to a node containing the character “e”. This node will be the end of the string “apple”. The “apply” string will be stored in the same way, but will have an extra branch for the character “y”.

## Time Complexity

The time complexity of a Trie Tree is based on the length of the string being stored. For a string of length n, the insertion, search, and deletion operations take O(n) time. This is because each character in the string must be traversed in order to find the correct node.

The time complexity for insertion, search, and deletion operations can be further broken down into best-case, average-case, and worst-case scenarios.

In the best-case scenario, the string is already present in the Trie Tree and the operation takes constant time, O(1). This is because the string is already present and the operation is only required to traverse the string once.

In the average-case scenario, the string is not already present in the Trie Tree and the operation takes linear time, O(n). This is because the string must be inserted, searched, or deleted one character at a time.

In the worst-case scenario, the string is not already present in the Trie Tree and the operation takes linear time, O(n). This is because the string must be inserted, searched, or deleted one character at a time.

## Space Complexity

The space complexity of a Trie Tree is based on the number of nodes in the tree. For a string of length n, the space complexity is O(n). This is because each character in the string must be stored in a separate node.

## Python Implementation

Now that we have a basic understanding of Trie Trees, let’s look at how to implement one in Python. We will create a simple Trie Tree class that can be used to store strings.

class TrieTree:
def __init__(self):
self.root = {}

def insert(self, word):
current_node = self.root
for char in word:
if char not in current_node:
current_node[char] = {}
current_node = current_node[char]
current_node['*'] = True

def search(self, word):
current_node = self.root
for char in word:
if char not in current_node:
return False
current_node = current_node[char]
return '*' in current_node

def delete(self, word):
current_node = self.root
for char in word:
if char not in current_node:
return False
current_node = current_node[char]
del current_node['*']
return True

The Trie Tree class consists of three methods: insert(), search(), and delete(). The insert() method takes a string as a parameter and inserts it into the Trie Tree. The search() method takes a string as a parameter and returns True if the string is present in the Trie Tree, or False otherwise. The delete() method takes a string as a parameter and deletes it from the Trie Tree.

## Conclusion

In this article, we have discussed Trie Trees, a data structure used for storing strings in an efficient manner. We have seen how they are implemented in Python and discussed their time and space complexity. Trie Trees are used in many applications such as spell-checking, auto-complete, network routing, and graph algorithms.

## Exercises

#### Create a Trie Tree and insert the strings “apple”, “app”, and “apply”.

# Create the Trie Tree
trie = TrieTree()

# Insert the strings
trie.insert("apple")
trie.insert("app")
trie.insert("apply")

#### Write a function that takes a string as a parameter and returns True if the string is present in the Trie Tree, or False otherwise.

def search(trie, word):
current_node = trie.root
for char in word:
if char not in current_node:
return False
current_node = current_node[char]
return '*' in current_node

#### Write a function that takes a string as a parameter and deletes it from the Trie Tree.

def delete(trie, word):
current_node = trie.root
for char in word:
if char not in current_node:
return False
current_node = current_node[char]
del current_node['*']
return True

#### Write a function that takes a list of strings as a parameter and inserts them into the Trie Tree.

def insert_all(trie, strings):
for string in strings:
trie.insert(string)

#### Write a function that takes a list of strings as a parameter and returns True if all the strings are present in the Trie Tree, or False otherwise.

def search_all(trie, strings):
for string in strings:
if not search(trie, string):
return False
return True