/// <summary> /// Adds the given <paramref name="word"/> to the Tree /// </summary> /// <param name="node">The current <see cref="TreeNode"/></param> /// <param name="position">The current position within the <paramref name="word"/></param> /// <param name="word">The word that should be added</param> protected void AddNode(TreeNode node, int position, string word) { if (word.Length < position) { //Cannot process this word further, because it is not long enough for the position; this should not happen! throw new IndexOutOfRangeException("Position parameter is too long for the given word"); } if (word.Length == position) { //We have found a word :) node.IsWord = true; return; } var nextChar = word.Substring(position, 1); if (node.ChildNodes.ContainsKey(nextChar)) { //We already know the first characters of the word and do not have to process it any further AddNode(node.ChildNodes[nextChar], position + 1, word); } else { //We do not know the next character and we discover uncharted territory. Add the word. var newNode = new TreeNode { Parent = node, Path = word.Substring(0, position + 1) }; node.ChildNodes.Add(nextChar, newNode); //Let's process the following content AddNode(newNode, position + 1, word); } }
public TreeNodeManager() { Root = new TreeNode(); }