Evening Folks,
Having a bit of a problem with somthing i am trying to code, wondering if anyone here can help....
The basic idea is that i am trying to build a Priority Queue of binary tree nodes so that they are stored in ascending order by a number stored in each of the tree nodes.
Each binary tree node is constructed from a CharNode and are definately being constructed properly. The problem seems to be in the BinaryNode's compareTo method as the Priority Queue is using this to sort the elements.
BinaryNode.java
public class BinaryNode implements Comparable<BinaryNode>{
private BinaryNode left;
private BinaryNode right;
private int frequency;
private char character;
public BinaryNode(int f, char c){
this.left = null;
this.right = null;
this.frequency = f;
this.character = c;
}
public BinaryNode getLeft() {
return left;
}
public void setLeft(BinaryNode left) {
this.left = left;
}
public BinaryNode getRight() {
return right;
}
public void setRight(BinaryNode right) {
this.right = right;
}
public int getFrequency() {
return frequency;
}
public void setFrequency(int frequency) {
this.frequency = frequency;
}
public char getCharacter() {
return character;
}
public void setCharacter(char character) {
this.character = character;
}
public int compareTo(BinaryNode x) {
// Assume neither string is null. Real code should
// probably be more robust
if (x.getFrequency() > this.getFrequency())
{
return -1;
}
else if (x.getFrequency() < this.getFrequency())
{
return 1;
}
else if (this.equals(x))
return 0;
else {
System.out.println("ERROR");
return 0;
}
}
public String toString(){
//String s = "";
return this.character + "/" + this.frequency;
}
public boolean equals(BinaryNode x){
if (this.frequency == x.frequency && this.character == x.character){
return true;
}
return false;
}
}
The code that uses the PriorityQueue to
public void buildTree(){
PriorityQueue<BinaryNode> queue = new PriorityQueue<BinaryNode>();
for (CharNode element : this.frequencyList.getFreqList()){
BinaryNode node = new BinaryNode(element.getFrequency(), element.getCharacter());
System.out.println("Adding Node " + node + " to queue.");
queue.add(node);
System.out.println("Queue is now :" + queue);
}
System.out.println(queue);
}
Ive tried using compartors but i come up against the same problem, the above code gives the following output:
Adding Node t/4 to queue.
Queue is now :[t/4]
Adding Node h/3 to queue.
Queue is now :[h/3, t/4]
Adding Node e/2 to queue.
Queue is now :[e/2, t/4, h/3]
Adding Node /4 to queue.
ERROR
Queue is now :[e/2, t/4, h/3, /4]
Adding Node c/1 to queue.
Queue is now :[c/1, e/2, h/3, /4, t/4]
Adding Node a/2 to queue.
Queue is now :[c/1, e/2, a/2, /4, t/4, h/3]
Adding Node i/1 to queue.
ERROR
Queue is now :[c/1, e/2, i/1, /4, t/4, h/3, a/2]
Adding Node n/1 to queue.
ERROR
Queue is now :[c/1, n/1, i/1, e/2, t/4, h/3, a/2, /4]
[c/1, n/1, i/1, e/2, t/4, h/3, a/2, /4]
I realise this might be a complicatedly worded etc, but if anyones willing to help ill glady explain any code etc...
Cheers,
Logi.