Esempio n. 1
0
        // Delete is just to set the value to null.

        private TernaryTrieNode <V> PutHelper(string key, V value, TernaryTrieNode <V> node, int d)
        {
            char charKey = key.ElementAt(d);

            if (node == null)
            {
                node = new TernaryTrieNode <V> {
                    CharKey = charKey
                };
            }

            var response = charKey.CompareTo(node.CharKey);

            if (response < 0)
            {
                node.Left = PutHelper(key, value, node.Left, d);
            }
            else if (response > 0)
            {
                node.Right = PutHelper(key, value, node.Right, d);
            }
            else if (d < key.Length - 1)
            {
                node.Middle = PutHelper(key, value, node.Right, d + 1);
            }
            else
            {
                node.Value = value;
            }
            return(node);
        }
Esempio n. 2
0
        // Search miss is the beauty here.
        private TernaryTrieNode <V> GetHelper(string key, TernaryTrieNode <V> node, int d)
        {
            if (d == key.Length || node == null)
            {
                return(node);
            }
            char c      = key.ElementAt(d);
            var  parent = node.Get(c);

            return(GetHelper(key, parent, d + 1));
        }
Esempio n. 3
0
        private int SearchTrie(string key, TernaryTrieNode <V> node, int d, int length)
        {
            if (node == null)
            {
                return(length);
            }
            if (node.Value != null)
            {
                length = d;
            }
            if (d == key.Length)
            {
                return(length);
            }
            char c      = key.ElementAt(d);
            var  parent = node.Get(c);

            return(SearchTrie(key, parent, d + 1, length));
        }
Esempio n. 4
0
 private void TraverseTrie(TernaryTrieNode <V> node, Queue <string> output, StringBuilder builder)
 {
     if (node == null)
     {
         builder.Clear();
         return;
     }
     builder.Append(node.CharKey);
     if (node.Value != null)
     {
         output.Enqueue(builder.ToString());
     }
     else
     {
         foreach (var child in new[] { node.Left, node.Middle, node.Right })
         {
             TraverseTrie(child, output, builder);
         }
     }
 }
Esempio n. 5
0
 public void put(string key, V value)
 {
     _root = PutHelper(key, value, _root, 0);
 }