private bool Contains(string word, WordTreeNode node) { if (word.Length == 0) return true; if (word.Length == 1) return word[0] == node.val; if (word[0] == node.val) return node.children.Any(x => Contains(word.Substring(1), x)); return false; }
public static WordTreeNode Create() { var root = new WordTreeNode(); var seq = Enum.GetValues(typeof(T)) .Cast <T>() .Select(v => new { EnumValue = v, TextValue = v.ToString() }); foreach (var a in seq) { root.Add(a.TextValue, a.EnumValue); } return(root); }
public void Add(string s, T t) { if (!string.IsNullOrEmpty(s)) { var ch = _UPPER_INVARIANT_MAP[s[0]]; if (!_CurrentLevel.TryGetValue(ch, ref _this_next)) { _this_next = new WordTreeNode(); _CurrentLevel.Add(ch, _this_next); } _this_next.Add(s.Substring(1), t); } else { _TValue = t; } }
private void AddWord(string word, WordTreeNode node) { if (word.Length == 0) return; WordTreeNode child; if (!node.children.Any(x => x.val == word[0])) { child = new WordTreeNode() { val = word[0], children = new List<WordTreeNode>() }; node.children.Add(child); } else { child = node.children.FirstOrDefault(x => x.val == word[0]); } AddWord(word.Substring(1), child); }
public async Task GivenTreeNode_WhenGetShortestPath_ExpectSpecificPath() { var wordTree = new WordTreeNode(new Word("side")) .AddChildren( new WordTreeNode(new Word("size")) ).AddChildren( new WordTreeNode(new Word("tize")) .AddChildren( new WordTreeNode(new Word("ture")) ) .AddChildren( new WordTreeNode(new Word("tore")) .AddChildren( new WordTreeNode(new Word("sore")) .AddChildren( new WordTreeNode(new Word("sort")) ) ) ) ).AddChildren( new WordTreeNode(new Word("sode")) .AddChildren( new WordTreeNode(new Word("sodt")) .AddChildren( new WordTreeNode(new Word("sort")) ) ) ); var shortestPath = await new SearchWordsPath() .GetShortestPath(new Word("side"), new Word("sort"), wordTree); Assert.Equal(4, shortestPath.Count()); Assert.Equal("side", shortestPath.ElementAt(0).Value); Assert.Equal("sode", shortestPath.ElementAt(1).Value); Assert.Equal("sodt", shortestPath.ElementAt(2).Value); Assert.Equal("sort", shortestPath.ElementAt(3).Value); }
public EnumParser() { _FirstWordTrieNode = WordTreeNode.Create(); }
public WordTree(string word) { this.root = new WordTreeNode() { val = char.ToUpper(word[0]), children = new List<WordTreeNode>() }; }
public WordTree(char root) { this.root = new WordTreeNode() { val = char.ToUpper(root), children = new List<WordTreeNode>() }; }