Advertisement
Mattie

Untitled

Jun 12th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package huffman;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5.  
  6. public class Huffman
  7. {
  8.     private ArrayList<Entry> list = new ArrayList<Entry>();
  9.     private ArrayList<Entry> boom = new ArrayList<Entry>();
  10.     private String input;
  11.     private String code = "";
  12.  
  13.     public Huffman(String s)
  14.     {
  15.         input = s;
  16.     }
  17.  
  18.     public void temp()
  19.     {
  20.         for (Entry e : boom)
  21.         {
  22.             System.out.println("Char: '" + e.getChar() + "'   freq: " + e.getFreq() + "");
  23.         }
  24.     }
  25.  
  26.     public void createCode(Entry e)
  27.     {
  28.  
  29.     }
  30.  
  31.     public void maakBoom()
  32.     {
  33.  
  34.         while(list.size() > 1)
  35.         {
  36.  
  37.             //maak eerst 2 elementen met de laagste frequenty
  38.             Entry e1 = list.remove(0);
  39.             Entry e2 = list.remove(0);
  40.  
  41.             int count = e1.getFreq() + e2.getFreq();//bereken gezamelijke frequentie
  42.             Entry root = new Entry('\u0000', count);// \\u000 == null voor char volgens googel
  43.  
  44.             root.setLeftChild(e1);//maak 2 kinderen
  45.             root.setRightChild(e2);
  46.  
  47.             boom.add(root);
  48.  
  49.             sortList();
  50.  
  51.             maakBoom();
  52.         }
  53.  
  54.     }
  55.  
  56.     public void calcFreq()
  57.     {
  58.         char[] c = input.toCharArray();
  59.         Entry e = null;
  60.  
  61.         for (int i = 0; i < c.length; i++)
  62.         {
  63.             if (list.size() == 0)
  64.             {
  65.                 Entry e2 = new Entry(c[i], 1);// de 1 == de frequenty
  66.                 list.add(e2);
  67.             }
  68.             else
  69.             // de lijst met letters != leeg
  70.             {
  71.                 boolean komtvoor = false;
  72.                 for (int listint = 0; listint < list.size(); listint++)
  73.                 {
  74.                     e = list.get(listint);
  75.                     if (c[i] == e.getChar())
  76.                     {
  77.                         int freq = e.getFreq();
  78.                         freq++;
  79.                         e.setFreq(freq);
  80.                         komtvoor = true;
  81.                     }
  82.                 }
  83.                 if (!komtvoor)
  84.                 {
  85.                     Entry charding = new Entry(c[i], 1);
  86.                     list.add(charding);
  87.                 }
  88.             }
  89.         }
  90.         System.out.println("**********FREQUENTIES CALCULATED************");
  91.     }
  92.  
  93.     public void dispFreq()
  94.     {
  95.         for (int i = 0; i < list.size(); i++)
  96.         {
  97.             Entry e = list.get(i);
  98.             System.out.println("De char: '" + e.getChar() + "' komt " + e.frequenty + " keer voor in de string!");
  99.         }
  100.         System.out.println("**********LIST PRINTED************");
  101.     }
  102.  
  103.     public void sortList()
  104.     {
  105.         CharCountComparator bla = new CharCountComparator();
  106.         Collections.sort(list, bla);
  107.         System.out.println("**********LIST SORTED************");
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement