Hi I am trying the huffman tree decoding with my own created classes. Its just 5 letters for now. here is the code of the three classes:
MAIN:
import java.io.*;
public class HuffmanTree
{
public static void main (String[] args) throws IOException
{
Tree tree = new Tree();
String character = "e";
String code = "0";
tree.insert(character, code);
System.out.println("Saved " + character);
character = "a";
code = "10";
tree.insert(character, code);
System.out.println("Saved " + character);
character = "d";
code = "111";
tree.insert(character, code);
System.out.println("Saved " + character);
character = "b";
code = "1100";
tree.insert(character, code);
System.out.println("Saved " + character);
character = "c";
code = "1101";
tree.insert(character, code);
System.out.println("Saved " + character);
tree.inOrder(tree.root()); //PROBLEM HERE, WHICH IS RELATED TO INSERTING
System.out.print("Decode: ");
//code = getString();
//tree.search(code);
}
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
NODE:
import java.io.*;
import java.util.*;
import java.lang.Integer;
public class Node
{
private int key;
private String ch; // data item (key)
private Node leftChild; // the node's left child
private Node rightChild; // the node's right child
public Node(String letter)
{
ch = letter;
}
public void displayNode()
{
System.out.println('{'+ch+'}');
}
public Node getLeftChild()
{
return leftChild;
}
public Node getRightChild()
{
return rightChild;
}
public String getCh()
{
return ch;
}
public double getKey()
{
return key;
}
public void setLeftChild(Node lC)
{
leftChild = lC;
}
public void setRightChild(Node rC)
{
rightChild = rC;
}
public void setCh(String ch)
{
this.ch = ch;
}
}
TREE:
import java.io.*;
public class Tree
{
public Node root; // first node of tree
Node rootNode = new Node("root");
public Tree()
{
root = rootNode;
}
public Node root()
{
return root;
}
public void insert(String letter, String input)
{
Node newNode = new Node(letter);
Node current = root;
Node parent;
for(int i=0; i<input.length(); i++)
{
parent = current;
char ch = input.charAt(i);
int bit = Character.getNumericValue(ch);
//System.out.println("i = " + i + ", bit = " + bit + ", ch = " + ch + ", Parent: " + parent);
if(bit == 0)
{
current = current.getLeftChild();
if((i == input.length()-1) && (current == null))
{
parent.setLeftChild(newNode);
newNode.setCh(letter);
}
else
{
Node newerNode = new Node("empty");
parent.setLeftChild(newerNode);
current = newerNode;
}
}
else
{
current = current.getRightChild();
if((i == input.length()-1) && (current == null))
{
//System.out.println("Module3");
parent.setRightChild(newNode);
newNode.setCh(letter);
}
else
{
//System.out.println("Module4");
Node newerNode = new Node("empty");
parent.setRightChild(newerNode);
current = newerNode;
}
}
}
}
public void search(String input)
{
String test = root.getRightChild().getRightChild().getCh();
System.out.println("Character at right is '" + test + "'");
Node current = root;
Node parent;
for(int i=0; i<input.length(); i++)
{
char ch = input.charAt(i);
int bit = Character.getNumericValue(ch);
//System.out.println("i = " + i + ", bit = " + bit + ", ch = " + ch);
if(bit == 0)
{
current = current.getLeftChild();
System.out.println("Left current: " + current.getCh());
if(current.getCh() != "empty")
{
System.out.println("moduleLEFT");
System.out.print(current.getCh());
current = root;
System.out.println(current.getCh());
}
}
else
{
System.out.println(current + current.getCh());
current = current.getRightChild();
System.out.println(current.getCh());
if(current.getCh() == "d")
{
System.out.println("moduleRIGHT");
System.out.print(current.getCh());
current = root;
}
}
}
}
public void display()
{
String test = root.getRightChild().getRightChild().getCh();
System.out.println("Character at right is '" + test + "'");
}
public void inOrder(Node localRoot)
{
if(localRoot != null)
{
inOrder(localRoot.getLeftChild());
localRoot.displayNode();
inOrder(localRoot.getRightChild());
}
}
}
When i run it i get the output:
[13818160@lab219-02 trees]$ java HuffmanTree
Saved e
Saved a
Saved d
Saved b
Saved c
{e}
{root}
{empty}
{empty}
{c}
{empty}
Decode:
The problem is when inserting as you can see, after i call inOrder to display the tree inOrder (from {e} onwards) it has empty for some reason when it should really be displaying the other letters. I have tried everything but cant seem to find the problem anymore.
Could someone help???
Thanks
Phil