예제 #1
0
 public void Insert(string word, int usesCount = 1)
 {
     if (!Search(word))
     {
         HeapTrieNode current = root;
         HeapTrieNode previous;
         foreach (char x in word.ToCharArray())
         {
             previous = current;
             HeapTrieNode child = current.GetChild(x);
             if (child != null)
             {
                 current      = child;
                 child.Parent = previous;
             }
             else
             {
                 current.Children.AddLast(new HeapTrieNode(x));
                 current        = current.GetChild(x);
                 current.Parent = previous;
             }
         }
         current.IsEnd        = true;
         current.SearchCount += usesCount;
     }
 }
예제 #2
0
        public Heap GetWordsHeap()
        {
            Heap heap = new Heap();

            if (IsEnd)
            {
                heap.Add(new HeapNode(ToWord(), SearchCount));
            }
            if (Children.Any())
            {
                for (int i = 0; i < Children.Count; i++)
                {
                    HeapTrieNode currentChild = Children.ElementAt(i);
                    if (currentChild != null)
                    {
                        var childHeap = currentChild.GetWordsHeap();
                        for (int j = 0; j < childHeap.Size; j++)
                        {
                            heap.Add(childHeap.Nodes.ElementAt(j));
                        }
                    }
                }
            }
            return(heap);
        }
예제 #3
0
        public Dictionary <string, int> FindMatches(string prefix)
        {
            HeapTrieNode lastNode = root;

            for (int i = 0; i < prefix.Length; i++)
            {
                lastNode = lastNode.GetChild(prefix[i]);
                if (lastNode == null)
                {
                    return(new Dictionary <string, int>());
                }
            }
            return(lastNode.GetWords());
        }
예제 #4
0
        public Heap FindMatchesHeap(string prefix)
        {
            HeapTrieNode lastNode = root;

            for (int i = 0; i < prefix.Length; i++)
            {
                lastNode = lastNode.GetChild(prefix[i]);
                if (lastNode == null)
                {
                    return(new Heap());
                }
            }
            return(lastNode.GetWordsHeap());
        }
예제 #5
0
        public bool Search(string word)
        {
            HeapTrieNode current = root;

            foreach (char x in word.ToCharArray())
            {
                HeapTrieNode child = current.GetChild(x);
                if (child != null)
                {
                    current = current.GetChild(x);
                }
                else
                {
                    return(false);
                }
            }
            if (current.IsEnd)
            {
                current.SearchCount += 1;
                return(true);
            }
            return(false);
        }
예제 #6
0
        public Dictionary <string, int> GetWords()
        {
            Dictionary <string, int> dictionary = new Dictionary <string, int>();

            if (IsEnd)
            {
                dictionary.Add(ToWord(), SearchCount);
            }
            if (Children.Any())
            {
                for (int i = 0; i < Children.Count; i++)
                {
                    HeapTrieNode currentChild = Children.ElementAt(i);
                    if (currentChild != null)
                    {
                        foreach (var item in currentChild.GetWords())
                        {
                            dictionary.Add(item.Key, item.Value);
                        }
                    }
                }
            }
            return(dictionary);
        }
예제 #7
0
 public HeapTrie()
 {
     root = new HeapTrieNode(' ');
 }