Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a trie in c# [closed]

Does anyone know where I can find an example of how to construct a trie in C#? I'm trying to take a dictionary/list of words and create a trie with it.

like image 918
locoboy Avatar asked Jun 20 '11 19:06

locoboy


People also ask

How do you declare a trie?

If the input key is new or an extension of the existing key, we need to construct non-existing nodes of the key, and mark the end of the word for the last node. If the input key is a prefix of the existing key in Trie, we simply mark the last node of the key as the end of a word. The key length determines Trie depth.

What is trie data structure in C?

A Trie data structure acts as a container for a dynamic array. In this article, we shall look at how we can implement a Trie in C/C++. This is based on the tree data structure but does not necessarily store keys. Here, each node only has a value, which is defined based on the position.

Does C++ have a trie?

This post covers the C++ implementation of the Trie data structure, which supports insertion, deletion, and search operations. We know that Trie is a tree-based data structure used for efficient retrieval of a key in a huge set of strings.

Is trie in STL?

Iterator: trie<T>::iteratorIterators are a very important part of STL. Trie project also has iterators to support the same.


2 Answers

This is my own code, pulled from my answer to How to find a word from arrays of characters? :

public class Trie {   public struct Letter   {     public const string Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";     public static implicit operator Letter(char c)     {       return new Letter() { Index = Chars.IndexOf(c) };     }     public int Index;     public char ToChar()     {       return Chars[Index];     }     public override string ToString()     {       return Chars[Index].ToString();     }   }    public class Node   {     public string Word;     public bool IsTerminal { get { return Word != null; } }     public Dictionary<Letter, Node> Edges = new Dictionary<Letter, Node>();   }    public Node Root = new Node();    public Trie(string[] words)   {     for (int w = 0; w < words.Length; w++)     {       var word = words[w];       var node = Root;       for (int len = 1; len <= word.Length; len++)       {         var letter = word[len - 1];         Node next;         if (!node.Edges.TryGetValue(letter, out next))         {           next = new Node();           if (len == word.Length)           {             next.Word = word;           }           node.Edges.Add(letter, next);         }         node = next;       }     }   } 
like image 122
Fantius Avatar answered Oct 02 '22 20:10

Fantius


Take a look at this codeplex project:

https://github.com/gmamaladze/trienet

It is a library containing several different variants of well tested generic c# trie classes including patricia trie and parallel trie.

  • Trie – the simple trie, allows only prefix search, like .Where(s => s.StartsWith(searchString))
  • SuffixTrie - allows also infix search, like .Where(s => s.Contains(searchString))
  • PatriciaTrie – compressed trie, more compact, a bit more efficient during look-up, but a quite slower durig build-up.
  • SuffixPatriciaTrie – the same as PatriciaTrie, also enabling infix search.
  • ParallelTrie – very primitively implemented parallel data structure which allows adding data and retriving results from different threads simultaneusly.
like image 33
George Mamaladze Avatar answered Oct 02 '22 20:10

George Mamaladze