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;
     }
 }
Example #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);
        }
        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());
        }
        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());
        }
        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);
        }
Example #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);
        }
 public HeapTrie()
 {
     root = new HeapTrieNode(' ');
 }