Trie is a tree-like data structure used in computer science to efficiently store and retrieve strings. It is also known as a prefix tree, as it stores strings by breaking them down into their individual characters and organizing them in a tree structure based on their common prefixes. Each node in the tree represents a single character, and the path from the root to a leaf node represents a complete string. Tries are commonly used in applications such as autocomplete, spell checkers, and IP routing tables, as they allow for fast and efficient searching and retrieval of strings. Keep reading below to learn how to use a Trie in C#.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

How to use a Trie in C# with example code

Trie is a tree-like data structure that is used to store a collection of strings. It is also known as a prefix tree because it can efficiently search for all strings that have a given prefix. In this blog post, we will learn how to use a Trie in C# with example code.

To use a Trie in C#, we need to define a TrieNode class that represents a node in the Trie. Each node has a character value, a boolean flag that indicates whether the node represents the end of a string, and a dictionary that maps characters to child nodes.


public class TrieNode
{
public char Value { get; set; }
public bool IsEndOfWord { get; set; }
public Dictionary Children { get; set; }

public TrieNode(char value)
{
Value = value;
IsEndOfWord = false;
Children = new Dictionary();
}
}

Next, we need to define a Trie class that provides methods for inserting, searching, and deleting strings in the Trie. The Trie class has a single root node that represents the empty string.


public class Trie
{
private TrieNode root;

public Trie()
{
root = new TrieNode('\0');
}

public void Insert(string word)
{
TrieNode current = root;

foreach (char c in word)
{
if (!current.Children.ContainsKey(c))
{
current.Children = new TrieNode(c);
}

current = current.Children;
}

current.IsEndOfWord = true;
}

public bool Search(string word)
{
TrieNode current = root;

foreach (char c in word)
{
if (!current.Children.ContainsKey(c))
{
return false;
}

current = current.Children;
}

return current.IsEndOfWord;
}

public void Delete(string word)
{
Delete(root, word, 0);
}

private bool Delete(TrieNode current, string word, int index)
{
if (index == word.Length)
{
if (!current.IsEndOfWord)
{
return false;
}

current.IsEndOfWord = false;

return current.Children.Count == 0;
}

char c = word[index];

if (!current.Children.ContainsKey(c))
{
return false;
}

bool shouldDeleteCurrentNode = Delete(current.Children, word, index + 1);

if (shouldDeleteCurrentNode)
{
current.Children.Remove(c);

return current.Children.Count == 0;
}

return false;
}
}

Let’s see how we can use the Trie class to insert, search, and delete strings.


Trie trie = new Trie();

trie.Insert("apple");
trie.Insert("banana");
trie.Insert("orange");

Console.WriteLine(trie.Search("apple")); // true
Console.WriteLine(trie.Search("banana")); // true
Console.WriteLine(trie.Search("orange")); // true
Console.WriteLine(trie.Search("grape")); // false

trie.Delete("banana");

Console.WriteLine(trie.Search("banana")); // false

In this example, we create a Trie object and insert three strings: “apple”, “banana”, and “orange”. We then search for these strings and verify that they are present in the Trie. Finally, we delete the string “banana” and verify that it is no longer present in the Trie.

In conclusion, Trie is a powerful data structure that can be used to efficiently store and search for a collection of strings. By using the TrieNode and Trie classes in C#, we can easily implement a Trie and perform operations on it.

What is a Trie in C#?

In conclusion, a Trie is a data structure that is used to efficiently store and retrieve strings. It is particularly useful in scenarios where there is a large number of strings that need to be searched or matched against. In C#, the Trie can be implemented using a variety of techniques, including arrays, linked lists, and hash tables. By using a Trie, developers can significantly improve the performance of their applications, especially when dealing with large amounts of text data. Overall, the Trie is a powerful tool that can help developers build faster and more efficient applications.

Contact Us