示例#1
0
        private string CompleteWord(string prefix, out CompletionTreeNode node)
        {
            StringBuilder sb = new StringBuilder(100);

            node = this.root;
            if (String.IsNullOrEmpty(prefix))
            {
                return(String.Empty);
            }
            // Advance all the prefix on the tree.
            // If a character is not found, the prefix can not be completed so it's returned as is
            for (int i = 0; i < prefix.Length; ++i)
            {
                node = node[prefix[i]];
                if (node == null)
                {
                    return(String.Empty);
                }
            }
            // At this point we are done with the prefix.
            // It must go over the three while the corrent node has only one child
            // and untill a EndOfWord is reached
            while (node != null)
            {
                if ((node.Children.Count != 1) || node.EndOfWord)
                {
                    break;
                }
                node = node.First;
                sb.Append(node.Value);
            }
            return(sb.ToString());
        }
示例#2
0
        public CompletionTreeNode Add(char value)
        {
            if (this.Children.ContainsKey(value))
            {
                return(this.Children[value]);
            }

            CompletionTreeNode node = new CompletionTreeNode(value);

            this.Children.Add(value, node);
            return(node);
        }
示例#3
0
        public void AddWord(string word)
        {
            word = word.Trim();
            CompletionTreeNode node  = this.root;
            CompletionTreeNode pNode = null;

            for (int i = 0; i < word.Length; ++i)
            {
                pNode = node;
                node  = node.Add(word[i]);
                if (Scanner.IsSpace(word[i]))
                {
                    node.EndOfWord = true;
                    if ((pNode.Children.Count == 1) && pNode.EndOfWord)
                    {
                        pNode.EndOfWord = false;
                    }
                }
            }
            node.EndOfWord = true;
        }
示例#4
0
 public CompletionTree()
 {
     this.root = new CompletionTreeNode((char)0);
 }