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 =]

[7491 byte] By [salubadshaa] at [2007-10-2 16:54:47]
# 1

1. Extend your MorseCodeTree class as follows

public class MorseCodeTree extends LinkedBinaryTree<String>

After that, you can call all the methods in LinkedBinaryTree passing

it String objects i.e DOT or DASH.

Ajay

ordinary_guya at 2007-7-13 18:07:08 > top of Java-index,Java Essentials,Java Programming...
# 2

> 1. Extend your MorseCodeTree class as follows

> public class MorseCodeTree extends

> LinkedBinaryTree<String>

>

> After that, you can call all the methods in

> LinkedBinaryTree passing

> it String objects i.e DOT or DASH.

I know if i extend a class i can call all it's methods but if you read the pseudo- code given in the class MorseTree for method insertLetter () it simply giving the expression that i've to use the BinaryTreeNode class because it contains all the methods, .setLeft or. setRight() etc. On the other hand, the LinkedBinaryTree class only conatins traversal tree and their Itertaors method. Since i'm extending LinkedBinaryTree Class, so it's obvious that i've incorporate its methods in order to build the tree with morse code. At the instant, i've no idea how to incorproate. Can someone please help me. Also i've tried to follow the pusedo code provided in insertLetter method and here's my code, can someone please tell me if it's correct, thanks in advance:

/**

* This method adds a new letter into the morse tree

*/

public void 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();

//LinkedBinaryTree<BinaryTreeNode> current = new LinkedBinaryTree<BinaryTreeNode>();

current = getRoot();

for (int i = 0; i < code.length(); i++) {

String checkCode = code.substring(i, i+1);

if (checkCode.equals(DOT)) {

if (current.getLeft() != null)

current = current.getLeft();

else {

current.setLeft(current);

this.count++;

}

} else {

if (current.getRight() != null)

current = current.getRight();

else {

current.setRight(current);

this.count++;

}

}

}

current.setElement(let);

}

Please, please someone help me. I'm stuck! Thanks a tons.

salubadshaa at 2007-7-13 18:07:08 > top of Java-index,Java Essentials,Java Programming...
# 3
.--....-.--.......--.....--..--.---..-.-..--..
prometheuzza at 2007-7-13 18:07:08 > top of Java-index,Java Essentials,Java Programming...
# 4

Try something like this (untested)

I am not sure if this is exactly what you want, but perhaps it can

help you go ahead with the problem you are stuck..

public void 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

}

}

*/

BinaryTreeNode<String> current = getRoot();

for (int i = 0; i < code.length(); i++) {

String checkCode = code.substring(i, i+1);

if (checkCode.equals(DOT)) {

if (current.getLeft() != null)

current = current.getLeft();

else {

BinaryTreeNode<String> newNode= new BinaryTreeNode<String>();

current.setLeft(newNode);

current = newNode;

this.count++;

}

} else {

if (current.getRight() != null)

current = current.getRight();

else {

BinaryTreeNode<String> newNode= new BinaryTreeNode<String>();

current.setRight(newNode);

current = newNode;

this.count++;

}

}

}

current.setElement(let);

}

ordinary_guya at 2007-7-13 18:07:08 > top of Java-index,Java Essentials,Java Programming...
# 5
.--....-.-- ..... ..- -.....-- ..--.---..-.- ..--.. Ukgent?
floundera at 2007-7-13 18:07:08 > top of Java-index,Java Essentials,Java Programming...
# 6

@ordinary_guy thanks for fixing that up but my originally problem is still the same. I don't know how to incoporate LinkedBinaryTreeClass instead of BinaryTreeNode bec LinkedBinaryTree class pretty much holds all the stuff of BinaryTreeNode class but it doesn't contain methods .setLeft . setRight that are required for this (insertLetter) method. I already provided the code for the LinkedBinaryTree class constructors. I'm pretty sure i've to use LinkedBinayTree<String> current = getRoot();

instead of

BinaryTreeNode<String> current = getRoot();

I'm pretty sure i've to incorporate LinkedBinaryTree object to code insertLetter() method instead of using BinaryTreeNode object because i'm extending Link BinaryTree class. There's no point to extend that class if i'm not going to use it? Can please someone tell me if it's even possible to do that? Please someone guide me

salubadshaa at 2007-7-13 18:07:08 > top of Java-index,Java Essentials,Java Programming...
# 7
> .--....-.--> > .....> > ..-> > -.....--> > ..--.---..-.-> > ..--.. > > Ukgent?did you even read my problem? :S i know what is morse code, just learned :p
salubadshaa at 2007-7-13 18:07:08 > top of Java-index,Java Essentials,Java Programming...
# 8

> .--....-.--

>

> > .....

> >

> > ..-

> >

> > -.....--

> >

> > ..--.---..-.-

> >

> > ..--..

>

> Ukgent?

Ah well, at least I'm as consistent in my morse as I am in my English!

; )

prometheuzza at 2007-7-13 18:07:08 > top of Java-index,Java Essentials,Java Programming...