/// <summary> /// Initializes a new instance of the <see cref="TrieTreeItem" /> class. /// </summary> /// <param name="c"> /// The c. /// </param> /// <param name="father"> /// The father. /// </param> /// <param name="floor"> /// The floor. /// </param> /// <param name="id"> /// The id. /// </param> public TrieTreeItem(string c, TrieTreeItem father, int floor, bool isTerminal) { this.Ch = c; this.Father = father; this.Floor = floor; this.IsTerminal = isTerminal; this.Children = new Dictionary<string, TrieTreeItem>(); this.GetWord(); }
/// <summary> /// Initializes a new instance of the <see cref="TrieTreeItem" /> class. /// </summary> /// <param name="c"> /// The c. /// </param> /// <param name="father"> /// The father. /// </param> /// <param name="floor"> /// The floor. /// </param> /// <param name="id"> /// The id. /// </param> public TrieTreeItem(char c, TrieTreeItem father, int floor, bool isTerminal) : this(c.ToString(), father, floor, isTerminal) { }
/// <summary> /// </summary> /// <param name="root"> /// The root. /// </param> /// <param name="wordList"> /// The word list. /// </param> private void ParseNode(TrieTreeItem root, List<string> wordList) { foreach (var pair in root.Children) { if (pair.Value.IsTerminal) { wordList.Add(pair.Value.Word); } this.ParseNode(pair.Value, wordList); } }
/// <summary> /// Initializes a new instance of the <see cref="TrieTree" /> class. /// </summary> /// <param name="isCaseSensitive"> /// The is case sensitive. /// </param> public TrieTree(bool isCaseSensitive) { this._root = new TrieTreeItem('#', null, RootFloorValue, false); this.IsCaseSensitive = false; this.TreeItemCount = 0; }
/// <summary> /// </summary> /// <param name="str"> /// The str. /// </param> /// <param name="isInsert"> /// The is insert. /// </param> /// <returns> /// </returns> private TrieTreeItem IsInTree(string str, bool isInsert) { int floor = RootFloorValue + 1; int start = floor; int end = str.Length - 1; TrieTreeItem curRoot = this._root; bool isFound = true; for (int i = 0; i < end; i++) { string curChar = str[i].ToString(); isFound = curRoot.Children.ContainsKey(curChar); if (isFound) { floor++; curRoot = curRoot.Children[curChar]; } else { break; } } // check last is terminal if (isFound) { string curChar = str[end].ToString(); isFound = curRoot.Children.ContainsKey(curChar) && curRoot.Children[curChar].IsTerminal; } if (isFound) { return curRoot; } if (isInsert) { start = floor; for (int j = start; j < end; j++) { char curChar = str[j]; var treeItem = new TrieTreeItem(curChar, curRoot, floor, false); curRoot.Children.Add(curChar.ToString(), treeItem); curRoot = treeItem; floor++; } // insert last char as terminal node char lastChar = str[end]; if (curRoot.Children.ContainsKey(lastChar.ToString())) { curRoot.Children[lastChar.ToString()].IsTerminal = true; return curRoot.Children[lastChar.ToString()]; } var terminalTreeItem = new TrieTreeItem(lastChar, curRoot, floor, true); curRoot.Children.Add(lastChar.ToString(), terminalTreeItem); return terminalTreeItem; } return null; }