Question

Complete the following code: You are asked to write some functionalities for a spelling checker i...

Complete the following code:
You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: 

/*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) 
/*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ 

public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number 26 indicates that each node in the tree has atmost 26 children (i.e. one each for the english alphabet). Each node in this tree has a child corresponding to a letter in the alphabet. Each node also indicates whether the word represented by the path between the root and the node is spelled correctly. For example, the tree shown in figure 1 depicts this indication as a filled-in node (in black color). This tree stores the words “boa,” “boar,” “boat,” “board,” “hi,” “hip,” “hit,” “hop,” “hot,” “trek,” and “tram.” To check whether a given word is spelled correctly, you begin at the tree’s root and follow the reference associated with the first letter in the word. If the reference is null, the word is not in the tree. Otherwise, you follow the reference associated with the second letter in the word, and so on. If you finally arrive at a node, you check whether it indicates a correctly spelled word. For example, the tree in figure 1 indicates that “t,” “tr,” and “tre” are spelling mistakes, but “trek” is spelled correctly 

public class Tree {
        private Node root;
        
        private class Node {
                /*
                 * TODO: Complete the class Node. 
                 * You can add any number of instance variables or methods
                 * or constructor to the Node class. 
                 */
        }
        
        
        /*
         * We have completed the constructor of the Tree class. 
         */
        public Tree()
        {
                root=new Node();
                
        }
        
        /*
         * TODO: add the word into the tree based on the structure
         * provided in the handout. You must use recursion. Feel free
         * to create a helper private method that will do the recursion 
         * for you.  
         */
        public void add(String word)
        {
                
                    
        }

        
        /*
         * TODO: Checks whether the word is present in the tree or not. 
         * You must use recursion. Feel free
         * to create a helper private method that will do the recursion 
         * for you.  
         */
        
        public boolean check(String word)
        {
                return false;
        }

        
        /*
         * We have already completed this method for you. 
         * This method works with the preconditions that all letter
         * are lower case and between and inclusive a-z
         * For example, if the letter is a, then the output is 0
         * For example, if the letter is b, then the output is 1
         * For example, if the letter is c, then the output is 2
         * .
         * .
         * .
         * For example, if the letter is z, then the output is 25
         */
        private static int convertFromLetterToIndexPosition(char letter)
        {
                int temp = (int)letter;
                int temp_integer = 96; //for lower case
                return (temp-temp_integer-1);
                
        }
        
        
        /*
         * We have already completed this method for you. 
         * This method works with the preconditions that all integers
         * are between 0 and 25. 
         * For example, if the index is 0, then the output is a
         * For example, if the letter is 1, then the output is b
         * For example, if the letter is 2, then the output is c
         * .
         * .
         * .
         * For example, if the letter is 25, then the output is z
         */
        private static String convertFromIndexToLetter(int index)
        {
                return String.valueOf((char)(index + 97));
        }
        
        /*
         * TODO: You must complete this method. You can use recursion or no recursion. 
         * It is completely upto you. This method however, must return back a string of 
         * words in alphabetical order that represents the dictionary that the tree is 
         * currently holding. See the main method for an expected output of this. 
         */
        public String getDictionaryInAlphabeticalOrder()
        {
                
                
        }
        
        
        public static void main(String[] args)
        {
                Tree dictionary=new Tree();
                dictionary.add("abbas");
                dictionary.add("ab");
                dictionary.add("a");
                dictionary.add("xan");
                dictionary.add("ban");
                dictionary.add("acbbas");
                dictionary.add("cat");
                dictionary.add("dog");
                
                System.out.println(dictionary.check("abbas")); //true
                System.out.println(dictionary.check("ab"));  //true
                System.out.println(dictionary.check("abb")); //false
                System.out.println(dictionary.check("a")); //true
                System.out.println(dictionary.check("xab")); //false
                System.out.println(dictionary.check("xan")); //true
                
                System.out.println(dictionary.getDictionaryInAlphabeticalOrder()); //a ab abbas acbbas ban cat dog xan 
                
        }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

public class Tree {

private Node root;

  

private class Node {

/*

   * TODO: Complete the class Node.

   * You can add any number of instance variables or methods

   * or constructor to the Node class.

   *

   */

String w;

Node(){

}

Node(String word){

w = word;

left = null;

right = null;

}

  

Node left;

Node right;

}

  

  

/*

   * We have completed the constructor of the Tree class.

   */

public Tree()

{

root=null;

  

}

  

/*

   * TODO: add the word into the tree based on the structure

   * provided in the handout. You must use recursion. Feel free

   * to create a helper private method that will do the recursion

   * for you.

   */

  

public Node addUtil(Node root, Node n) {

if (root == null)

return n;

if (n.w.compareTo(root.w) == 0)

return root;

if (n.w.compareTo(root.w) <= 0)

root.left = addUtil(root.left, n);

else

root.right = addUtil(root.right, n);

return root;

}

public void add(String word)

{

  

Node n = new Node(word);

  

root = addUtil(root,n);

  

  

}

  

/*

   * TODO: Checks whether the word is present in the tree or not.

   * You must use recursion. Feel free

   * to create a helper private method that will do the recursion

   * for you.

   */

  

public boolean CheckUtil(Node root, String word) {

if (root == null)

return false;

else if (word.compareTo(root.w) == 0)

return true;

else if (word.compareTo(root.w) < 0)

return CheckUtil(root.left, word);

else

return CheckUtil(root.right, word);

}

  

public boolean check(String word)

{

  

return CheckUtil(root, word);

}

  

/*

   * We have already completed this method for you.

   * This method works with the preconditions that all letter

   * are lower case and between and inclusive a-z

   * For example, if the letter is a, then the output is 0

   * For example, if the letter is b, then the output is 1

   * For example, if the letter is c, then the output is 2

   * .

   * .

   * .

   * For example, if the letter is z, then the output is 25

   */

private static int convertFromLetterToIndexPosition(char letter)

{

int temp = (int)letter;

int temp_integer = 96; //for lower case

return (temp-temp_integer-1);

  

}

  

  

/*

   * We have already completed this method for you.

   * This method works with the preconditions that all integers

   * are between 0 and 25.

   * For example, if the index is 0, then the output is a

   * For example, if the letter is 1, then the output is b

   * For example, if the letter is 2, then the output is c

   * .

   * .

   * .

   * For example, if the letter is 25, then the output is z

   */

private static String convertFromIndexToLetter(int index)

{

return String.valueOf((char)(index + 97));

}

  

/*

   * TODO: You must complete this method. You can use recursion or no recursion.

   * It is completely upto you. This method however, must return back a string of

   * words in alphabetical order that represents the dictionary that the tree is

   * currently holding. See the main method for an expected output of this.

   */

  

  

public void getDictionaryInAlphabeticalOrderUtil(Node root, ArrayList<String> x)

{

  

if (root != null) {

getDictionaryInAlphabeticalOrderUtil(root.left, x);

x.add(root.w);

getDictionaryInAlphabeticalOrderUtil(root.right,x);

}

}

public String getDictionaryInAlphabeticalOrder()

{

  

String ret = " ";

  

ArrayList<String> x = new ArrayList<String>();

getDictionaryInAlphabeticalOrderUtil(root,x);

for(int i=0 ;i<x.size() ; ++i) {

ret = ret + x.get(i) + " ";

}

  

return ret;

  

}

  

  

public static void main(String[] args)

{

Tree dictionary=new Tree();

dictionary.add("abbas");

dictionary.add("ab");

dictionary.add("a");

dictionary.add("xan");

dictionary.add("ban");

dictionary.add("acbbas");

dictionary.add("cat");

dictionary.add("dog");

  

System.out.println(dictionary.check("abbas")); //true

System.out.println(dictionary.check("ab")); //true

System.out.println(dictionary.check("abb")); //false

System.out.println(dictionary.check("a")); //true

System.out.println(dictionary.check("xab")); //false

System.out.println(dictionary.check("xan")); //true

  

System.out.println(dictionary.getDictionaryInAlphabeticalOrder()); //a ab abbas acbbas ban cat dog xan

  

}

}

==========================
SEE OUTPUT
150 51 public String getDictionaryInAlphabetical0rderO 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 public

Thanks, PLEASE UPVOTE

Add a comment
Know the answer?
Add Answer to:
Complete the following code: You are asked to write some functionalities for a spelling checker i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 26-ary tree for spell checker in JAVA You are asked to write some functionalities for a...

    26-ary tree for spell checker in JAVA You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number...

  • For this assignment, you will write a program to work with Huffman encoding. Huffman code is...

    For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • Instructions Create a class BettterTree that extends OurTree, to facilitate the following. Update: if extending the...

    Instructions Create a class BettterTree that extends OurTree, to facilitate the following. Update: if extending the class is too much, you can modify class OurTree. In our discussions about OurTree, we focused on nodes and their left and right children. For each node in the tree, is there another node of interest in addition to its children? If yes, how would you extend OurTree for that? How will the behavior of addNode method change? OurTree Code: public class OurTree {...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled...

    ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values...

    package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...

  • 1. Write a function in Tree class which returns true if and only if the tree...

    1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...

  • Need help with these two questions String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal....

    Need help with these two questions String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "bcaahttaetersse" 4. String outputDepthFirstSearch(): returns a string represenng a pre order depth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "batcathateersse This is my code so far public class Trie { final TrieNode root; public Trie() { this.root...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT