Question
Please help me code the following in JAVA Please read the task thoroughly, and include many COMMENTS so I can understand! Full points will be
Please help me code the following in JAVA
Please read the task thoroughly, and include many COMMENTS so I can understand!
Full points will be awarded, thanks in advance! MorseTree class:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;;
/*
* This class reads in data from a text file ("data.txt") and populates a binary tree with an
* ordering constraint. See the lab instructions for more information, but in general, dots go right
* and dashes go left when constructing or traversing a Morse code tree. Search for //TODO
* in the code to see what code you have to implement.
*
* Start with the constructor. In your constructor read each line in from the textfile first,
* calling add() for each {letter, morseCodeStr} pair.
*
*/
public class MorseTree {
//TODO: data member called "root" goes here
//TODO: Complete constructor
public MorseTree() {
//first, open data.txt, add each line to the tree
Scanner fin;
try {
//for each line in the file,
// get the letter(char) and the Morse string
// call add() with this data
// print out the letter and Morse string here for debugging
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(String morseStr, char letter) {
root = insertInSubtree(morseStr, letter, root);
}
//TODO: recursively complete this function. It's only a few characters different from findInSubtree()
private TreeNode
//base case 1 : subtree is null
//base case 2 : morseStr is of length 0
//recursive case 1: the first char in morseStr is a '.', so recursively traverse tree
//recursive case 2: the first char in the morseStr is a '-', so recurse accordingly
return subtree; //always the last line, always return the node you are working on
}
public Character translate(String morseStr) {
return findInSubtree(morseStr, root);
}
//TODO: recursively complete this function. Very similar to insertInSubtree()
private Character findInSubtree(String morseStr, TreeNode subtree) {
//base case 1 : subtree is null
//base case 2 : morseStr is of length 0
//recursive case 1: the first char in morseStr is a '.', so recursively traverse tree
//recursive case 2: the first char in the morseStr is a '-', so re-curse accordingly
return null; //remove this
}
//TODO: Non-recursive function that calls other (recursive) functions
public String translateString(String tokens) {
String retVal = "";
//build a scanner here using tokens as input
//iterate over the tokens calling translate on each token (substring separated by a space)
//concat these characters and return them
return retVal;
}
public String toMorseCode(Character c) { .
//walk the tree looking for the TreeNode with the char c in it
//preorder walk?
//inorder walk?
//postorder walk?
//when you've found the char c, report the path from the root to the node
//and build the morse code by adding a "." when you go right, "-" when you go left
return new String("You wish.");
}
public String toString() {
return inorderWalk();
}
private String inorderWalk() {
return new String("Another wish.");
}
public static void main(String[] args) {
MorseTree mt = new MorseTree(); //builds our tree using data from a file
//System.out.println(mt.translate("...")); //prints out S
//System.out.println(mt.translate("---")); //prints out O
//System.out.println(mt.translate(".......-")); //prints out null
//System.out.println(mt.translateString("... --- ...")); //SOS
//System.out.println(mt.toMorseCode('S')); //find where we are in the tree, remember path to root
}
// Inner class to create the linked structure
private class TreeNode
Object data; // holds a given nodes data
TreeNode right;
TreeNode left;
public TreeNode() {
this.data = null;
this.right = null;
this.left = null;
}
public void setRight(TreeNode rightNode) {
this.right = rightNode;
}
public void setLeft(TreeNode leftNode) {
this.left = leftNode;
}
}
}
2.1 TreeNode Inner Class In this section, we'll build an internal class using generics that can store one data item and two TreeNodereferences (one for the left child, one for the right). Start by uncommenting the private inner class called TreeNode, at the end of Morse Tree.java Data & Method Members Declare a "Object data;" item to hold a given node's data Declare two TreeNode references called left and right Declare one constructor that takes three parameters and populates the data members for this structure. . 2.2 The MorseTree (Enclosing) Class In this section, we'll build our MorseTree class by declaring only one private data item (a TreeNode) and multiple public (and private) methods. A common pattern here will be as follows: Clients of our code will call some public method (say, add(int data)) and our public method will redirect to a private method (insertlntoSubtree(data, root)). We'll follow this pattern in the methods we implement below. See the Tree code in your Savitch text for additional code samples and guidance. Data Members Declare a private TreeNode called root (which is similar to head in linked lists) 2.1 TreeNode Inner Class In this section, we'll build an internal class using generics that can store one data item and two TreeNodereferences (one for the left child, one for the right). Start by uncommenting the private inner class called TreeNode, at the end of Morse Tree.java Data & Method Members Declare a "Object data;" item to hold a given node's data Declare two TreeNode references called left and right Declare one constructor that takes three parameters and populates the data members for this structure. . 2.2 The MorseTree (Enclosing) Class In this section, we'll build our MorseTree class by declaring only one private data item (a TreeNode) and multiple public (and private) methods. A common pattern here will be as follows: Clients of our code will call some public method (say, add(int data)) and our public method will redirect to a private method (insertlntoSubtree(data, root)). We'll follow this pattern in the methods we implement below. See the Tree code in your Savitch text for additional code samples and guidance. Data Members Declare a private TreeNode called root (which is similar to head in linked lists)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started