Question

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 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.

package;

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 up to 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
package com.example;

import java.util.ArrayList;
import java.util.List;

public class Tree {

    private Node root;

    private class Node {
        Boolean isEnd;
        Node childNode[];
        public Node() {
            childNode = new Node[26];
            isEnd = Boolean.FALSE;
        }
    }

    /*

     * 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) {
        Node current = this.root;
        addChar(word, 0, current);

    }

    // insert char from word recursively
    private void addChar(String word, int index, Node current) {
        if (index >= word.length()) {
            current.isEnd = Boolean.TRUE;
            return;
        }
        int indexValue = convertFromLetterToIndexPosition(word.charAt(index));
        if (current.childNode[indexValue] == null) {
            current.childNode[indexValue] = new Node();
        }
        current = current.childNode[indexValue];
        addChar(word, index + 1, current);
    }

    /*

     * 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 checkChar(word, 0, this.root);

    }

    // check if word exist recursively
    private boolean checkChar(String word, int index, Node current) {
        if (current == null) {
            return false;
        }
        if (index >= word.length()) {
            return current.isEnd;
        }
        int indexValue = convertFromLetterToIndexPosition(word.charAt(index));
        return current.childNode[indexValue] == null ? Boolean.FALSE :
                checkChar(word, index + 1, current.childNode[indexValue]);

    }

    /*

     * 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 up to 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() {
        List<String> stringList = new ArrayList<>();
        getDictionary(this.root, "", stringList);
        String str = "";
        for (String s : stringList) {
            str += s + " ";
        }
        return str;
    }

    // Recursively get dictionary words

    private void getDictionary(Node current, String str, List<String> masterStr) {
        if (current.isEnd) {
            masterStr.add(str);
        }
        for (int i = 0; i < current.childNode.length; i++) {
            if (current.childNode[i] != null) {
                String newStr = str + convertFromIndexToLetter(i);
                getDictionary(current.childNode[i], newStr, masterStr);
            }
        }
    }

    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

    }

}



Add a comment
Know the answer?
Add Answer to:
26-ary tree for spell checker in JAVA You are asked to write some functionalities for a...
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
  • 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...

  • 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...

  • 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...

  • 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...

  • IN JAVA: Write a spell checker class that stores a set of words, W, in a...

    IN JAVA: Write a spell checker class that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a Spell Check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, since it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to...

  • Summary You will write an application to build a tree structure called Trie for a dictionary...

    Summary You will write an application to build a tree structure called Trie for a dictionary of English words, and use the Trie to generate completion lists for string searches. Trie Structure A Trie is a general tree, in that each node can have any number of children. It is used to store a dictionary (list) of words that can be searched on, in a manner that allows for efficient generation of completion lists. The word list is originally stored...

  • 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 {...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • ​​​​​ 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;”...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

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
ADVERTISEMENT