public void Add(string word, TRecord record) { if (string.IsNullOrEmpty(word)) { throw new ArgumentException("Word is empty or null."); } var current = _root; foreach (char key in word) { var child = current.GetChildrenOrDefault(key); if (child == null) { var newTrieNode = new TrieMapNode <TRecord>(key, default(TRecord)); child = current.AddChildren(key, newTrieNode); } current = child; } if (current.Record != null) { throw new ApplicationException("Word already exists in Trie."); } ++_count; current.Record = record; }
public TrieMap(IEnumerable <TrieMapNode <TRecord> > childs) { _count = 0; _root = new TrieMapNode <TRecord>(' ', default(TRecord)); foreach (var child in childs) { _root.AddChildren(child.Key, child); } }
public TrieMapNode <TRecord> AddChildren(char ch, TrieMapNode <TRecord> newTrieNode) { if (_children == null) { _children = new TrieMapNode <TRecord> [0]; } _children = _children.Concat(new [] { newTrieNode }).OrderBy(x => x.Key).ToArray(); return(newTrieNode); }
public int CompareTo(TrieMapNode <TRecord> other) { if (ReferenceEquals(this, other)) { return(0); } if (ReferenceEquals(null, other)) { return(1); } return(Key.CompareTo(other.Key)); }
private TrieSearchResult <TRecord>[] GetAllChilds(TrieMapNode <TRecord> current, string prefixWord) { var list = new List <TrieSearchResult <TRecord> >(); if (current.Record != null) { list.Add(new TrieSearchResult <TRecord>(prefixWord, current.Record)); } foreach (var currentChild in current.Children) { var newPrefixWord = prefixWord + currentChild.Key; //if (currentChild.Record != null) //list.Add(new TrieSearchResult<TRecord>(newPrefixWord, currentChild.Record)); //else { list.AddRange(GetAllChilds(currentChild, newPrefixWord)); } } return(list.ToArray()); }
public TrieMap() { _count = 0; _root = new TrieMapNode <TRecord>(' ', default(TRecord)); }