MorseCode--Need help
hey guys,
i'm trying to write a program that basically encode and decode the morse code from a text file using the Binary tree. I've implemented both the LinkedBinaryTree and BinaryTreeNode classes.Code for BinaryTreeNode
import java.util.Iterator;
publicclass BinaryTreeNode<T>{
private T element;
private BinaryTreeNode<T> left, right, root;
// Creates a new empty tree node.
BinaryTreeNode (){
element =null;
left =null;
right =null;
}// constructor BinaryTreeNode
// Creates a new tree node with the specified data.
BinaryTreeNode (T obj){
element = obj;
left =null;
right =null;
}// constructor BinaryTreeNode
publicvoid setLeft (BinaryTreeNode<T> nodeL){ left = nodeL;}
publicvoid setRight (BinaryTreeNode<T> nodeR){ right = nodeR;}
publicvoid setElement (T newE){ element = newE;}
public T getElement (){return element;}
public BinaryTreeNode getLeft(){return left;}
public BinaryTreeNode getRight(){return right;}
}
code for LinkedBinaryTree, note i'm only giving the code for the instructors bec i hink they hold important info, rest of the methods in that class are the 4 traversal and their iterators
import java.util.Iterator;
publicclass LinkedBinaryTree<T>implements BinaryTreeADT<T>{
protectedint count;
protected BinaryTreeNode<T> root;
// Creates an empty binary tree.
public LinkedBinaryTree(){
count = 0;
root =null;
}// constructor LinkedBinaryTree
// Creates a binary tree with the specified element as its root.
public LinkedBinaryTree (T element){
count = 1;
root =new BinaryTreeNode<T> (element);
}// constructor LinkedBinaryTree
// Constructs a binary tree from the two specified binary trees.
public LinkedBinaryTree (T element, LinkedBinaryTree<T> leftSubtree,
LinkedBinaryTree<T> rightSubtree){
root =new BinaryTreeNode<T> (element);
count = 1;
if (leftSubtree !=null){
count = count + leftSubtree.size();
root.setLeft(leftSubtree.root);
}//if
else
root.setLeft(null);
if (rightSubtree !=null){
count = count + rightSubtree.size();
root.setRight(rightSubtree.root);
}//if
else
root.setRight(null);
}// constructor LinkedBinaryTree
}
In my assignmnet instructions it says for the MorseTree class extend the LinkedBinaryTree class but as far as i know when i'm going to insert letter in either left or right node i'll have to call BinaryTreeNode class. Although, i've the ideas how to insertLetter in the node, etc etc. i'm still quite confused how to get started. Please someone help me!Code for MorseTreeClass
publicclass MorseCodeTreeextends LinkedBinaryTree{
final String DOT =".", DASH ="-";
String fileName;
/**
* create new MorseCode tree
*/
public MorseCodeTree (String fN){
fileName = fN;
}
/**
* This method adds a new letter into the morse tree
*/
publicvoid insertLetter (String let, String code){
/**
*Puesdo code for this class
create new node for letter:
start at root
while there are more code characters (dots, dashes)
{
if next code character is a dot
{
if can go left (i.e. left reference is not null), go left
else insert node at left
}
else (it's a dash)
{
if can go right, go right
else insert node at right
}
}
*/
// not too sure whether to use BinaryTreeNode or LinkedBinaryTree
BinaryTreeNode current =new BinaryTreeNode();
BinaryTreeNode temp =new BinaryTreeNode();
}
// returns the current node
public BinaryTreeNode getRoot (){
return this.root;
}
}
It's very urgent please someone help me! Thanks in advance =]

