00001 package HuffMan;
00002
00003
00004
00005
00006
00007
00008
00009 import java.util.Vector;
00010
00034 public class Huffman{
00035
00036
00037
00038 public float _count[] ;
00039 int _dad[] ;
00040 int _code[] ;
00041 int _len[] ;
00042 int _root;
00043
00044
00045 final int SIZE ;
00046 static final int BITS_PER_CHAR = 8;
00047
00048
00053 public Huffman(float[] _count){
00054 SIZE=_count.length;
00055 this._count = new float[2 * SIZE];
00056 float totaal=0;
00057 for(int i =0;i!=_count.length;i++){
00058 this._count[i]=_count[i];
00059 totaal+=_count[i];
00060 }
00061 if(totaal < 1-0.01 || totaal>1+0.01){
00062 Debug.print("arraytotaal"+Algorithms.ArrayHandler.totaal(_count));
00063 Debug.print("totaal:= "+totaal);
00064 Debug.print("Error Huffman -> totaal van _count (PDF) is !=1");
00065 System.exit(-1);
00066 }
00067 _dad = new int[2 * SIZE];
00068 _code = new int[SIZE];
00069 _len = new int[SIZE];
00070 }
00071
00072
00073
00074
00075
00082 public String charCode(int index) {
00083 StringBuffer outString = new StringBuffer();
00084 int i;
00085
00086 try{
00087 for(i = _len[index]; i > 0; i--)
00088 outString.append((_code[index] >> (i - 1)) & 1);
00089 }
00090 catch(java.lang.ArrayIndexOutOfBoundsException oobe){
00091 Debug.print("index:= "+index +"aantal codewoorden:= " + _count.length + " _code.length "+ _code.length+
00092 " _len.length "+_len.length);
00093 Debug.print("ERROR Huffman.charCode(): indexOutOfBoundsException");
00094 System.exit(-1);
00095 }
00096
00097 return outString.toString();
00098 }
00099
00100
00105 public void compress() {
00106
00107 PriorityQueue pq = new PriorityQueue();
00108 Vector path;
00109 int x, j, k, t;
00110 PairElement t1, t2;
00111
00112
00113
00114
00115
00116
00117
00118 for(int i = 0; i < _count.length; i++){
00119 if(_count[i] > 0) {
00120 pq.insert(new PairElement(i, _count[i]));
00121 }
00122 }
00123
00124
00125
00126
00127
00128 int teller=SIZE;
00129 while(pq.size() > 1) {
00130 t1 = pq.removeMin();
00131 t2 = pq.removeMin();
00132 _dad[teller] = 0;
00133 _dad[t1.key] = teller;
00134 _dad[t2.key] = -teller;
00135 _count[teller] = _count[t1.key] + _count[t2.key];
00136 if(!pq.isEmpty())
00137 pq.insert(new PairElement(teller,_count[teller]));
00138 teller++;
00139 }
00140
00141
00142
00143 if(pq.size() == 1) {
00144 t1 = pq.removeMin();
00145 _dad[teller] = 0;
00146 _dad[t1.key] = teller;
00147 _count[teller] = _count[t1.key];
00148 teller++;
00149 }
00150
00151
00152 _root = teller - 1;
00153
00154
00155
00156
00157
00158
00159
00160
00161 for(k = 0; k < SIZE; k++) {
00162 path = new Vector();
00163 int i = 0;
00164 x = 0;
00165 j = 1;
00166
00167 if(_count[k] != 0) {
00168
00169 t = _dad[k];
00170
00171 while(t != 0) {
00172 if(t < 0) {
00173 x += j;
00174 t = -t;
00175 }
00176 path.addElement(new Integer(t));
00177
00178 t = _dad[t];
00179 j += j;
00180 i++;
00181 }
00182 }
00183
00184 _code[k] = x;
00185 _len[k] = i;
00186 }
00187
00188
00189
00190
00191
00192
00193 }
00194
00195 public static void main(String args[]){
00196 float[] _count=new float[50];
00197 float totaal=0;
00198 for(int i=0;i!=_count.length;i++){
00199 _count[i]=(float)Math.random();
00200 totaal+=_count[i];
00201 }
00202
00203 for(int i =0;i!=_count.length;i++){
00204 _count[i]=_count[i]/totaal;
00205 }
00206
00207 Huffman h =new Huffman(_count);
00208 h.compress();
00209 String code;
00210 float COMPR=0;
00211 float ENTROPIE=0;
00212 for (int i =0;i!=_count.length;i++){
00213 code =h.charCode(i);
00214 if(!Debug.DISTIBUTION) Debug.print("de code van de het " +i+"-de woord met kans "+ h._count[i]+ " ="+ code);
00215 COMPR+=code.length()*h._count[i];
00216 ENTROPIE+=Math.log(h._count[i])*h._count[i];
00217 }
00218
00219 Debug.print("Entropie:= "+ (-ENTROPIE)+ " Compressie := " + COMPR + " log_2(N):= "+ Math.log(_count.length)/Math.log(2));
00220 }
00221 }
00222
00229 class PriorityQueue extends Vector {
00235 public PriorityQueue() {
00236 super();
00237 }
00238
00246 public void insert(PairElement elem) {
00247 int i;
00248
00249 i = 0;
00250 while(i < size() &&
00251 ((PairElement) elementAt(i)).freq < elem.freq) {
00252 i++;
00253 }
00254
00255 insertElementAt(elem, i);
00256 }
00257
00264 public PairElement removeMin() {
00265 PairElement e;
00266
00267 if(size() > 0) {
00268 e = (PairElement) firstElement();
00269 removeElementAt(0);
00270
00271 return e;
00272 } else {
00273 return null;
00274 }
00275 }
00276 }
00277
00283 class PairElement extends Object {
00287 int key;
00291 float freq;
00297 public PairElement(int key, float freq) {
00298 super();
00299
00300 this.key = key;
00301 this.freq = freq;
00302 }
00303 }