public AlphaTrieNode(string _key, T _value)
 {
     Value = new KeyValueReference <string, T>(_key, _value);
 }
 public AlphaTrieNode(KeyValueReference <string, T> KVR)
 {
     Value = KVR;
 }
        private void Insert(AlphaTrieNode node, KeyValueReference <string, T> insKV, int level)
        {
            if (node.Value != null && node.Value.Key == insKV.Key)
            {
                throw new InvalidOperationException("Attempted to insert duplicate key into the trie.");
            }

            if (node.Nodes == null && node.Value == null)
            {
                node.Value = insKV;
                return;
            }

            if (node.Nodes == null && node.Value != null)
            {
                node.Nodes = new AlphaTrieNode[NodeCount];
                // Only move the current Value if it isn't exactly there.
                if (node.Value.Key.Length > level)
                {
                    KeyValueReference <string, T> moveKV = node.Value;
                    node.Value = null;
                    int im = AlphaTrieNode.CharToTrieNodeIndex(moveKV.Key[level]);
                    node.Nodes[im] = new AlphaTrieNode(moveKV);
                    node.Branches++;
                }
                // If ours fits perfectly, we know we had to move the old one.
                if (insKV.Key.Length == level)
                {
                    node.Value = insKV;
                    return;
                }

                int ii = AlphaTrieNode.CharToTrieNodeIndex(insKV.Key[level]);
                if (node.Nodes[ii] == null)
                {
                    node.Nodes[ii] = new AlphaTrieNode(insKV);
                    node.Branches++;
                }
                else
                {
                    Insert(node.Nodes[ii], insKV, level + 1);
                }
                return;
            }

            if (node.Nodes != null)
            {
                // If we go exactly here, stick it in the Value.
                if (insKV.Key.Length == level)
                {
                    node.Value = insKV;
                    return;
                }
                // Otherwise, make new path or follow the old path.
                int ii = AlphaTrieNode.CharToTrieNodeIndex(insKV.Key[level]);
                if (node.Nodes[ii] == null)
                {
                    node.Nodes[ii] = new AlphaTrieNode(insKV);
                    node.Branches++;
                }
                else
                {
                    Insert(node.Nodes[ii], insKV, level + 1);
                }
            }
        }