public WordsTrees(IEnumerable <string> words) { Dictionary <char, List <string> > firstLetters = new Dictionary <char, List <string> >(); bool allWordsEmpty = true; foreach (var word in words) { if (string.IsNullOrEmpty(word)) { continue; } if (allWordsEmpty) { allWordsEmpty = false; } firstLetters.AddOrUpdateList(word[0], new List <string>() { word }); } int currentKey; string currentKeyS; WordsTreeChild parent = null; foreach (var word in words) { currentKeyS = word; currentKey = -1; parent = null; foreach (var c in word) { currentKey++; currentKeyS += $"{currentKey}{c}"; if (currentKey == 0) { if (!_RootChildren.ContainsKey(c)) { _RootChildren.Add(c, new WordsTreeChildRoot(firstLetters[c], currentKeyS, null, c)); } parent = _RootChildren[c]; } else { if (parent.Children == null || !parent.Children.ContainsKey(c)) { var child = new WordsTreeChild(currentKeyS, parent, c); parent.AddChild(child); parent = child; } else { parent = parent.Children[c]; } } } } }
public WordsTreeChildRoot(IEnumerable <string> possibleWords, string key, WordsTreeChild prev, char ckey) : base(key, prev, ckey) { _PossibleWords = possibleWords.ToArray(); }