public bool Contains(String word) { TreeNode tempNode = this.root; char[] charArray = word.ToCharArray(); for (int i = 0; i < charArray.Length; i++) { TreeNode node = new TreeNode(); node.Character = charArray[i]; if (!tempNode.ContainsChild(node)) return false; else tempNode = tempNode.GetChildren(charArray[i]); } if (tempNode.IsWordEnd) return true; else return false; }
public void Add(String word) { TreeNode tempNode = this.root; char[] charArray = word.ToCharArray(); for (int i = 0; i < charArray.Length; i++) { TreeNode node = new TreeNode(); node.Character = charArray[i]; if (!tempNode.ContainsChild(node)) { tempNode.AppendChild(node); tempNode = node; } else { tempNode = tempNode.GetChildren(charArray[i]); } } tempNode.IsWordEnd = true; }
public void AppendChild(TreeNode child) { child.parent = this; this.children.Add(child.character, child); }
public TreeNode(char character, bool isWordEnd, TreeNode parent) { this.character = character; this.isWordEnd = isWordEnd; this.parent = parent; }
public void RemoveChild(TreeNode child) { this.children.Remove(child.character); }
public bool ContainsChild(TreeNode child) { return this.children.ContainsKey(child.character); }
public bool Associate(char nextChar) { if (currNode.ContainsKey(nextChar)) { step++; this.currNode = currNode.GetChildren(nextChar); if (currNode.IsWordEnd) this.isOccurWord = true; return true; } else return false; }
public AssociateStream() { tree = WordTreeFactory.GetInstance(); currNode = Root; }
public void Reset() { this.step = 0; this.currNode = this.Root; this.isOccurWord = false; }
public bool Move(string word) { char[] charArray = word.ToCharArray(); for (int i = 0; i < charArray.Length; i++) { if (!Associate(charArray[i])) { step -= i; for(;i > 0;i--) this.currNode = this.currNode.ParentNode; return false; } } return this.currNode.IsWordEnd; }
public void DeleteWord(string word) { Reset(); if (Move(word) && currNode.IsWordEnd) { if (currNode.HasChildren) { currNode.IsWordEnd = false; } else { do { TreeNode tempNode = currNode.ParentNode; tempNode.RemoveChild(currNode); currNode = tempNode; } while (!Root.Equals(currNode) && !currNode.IsWordEnd && !currNode.HasChildren); } } Reset(); }
public bool BackToLastWordEnd() { TreeNode tempNode = currNode; do { tempNode = tempNode.ParentNode; step--; if (tempNode.IsWordEnd) { currNode = tempNode; return true; } } while (tempNode.ParentNode != null); return false; }
private WordTree() { root = new TreeNode(); }