Beispiel #1
0
 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;
 }
Beispiel #2
0
 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;
 }
Beispiel #3
0
 public void AppendChild(TreeNode child)
 {
     child.parent = this;
     this.children.Add(child.character, child);
 }
Beispiel #4
0
 public TreeNode(char character, bool isWordEnd, TreeNode parent)
 {
     this.character = character;
     this.isWordEnd = isWordEnd;
     this.parent = parent;
 }
Beispiel #5
0
 public void RemoveChild(TreeNode child)
 {
     this.children.Remove(child.character);
 }
Beispiel #6
0
 public bool ContainsChild(TreeNode child)
 {
     return this.children.ContainsKey(child.character);
 }
Beispiel #7
0
        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;
        }
Beispiel #8
0
 public AssociateStream()
 {
     tree = WordTreeFactory.GetInstance();
     currNode = Root;
 }
Beispiel #9
0
 public void Reset()
 {
     this.step = 0;
     this.currNode = this.Root;
     this.isOccurWord = false;
 }
Beispiel #10
0
 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;
 }
Beispiel #11
0
 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();
 }
Beispiel #12
0
 public bool BackToLastWordEnd()
 {
     TreeNode tempNode = currNode;
     do
     {
         tempNode = tempNode.ParentNode;
         step--;
         if (tempNode.IsWordEnd)
         {
             currNode = tempNode;
             return true;
         }
     } while (tempNode.ParentNode != null);
     return false;
 }
Beispiel #13
0
 private WordTree()
 {
     root = new TreeNode();
 }