Today LZ in Do Huffman codec, need to do the module has one is, for the characters appear frequency to order from small to large, and then continue to build frequency sort sequence, My last option was to use LinkedList to emulate the Huffmannode queue, so a question was how to sort the objects by their attributes. 1): On the internet to see the discovery can let the Huffmannode class implementation comparable interface, note is the CompareTo (object that) method, the return is an integer, 0, negative. When the object is larger than that, returns an integer, which is smaller than that and returns a negative number; equals, 0.
public class Huffnode implements comparable{
Public Char lett;//Letter
public int freq;//Frequency
Public Huffnode left;//left node
Public Huffnode right;//Right node
Public Huffnode () {
Lett = ' + ';
freq = 0;
left = right =null;
}
Public huffnode (int freq) {
This.freq = freq;
}
/**
* and that comparison frequency size
* @param that
* @return
*/
public int CompareTo (Object) {
Huffnode other = (huffnode) that;
return freq-other.freq;
}
Public String toString () {
return lett+ "/" +FREQ;
}
}2) So how to use the sort? Combined with the problem, I need a queue, then I'll write an interface pqueue
Next write our Custom queue class:
Here I use is linkedlist to simulate, because LinkedList inherited the Queue<e> interface, directly with offer and poll method said so long to everyone see results as follows: 1 2 2 It is true that the analysis is right. The next blog LZ will say a simple Huffman codec how to do, thank you
Brief analysis of comparable interface and collection