Exemplo n.º 1
0
        //Adds the given child to the node
        public Node addChild(char letter)
        {
            //Checks if given node has any children
            //if not creates dictionary for them
            if (children == null)
            {
                children = new Dictionary<char, Node>();
            }

            //Checks if the given node already has mapping for the letter
            //If not, creates a mapping to it
            if (!children.ContainsKey(letter))
            {
                //checks if letter isnt EoW letter
                if (letter == endOfWord)
                {
                    children.Add(letter, null);
                    return null;
                }
                else
                {
                    var node = new Node(letter);
                    children.Add(letter, node);
                    return node;
                }
            }

            //returns of a node representation of the child with the given letter
            return (Node)children[letter];
        }
Exemplo n.º 2
0
        //Private helper method to recursively call a match, in order to fetch
        //all the results stemming from a given substring input
        //Param node: the node being inspected, going to be branched out from
        //Param allWords: the continually building set of all terminal node words
        //Param currentStr: The current set of letters which is all the letters of the nodes in the path it took to get where it is
        //Param substring: The substring of the possible words one is searching for
        //param numResults: the max number of matches of words that one wants
        private static void matchRecursive(Node node, List<string> allWords, string currentStr, string substr, int numResults)
        {
            //if the set has already reached the desired amount
            if (allWords.Count == numResults)
            {
                return;
            }

            //if node passed is null, which happens when reached a terminal word
            if (node == null)
            {
                //if list doesn't contain the string, add the string to it, and return
                if (!allWords.Contains(currentStr))
                {
                    allWords.Add(currentStr);
                }
                return;
            }

            //concatenate the current letters to add the given node letter
            currentStr += node.letter.ToString();

            //if the string isn't empty
            if (substr.Length > 0)
            {
                //if the node has a mapping to a child with the first letter in the substring
                if (node.children.ContainsKey(substr[0]))
                {
                    //recursively call the child node, and then chop off first letter, and keep going
                    matchRecursive(node.children[substr[0]], allWords, currentStr, substr.Remove(0, 1), numResults);
                }
            }
            //else substring was empty, so get all other keys associated
            else
            {
                foreach (char key in node.children.Keys)
                {
                    matchRecursive(node.children[key], allWords, currentStr, substr, numResults);
                }
            }
        }
Exemplo n.º 3
0
 //Initilizes Tree, sets root node
 public Trie()
 {
     root = new Node { letter = Node.root };
 }