Пример #1
0
        public void Add(String word)
        {
            Node current = root;
            Node tmp = null;

            var parts = word.Split(' ');
            foreach (var ch in parts[0])
            {
                if (current.Childs == null) { current.Childs = new Dictionary<int, Node>(); }
                if (!current.Childs.Keys.Contains(ch))
                {
                    tmp = new Node();
                    tmp.NodeKey = ch;
                    tmp.Words.Add(parts[0], int.Parse(parts[1]));
                    current.Childs.Add(ch, tmp);
                }
                else
                {
                    int Sequence = int.Parse(parts[1]);
                    if (current.Words.Count < 10)
                    {
                        current.Words.Add(parts[0], Sequence);
                    }
                    else
                    {
                        int MinValue = current.Words.Values.Min();
                        if (MinValue < Sequence)
                        {
                            current.Words.Remove(current.Words.Where(r => r.Value == MinValue).OrderByDescending(r => r.Key).Select(r => r.Key).First());
                            current.Words.Add(parts[0], Sequence);
                        }
                    }
                }
                current = current.Childs[ch];
            }
        }
Пример #2
0
 public Trie()
 {
     root = new Node() { NodeKey = ' '};
 }