/// <summary> /// Add word to trie /// </summary> public void Add(string word, TRecord record) { if (string.IsNullOrEmpty(word)) { throw new ArgumentException("Word is empty or null."); } var current = _root; for (var i = 0; i < word.Length; ++i) { if (!current.Children.ContainsKey(word[i])) { var newTrieNode = new TrieMapNode <TRecord>(word[i], default(TRecord)); newTrieNode.Parent = current; current.Children.Add(word[i], newTrieNode); } current = current.Children[word[i]]; } if (current.IsTerminal) { throw new ApplicationException("Word already exists in Trie."); } ++_count; current.IsTerminal = true; current.Record = record; }
/// <summary> /// IComparer interface implementation /// </summary> public int CompareTo(TrieMapNode <TRecord> other) { if (other == null) { return(-1); } return(Key.CompareTo(other.Key)); }
/// <summary> /// Clears this insance. /// </summary> public void Clear() { _count = 0; _root.Clear(); _root = new TrieMapNode <TRecord>(' ', default(TRecord), false); }
/// <summary> /// CONSTRUCTOR /// </summary> public TrieMap() { _count = 0; _root = new TrieMapNode <TRecord>(' ', default(TRecord), false); }