C# Class rm.Trie.Trie

Trie data structure.
Inheritance: ITrie
Show file Open project: rmandvikar/csharp-trie

Private Properties

Property Type Description
AddWord void
GetCount void
GetLongestWords void
GetShortestWords void
GetTrieNode TrieNode
GetTrieNodesStack Stack
GetWords void
RemovePrefix void
RemoveWord int
Trie System
Trim void

Public Methods

Method Description
AddWord ( string word ) : void

Add a word to the Trie.

Clear ( ) : void

Clear all words from the Trie.

Count ( ) : int

Get total word count in the Trie.

GetLongestWords ( ) : ICollection

Get longest words from the Trie.

GetShortestWords ( ) : ICollection

Get shortest words from the Trie.

GetWords ( ) : ICollection

Get all words in the Trie.

GetWords ( string prefix ) : ICollection

Get words for given prefix.

HasWord ( string word ) : bool

Returns true or false if the word is present in the Trie.

RemovePrefix ( string prefix ) : void

Remove words by prefix from the Trie.

RemoveWord ( string word ) : int

Remove word from the Trie.

UniqueCount ( ) : int

Get unique word count in the Trie.

WordCount ( string word ) : int

Returns the count for the word in the Trie.

Private Methods

Method Description
AddWord ( TrieNode trieNode, char word ) : void

Recursive method to add word. Gets the first char of the word, creates the child TrieNode if null, and recurses with the first char removed from the word. If the word length is 0, return.

GetCount ( TrieNode trieNode, Wrapped count, bool isUnique ) : void

Get word count in the Trie.

GetLongestWords ( TrieNode trieNode, ICollection longestWords, StringBuilder buffer, Wrapped length ) : void

Recursive method to get longest words starting from given TrieNode.

GetShortestWords ( TrieNode trieNode, ICollection shortestWords, StringBuilder buffer, Wrapped length ) : void

Recursive method to get shortest words starting from given TrieNode.

GetTrieNode ( string prefix ) : TrieNode

Get the equivalent TrieNode in the Trie for given prefix. If prefix not present, then return null.

GetTrieNodesStack ( string s, bool isWord = true ) : Stack

Get stack of trieNodes for given string.

GetWords ( TrieNode trieNode, ICollection words, StringBuilder buffer ) : void

Recursive method to get all the words starting from given TrieNode.

RemovePrefix ( Stack trieNodes ) : void

Remove prefix and trim.

RemoveWord ( Stack trieNodes ) : int

Remove word and trim.

Trie ( ) : System

Create a new Trie instance.

Trim ( Stack trieNodes ) : void

Removes unneeded trieNodes going up from a trieNode to root.

TrieNode, except root, that is not a word or has no children can be removed.

Method Details

AddWord() public method

Add a word to the Trie.
public AddWord ( string word ) : void
word string
return void

Clear() public method

Clear all words from the Trie.
public Clear ( ) : void
return void

Count() public method

Get total word count in the Trie.
public Count ( ) : int
return int

GetLongestWords() public method

Get longest words from the Trie.
public GetLongestWords ( ) : ICollection
return ICollection

GetShortestWords() public method

Get shortest words from the Trie.
public GetShortestWords ( ) : ICollection
return ICollection

GetWords() public method

Get all words in the Trie.
public GetWords ( ) : ICollection
return ICollection

GetWords() public method

Get words for given prefix.
public GetWords ( string prefix ) : ICollection
prefix string
return ICollection

HasWord() public method

Returns true or false if the word is present in the Trie.
public HasWord ( string word ) : bool
word string
return bool

RemovePrefix() public method

Remove words by prefix from the Trie.
public RemovePrefix ( string prefix ) : void
prefix string
return void

RemoveWord() public method

Remove word from the Trie.
public RemoveWord ( string word ) : int
word string
return int

UniqueCount() public method

Get unique word count in the Trie.
public UniqueCount ( ) : int
return int

WordCount() public method

Returns the count for the word in the Trie.
public WordCount ( string word ) : int
word string
return int