Question

In: Computer Science

Root is a trie that has characters for values. When traversed it spells words by formin...

Root is a trie that has characters for values. When traversed it spells words by formin strings.

I'm trying to make findNode such that it will return the node at which we start collecting candidates for words that start with a specific prefix.

How do I code this method traversing depth-first?

my code:

private static TreeNode<Character> findNode(TreeNode<Character> root, String prefix, int index){

Stack <TreeNode<Character>> s = new Stack<>();

s.push(root);

for(TreeNode<Character> child : root.getChildren()){
while(index <= prefix.length()-1){
if(prefix.charAt(index) == child.getValue()){
index++;
findNode(child, prefix, index);
if(prefix.charAt(index) != child.getValue()){
return child;  
}
}
}
}

}

Solutions

Expert Solution

Assuming standard functions such as getChildren() to be present in your implementation of the Trie Class and index is passed as 0 in the beginning.

There is no requirement of a stack in the above code. Since , we want to reach to that node from which , we can start collecting candidates for making new words having that prefix. It actually means traversing the trie to reach to the last letter of the prefix. When we reach and return the last letter of the prefix , we can form all possibe words from there on. Hence , we must return the node with last letter of the prefix starting with the root.

There is a mistake in declaring the Base cases and making the code more efficient . Here is how you can do it in a better way.

private static TreeNode<Character> findNode(TreeNode<Character> root, String prefix, int index)
{
 /* If the root is NULL and has no characters */
 if(root==NULL)
    return root;
 /* Get all its children */
 for(TreeNode<Character> child : root.getChildren())
 {
         /* If the character of root's children matches with Prefix[index] */
         if(prefix.charAt(index) == child.getValue())
         {
            /*Increase the index and search for next character of prefix */
            index++;
            /*If entire prefix is found , return the current root (the last element)*/
            if(index == prefix.length())
                return child;
            /* Other wise , simply called the function recursively */ 
            else 
                return findNode(child, prefix, index);
         }
  }
  /* If no children of root has Prefix[index] character , that means it is not possible to make prefix*/
  return NULL;
}

Related Solutions

This is 668 words and NEED to summarize to 475 words! This is 4,186 characters with...
This is 668 words and NEED to summarize to 475 words! This is 4,186 characters with spaces. I NEED it to be 3000 characters with spaces for nursing application. Please summarize in own words. This helps a lot and greatly appreciated! Education is a stepping stone for the inception of personal development which acts as a sign for promising career growth. At these unprecedented times, I extend my heartfelt gratitude to my brother and sister who worked as nurses expressing...
Create a function that keeps only strings with repeating identical characters (in other words, it has...
Create a function that keeps only strings with repeating identical characters (in other words, it has a set size of 1). Examples: identicalFilter(["aaaaaa", "bc", "d", "eeee", "xyz"]) ➞ ["aaaaaa", "d", "eeee"] identicalFilter(["88", "999", "22", "545", "133"]) ➞ ["88", "999", "22"] identicalFilter(["xxxxo", "oxo", "xox", "ooxxoo", "oxo"]) ➞ [] Notes A string with a single character is trivially counted as a string with repeating identical characters. If there are no strings with repeating identical characters, return an empty array. Code in C++...
The _____________ is the square root of the arithmetic average of the squared deviations from the mean. In other words, it is simply the square root of the ______________.
The _____________ is the square root of the arithmetic average of the squared deviations from the mean. In other words, it is simply the square root of the ______________.data spread; population standard deviationStandard deviation; data spreadpopulation standard deviation; variancevariance; standard deviation
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
In a posting of 250 words, describe your values statement when it comes to your professional...
In a posting of 250 words, describe your values statement when it comes to your professional working environment (Hospital). Think about your own personal values and how they relate to the work environment.
Write a C++ function to sort the characters in words in a inputed phrase. DO not...
Write a C++ function to sort the characters in words in a inputed phrase. DO not sort the characters in the string. Some of the steps has been done for you. Fill out the program in C++. See below for the expected console output. Make the program as simple as possible. Use introductory Object-Oriented C++ programming. If all the requirements are met, then this will be upvoted. Do use the C++ Standard Library headers, do not use the C Standard...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
2. [50] Write a C program to count the total number of commented characters and words...
2. [50] Write a C program to count the total number of commented characters and words in a C file taking both types of C file comments (single line and block) into account.
Write a C program that reads a file and reports how many lines, words, and characters...
Write a C program that reads a file and reports how many lines, words, and characters appear in it. For the purpose of this program, a word consists of a consecutive sequence of any characters except white space characters. For example, if the file lear.txt contains the following passage from King Lear, Poor naked wretches, wheresoe’er you are, That bide the pelting of this pitiless storm, How shall your houseless haeds and unfed sides, Your loop’d and window’d raggedness, defend...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT