Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The MorseTree (Enclosing) Class Method Members - public void MorseTree() { //Con

ID: 3576079 • Letter: T

Question

The MorseTree (Enclosing) Class

Method Members

- public void MorseTree() { //ConstructorUse this to load data from a file (“data.txt”) and populate your Binary Tree.

Each line in the file is a pair, as in “S …”, which is the letter followed by the morse code equivalent

Call the add() function below for each pair read from the file.

- private void insertInSubtree(String morseStr, Character letter, TreeNode subtree)

Note that the public add() function has been provided for you

Walk the tree while morseStr.length() > 0, removing the leading character from the morse string and…

Create a new TreeNode if your subtree is null.

Recursively move down the tree, going right if a “.” and left if a “-“.

- public void findInSubtree(String morseStr, TreeNode subtree)

Note that the outer (wrapper) function translate() has been provided for you.

Walk the tree while the morseStr.length() is greater than 0, removing the leading character from the morse string and…

Recursively move down the tree, going right if we encounter a “.” and left otherwise.

--------MorseTree.java--------

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MorseTree {
  
   TreeNode root;
   //TODO: Complete constructor
   public MorseTree() {
       //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<Character> insertInSubtree(String morseStr, char letter, 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 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 comlpete 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 recurse 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 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
   }


   private class TreeNode<TBA extends Comparable> {
  
   Object data; //to hold given node's data
   TreeNode left;
   TreeNode right;
  
   public TreeNode(Object d, TreeNode l, TreeNode r){
   this.data = d;
   this.left = l;
   this.right = r;
   }
   }
}

-----data.txt-----

A .-
B -...
C -.-.
D -..
E .
F ..-.
G --.
H ....
I ..
J .---
K -.-
L .-..
M --
N -.
O ---
P .--.
Q --.-
R .-.
S ...
T -
U ..-
V ...-
W .--
X -..-
Y -.--
Z --..
0 -----
1 .----
2 ..---
3 ...--
4 ....-
5 .....
6 -....

Explanation / Answer

public class MTree<E> {      

       Node<E> root;

      

       private class Node<E> {

              public Node<E> left;

              public Node<E> right;

              public E value;

             

              public Node(Node<E> left, Node<E> right, E value) {

                     this.left = left;

                     this.right = right;

                     this.value = value;

              }

       }

      

       public MTree() {

              root = new Node<E>(new Node<E>(null, null, null), new Node<E>(null, null, null), null);

       }

      

       public void add(String line) {

              Node current = root;

              String[] components = line.split(" ");

              for(String s : components)

                     s.trim();

             

              for(char c : components[1].toCharArray()) {

                     if(c == '.') {

                           if(current.left != null) {

                                  current = current.left;

                           } else {

                                  current.left = new Node(null, null, '~');

                                  current = current.left;

                           }

                     } else if(c == '-') {

                           if(current.right != null) {

                                  current = current.right;

                           } else {

                                  current.right = new Node(null, null, '~');

                                  current = current.right;

                           }

                     } else {

                           System.out.println("Ignoring: " + c);

                     }

              }

              current.value = components[0].charAt(0);

       }

      

       public E decode(String code) {

              char[] components = code.toCharArray();

              Node<E> current = root;

              for(char c : components) {

                     if(c == '.') {

                           if(current.left != null)

                                  current = current.left;

                           else

                                  System.err.println("Error traversing on char "" + c + """);

                     } else if(c == '-') {

                           if(current.right != null)

                                  current = current.right;

                           else

                                  System.err.println("Error traversing on char "" + c + """);

                     } else {

                           System.out.println("Ignore: " + c);

                     }

              }

              return (E)current.value;

       }

      

}

public class MTree<E> {      

       Node<E> root;

      

       private class Node<E> {

              public Node<E> left;

              public Node<E> right;

              public E value;

             

              public Node(Node<E> left, Node<E> right, E value) {

                     this.left = left;

                     this.right = right;

                     this.value = value;

              }

       }

      

       public MTree() {

              root = new Node<E>(new Node<E>(null, null, null), new Node<E>(null, null, null), null);

       }

      

       public void add(String line) {

              Node current = root;

              String[] components = line.split(" ");

              for(String s : components)

                     s.trim();

             

              for(char c : components[1].toCharArray()) {

                     if(c == '.') {

                           if(current.left != null) {

                                  current = current.left;

                           } else {

                                  current.left = new Node(null, null, '~');

                                  current = current.left;

                           }

                     } else if(c == '-') {

                           if(current.right != null) {

                                  current = current.right;

                           } else {

                                  current.right = new Node(null, null, '~');

                                  current = current.right;

                           }

                     } else {

                           System.out.println("Ignoring: " + c);

                     }

              }

              current.value = components[0].charAt(0);

       }

      

       public E decode(String code) {

              char[] components = code.toCharArray();

              Node<E> current = root;

              for(char c : components) {

                     if(c == '.') {

                           if(current.left != null)

                                  current = current.left;

                           else

                                  System.err.println("Error traversing on char "" + c + """);

                     } else if(c == '-') {

                           if(current.right != null)

                                  current = current.right;

                           else

                                  System.err.println("Error traversing on char "" + c + """);

                     } else {

                           System.out.println("Ignore: " + c);

                     }

              }

              return (E)current.value;

       }

      

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote