473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Implementation of a string trie

I've seen a few people asking for this elsewhere, so here's a basic
implementation of a string trie in C#. I'm using this to load and
search a dictionary of about two hundred thousand words. It's fairly
quick (a couple of seconds for the full load). It's a bit of a memory
hog -- about 50MB resident, once the file is loaded -- but most of my
C#/.NET programs are.

Example usage:

StringTrie st = new StringTrie();
st.LoadFromFile("wordlist.txt");
st.Add("additionalword");
st.Add("additionalotherword");
if ( st.Search("myword") == StringTrie.Found )
etc...
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace TrieLib
{
class StringTrie
{
private StringTrieBucket bucket = new StringTrieBucket(' ');

public enum SearchResults
{
NotFound,
Found,
FoundPartial
}

public StringTrie()
{
}

public void LoadFromFile(string pathname)
{
TextReader tr = File.OpenText(pathname);

string line;
while ( (line = tr.ReadLine() ) != "")
{
string[] sa = line.Split(' ');
foreach (string s in sa)
if (s.Length < 11)
Add(s);
}
tr.Close();
GC.Collect();
}

public void Add(string s)
{
Queue<charletters = new
Queue<char>(s.ToLower().ToCharArray());
AddToTrie(bucket, letters);
}

public SearchResults Search(string s)
{
Queue<charletters = new
Queue<char>(s.ToLower().ToCharArray());
return SearchTrie(bucket, letters);
}

private SearchResults SearchTrie(StringTrieBucket b,
Queue<charletters)
{
if (letters.Count == 0)
if (b.terminal)
return SearchResults.Found;
else
return SearchResults.FoundPartial;

Char c = letters.Dequeue();
StringTrieBucket bucket = b;
foreach (StringTrieBucket stb in b.kids)
if (stb.letter == c)
return SearchTrie(stb, letters);

return SearchResults.NotFound;
}

private void AddToTrie(StringTrieBucket b, Queue<char>
letters)
{
if (letters.Count == 0)
{
b.terminal = true;
return;
}
Char c = letters.Dequeue();
StringTrieBucket bucket = null;
bool found = false;
foreach (StringTrieBucket stb in b.kids)
if (stb.letter == c)
{
bucket = stb;
found = true;
break;
}
if (! found)
{
bucket = new StringTrieBucket(c);
b.kids.Add(bucket);
}
AddToTrie(bucket, letters);
}
}

class StringTrieBucket
{
internal char letter;
internal List<StringTrieBucketkids;
internal bool terminal;

internal StringTrieBucket(char c)
{
this.letter = c;
kids = new List<StringTrieBucket>();
terminal = false;
}
}
}

Nov 8 '07 #1
0 2046

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
21
by: Chris S. | last post by:
I have a number of strings, containing wildcards (e.g. 'abc#e#' where # is anything), which I want to match with a test string (e.g 'abcdef'). What would be the best way for me to store my strings...
0
by: Polar | last post by:
Hi! Do you know a C library for trie data structures (a particular type of tree)? And some link about trie with C language? Thank you Polar
3
by: Paul | last post by:
Does anyone have an example of a trie data structure implemented in c#? I'm looking for a string trie to use to hold the word list for my spell checker. Any examples or suggestions would be great....
1
by: openleren | last post by:
Hi all, with a little help from my friends, I am trying to construct a connectionstring from a relative path in web.config. It contains <add key="conAccess" value="microsoft.jet.oledb.4.0;data...
5
by: csharpula csharp | last post by:
Hello , I need to perform the following operation: There are two strings (str1 and strSet) ,I need to write a function which will get str1 and strSet and will return an index of the first...
0
by: anto.anish | last post by:
Hi , Since, i did not want to write instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of template...
1
by: anto.anish | last post by:
Hi , Since, i did not want to write explicit instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of...
5
by: iaminsik | last post by:
I implemented a TRIE in memory by myself in C. But, as you know, when a size of data increases, loading time of data becomes longer. I want to implement a TRIE in file, which means division of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.