}
static private String [] huffmancode (long f[]) {
ArrayList htree = new ArrayList ();
for (int i=0; i<256; i++) htree.add(new Node(Integer.toString(i) ,f[i]));
Node s1,s2;
while(htree.size()>1){
s1=removeSmallest(htree);
s2=removeSmallest(htree);
Node r = new Node (s1.getValue().concat(s2.getValue()), s1.getFreq()+s2.getFreq());
r.setLeft(s1);
r.setRight(s2);
s1.setTop(r);
s2.setTop(r);
htree.add(r);
}
Node root=removeSmallest(htree);
String hcode []=new String [256];
tranverse (root, "", hcode);
for (int i=0; i<256; i++) System.out.println(hcode[i]);
return(hcode);
}
static private Node removeSmallest (ArrayList a) {
int s=a.size();
long minval=Long.MAX_VALUE;
int minpos=0;
long t; Node n;
for (int i=0;i<s; i++) {
n=(Node) a.get(i);
t=n.getFreq();
if(t<minval) {
No comments:
Post a Comment