Beispiel #1
0
        // Returns all values whose key begin with k
        public List <ValueType> AutoComplete(String key)
        {
            var result = new List <ValueType>();
            TrieNodeArray <ValueType> tmpNode = null;
            var currentNode = root;

            // iterate characters of key
            for (int i = 0; i < key.Length; i++)
            {
                char ch = key[i];
                // if we find a child node with key matching character
                if ((tmpNode = currentNode.FindChildNode(ch)) != null)
                {
                    // set our current node as child node
                    currentNode = tmpNode;

                    // if we are on the last character
                    // return all terminal nodes from this node
                    if (i == (key.Length - 1))
                    {
                        foreach (var node in currentNode.TerminalNodes())
                        {
                            result.Add(node.Value);
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
 public TrieNodeArray()
 {
     Character = default(char);
     Parent    = null;
     Children  = new TrieNodeArray <ValueType> [MAXCHARS];
     Value     = default(ValueType);
 }
Beispiel #3
0
 // Adds the node to first available child node
 // adds optional value if its the last character
 public void AddToChildren(TrieNodeArray <ValueType> node, ValueType value = default(ValueType))
 {
     for (int i = 0; i < Children.Length; i++)
     {
         if (Children[i] == null)
         {
             Children[i]       = node;
             Children[i].Value = value;
             break;
         }
     }
 }
Beispiel #4
0
        // Removes key/value pair from Trie
        // Returns true if successful
        public bool Remove(String key)
        {
            var currentNode = root;
            TrieNodeArray <ValueType> tmpNode = null;

            for (int i = 0; i < key.Length; i++)
            {
                char ch = key[i];

                if ((tmpNode = currentNode.FindChildNode(ch)) != null)
                {
                    // we are on the last character of the key
                    // and the is a child node that is terminal
                    if ((i == (key.Length - 1)) && (tmpNode.Value != null))
                    {
                        tmpNode.Remove();
                        return(true);
                    }
                }
                currentNode = tmpNode;
            }
            return(false);
        }
Beispiel #5
0
        // Returns value v that corresponds to full key k
        public ValueType Translate(String key)
        {
            var currentNode = root;
            TrieNodeArray <ValueType> tmpNode = null;

            // iterate characters of key
            for (int i = 0; i < key.Length; i++)
            {
                char ch = key[i];

                if ((tmpNode = currentNode.FindChildNode(ch)) != null)
                {
                    // if we are on last character and child node exists thats terminal
                    if ((i == (key.Length - 1)) && (tmpNode.Value != null))
                    {
                        // return the value of this child node
                        return(tmpNode.Value);
                    }
                    currentNode = tmpNode;
                }
            }
            return(default(ValueType));
        }
Beispiel #6
0
        // Add key/value pair to Trie
        // Update value if key already exists
        public void AddWord(String key, ValueType value)
        {
            // key must be something!
            if (key == null)
            {
                throw new ArgumentException("key cannot be null");
            }

            // value must be something!
            if (value == null)
            {
                throw new ArgumentException("value cannot be null");
            }

            TrieNodeArray <ValueType> currentNode = root;
            TrieNodeArray <ValueType> tmpNode     = null;

            // iterate through each key character
            for (int i = 0; i < key.Length; i++)
            {
                char ch = key[i];

                // iterate through each child of currentNode
                for (int j = 0; j < currentNode.Children.Length; j++)
                {
                    // node exists, follow the path
                    TrieNodeArray <ValueType> nodeExists = currentNode.FindChildNode(ch);
                    if (nodeExists != null)
                    {
                        // follow the if it exists, has a value
                        // and we are not on the last character
                        if (nodeExists.Value != null && (i != (key.Length - 1)))
                        {
                            currentNode = nodeExists;
                            break;
                        }
                        // if the node exists and we are on the last character
                        // update the value
                        else if (i == (key.Length - 1))
                        {
                            nodeExists.Value = value;
                            break;
                        }
                        else
                        {
                            // otherwise, continue down the path
                            currentNode = nodeExists;
                            break;
                        }
                    }
                    // create the character
                    else
                    {
                        tmpNode = new TrieNodeArray <ValueType>()
                        {
                            Character = ch, Parent = currentNode
                        };
                        // we are at the end of the key, save the value
                        if (i == (key.Length - 1))
                        {
                            currentNode.AddToChildren(tmpNode, value);
                            break;
                        }
                        else
                        {
                            currentNode.AddToChildren(tmpNode);
                            currentNode = currentNode.FindChildNode(ch);
                            break;
                        }
                    }
                }
            }
        }
Beispiel #7
0
 // removes node holding value
 public void Remove()
 {
     Character = default(char);
     Value     = default(ValueType);
     Parent    = null;
 }