Menu

[Solved]Given Bstnode Class Would Create Class Called Morsecharacterjava Implement Following Metho Q37109744

Given the BSTNode class, how would you create a class calledMorseCharacter.java and implement the following methods?

P- 0 Figure 1 Sample Morse Code (note that codefile.dat has each letter on a separate line.) Build a binary search tree to deMorseCharacter class: compareTo is implemented correctly hashCodel) method is implemented correctly equals(Object) method is

public class BSTNode<K extends Comparable<K>,V>extends BinaryNode<K,V>{

   BSTNode(K aKey, V aValue) {
       super(aKey, aValue);
   }
  
   BSTNode(K aKey, V aValue, BinaryNode<K,V> aLeft,BinaryNode<K,V> aRight){
      super(aKey,aValue,aLeft,aRight);
   }

   public void add(K aKey, V aValue) {
       add(aKey,aValue,this);
   }
  
   private void add(K aKey, V aValue,BinaryNode<K,V> root) {      
       if(aKey.compareTo(root.key) < 0){
           if(root.left !=null) { //there is no left child
              add(aKey, aValue, root.left); //give the root aleft child containing aKey and aValue
       }
           else {
              root.left = new BinaryNode<K, V>(aKey,aValue); //if BST has a left child add left subtree
              }
       }
       else if(aKey.compareTo(root.key)> 0) {
              if(root.right != null) { //there is no rightchild
                  add(aKey, aValue,root.right); //give right root a key and value
       }
           else {
              root.right = new BinaryNode<K,V>(aKey,aValue); //BST has a right child add right subtree
              }
       }
       else {
           throw newIllegalStateException(“All keys must be unique”); //key matches thekey in the root of BST
       }
   }

   public V search(K aKey) {
       return search(aKey,this);
   }
  
   private V search(K aKey,BinaryNode<K,V> root){
      
   if(root == null) {
       return null;
   }
   if(aKey.compareTo(root.key) < 0) {
       return search(aKey,root.left);
   }
   else if(aKey.compareTo(root.key) > 0) {
       return search(aKey,root.right);
   }
   else {
       return root.value;
   }
}
  
   public V remove(K aKey) {
       returnremove(aKey,this,null,true);
   }
  
   private V remove(K aKey, BinaryNode<K,V> root,BinaryNode<K,V> parent, boolean left) {
       //Use recursion to find the nodebeing removed. If the node is not in the tree return null.
       //Recursive calls keep track of thecurrent node, its parent,
           //and if thecurrent node is the left or right child of the parent.
       if(root == null) {
           returnnull;
       }
       if(aKey.compareTo(root.key) < 0){
           if(root.left !=null) {
           remove(aKey,root.left, root, true);
       }
           else {
           returnnull;
           }
       }
       else if(aKey.compareTo(root.key)> 0) {
           if(root.right !=null) {
           remove(aKey,root.right, root, false);
           }
           else {
           returnnull;
       }
   }
       //This condition is only reachedwhen the node to be removed is found.
       else{
           //newChild isthe node replacing the removed node.
           //More formally,it is the new child of the removed nodes parent.
              //Which child (left or right) is determined bythe left parameter.
           BinaryNode<K,V> newChild = null;
           //Here weimplement our removal algorithms
           //In thefollowing code the “root” is the node being removed
           //If the roothas no children, the newChild is null
           if(root.left ==null && root.right == null) {
              newChild = null;
           }
           //If the roothas both a left and right child…
           elseif(root.left != null && root.right != null) {
              //We find the smallest node in the removed nodesright subtree
              BinaryNode <K,V> smallest =root.right;
              BinaryNode <K,V> smallestParent =root;
             

              while(smallest.left != null) {
                  smallestParent =smallest;
                  smallest =smallest.left;
           }
              //If the root of the removed nodes right subtreeis the smallest value in the subtree
                  //Set the removed node’sright child to its right grand-child.
              if (root.right.left == null) {
                  root.right =smallest.right;
           }
              //Else, find the smallest value in the removednodes right subtree
                  //When the smallest node isfound, take its right subtree and make it it’s parents leftchild.
                  //If the smallest node doesnot have a right subtree, then we make its parents left childnull.
                  //This is done to remove thesmallest node from the tree (so it doesn’t show up twice)

              else {
                  if(smallest.right != null){
                     smallestParent.left = smallest.right;
                  }
                  else {
                     smallestParent.left = null;
                  }
           }

              //Make the newChild the smallest node in theremoved nodes right subtree
             
              newChild = smallest;
             
              //Make the newChild’s children the children ofthe removed node
             
              newChild.left = root.left;
              newChild.right = root.right;
                 
           }
           //If the roothas a left child, make the new child the root’s left child
           elseif(root.left != null) {
              newChild = root.left;
           }
           //If the roothas a right child, make the new child the roots’ right child
           elseif(root.right != null) {
              newChild = root.right;
           }
          
           //Edge case forhandling the removal of the root
           if(parent ==null) {
              if(newChild == null) {
                  throw newIllegalStateException(“Can’t remove root from tree of heightzero”);
              }
              //TODO Make this warning go away!
              V toReturn = root.value;
              this.key = newChild.key;
              this.value = newChild.value;
              this.left = newChild.left;
              this.right = newChild.right;
              return toReturn;
           }
          
           //Replace theroot (node being removed) with the new child.
           //We have both areference to the removed node’s parent and a boolean indicating ifthe removed node is a
              //left or right child
           if(left){parent.left = newChild;}
           else{parent.right = newChild;}
           returnroot.value;
       }
  
           returnroot.value;
   }

}

P- 0 Figure 1 Sample Morse Code (note that codefile.dat has each letter on a separate line.) Build a binary search tree to decode Morse code messages in a tree structure using the BSTNode class. Morse code characters are stored in the nodes of the tree in Morse code order, considering “dot” to be less than “dash”. Then use this tree in the implementation of a method to decode a message in Morse code. To decode a character, start at the root and move left for each dot, right for each dash, until the character is found. Repeat this process for each character in the string to build the output result for the method File input of the Morse code conversions needed to build the tree is in codefile.dat. Each line of this file contains a character, followed by a space, followed by its Morse code representation. As you read in the file, create a MorseCharacter object to store the Morse code character. This class should store each Morse character as a String and contain constructors, getters and setters, toString, equals, hashCode, and implement the Comparable<E> interface (use the Morse code strings for comparisons). MorseCharacter class: compareTo is implemented correctly hashCodel) method is implemented correctly equals(Object) method is implemented correctly Rest of object is implemented correctly Show transcribed image text P- 0 Figure 1 Sample Morse Code (note that codefile.dat has each letter on a separate line.) Build a binary search tree to decode Morse code messages in a tree structure using the BSTNode class. Morse code characters are stored in the nodes of the tree in Morse code order, considering “dot” to be less than “dash”. Then use this tree in the implementation of a method to decode a message in Morse code. To decode a character, start at the root and move left for each dot, right for each dash, until the character is found. Repeat this process for each character in the string to build the output result for the method File input of the Morse code conversions needed to build the tree is in codefile.dat. Each line of this file contains a character, followed by a space, followed by its Morse code representation. As you read in the file, create a MorseCharacter object to store the Morse code character. This class should store each Morse character as a String and contain constructors, getters and setters, toString, equals, hashCode, and implement the Comparable interface (use the Morse code strings for comparisons).
MorseCharacter class: compareTo is implemented correctly hashCodel) method is implemented correctly equals(Object) method is implemented correctly Rest of object is implemented correctly

Expert Answer


Answer to Given the BSTNode class, how would you create a class called MorseCharacter.java and implement the following methods? pu… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *