Guest User

Untitled

a guest
Jan 12th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6.  
  7. public class Huff
  8. {
  9. File file;
  10. BufferedReader reader;
  11. BitInputStream inputStream;
  12. BitOutputStream outputStream;
  13. PriorityQueue queue;
  14. String string = "";
  15. char[] tokens;
  16. ArrayList<TreeNode> array = new ArrayList<TreeNode>();
  17.  
  18.  
  19.  
  20. public Huff(String file)
  21. {
  22. this(new File(file));
  23. }
  24.  
  25. public Huff(File file)
  26. {
  27. this.file = file;
  28. }
  29.  
  30. public void read()
  31. {
  32. try {
  33. reader = new BufferedReader(new FileReader(file));
  34. while(reader.ready())
  35. {
  36. string += reader.readLine();
  37. }
  38. tokens = string.toCharArray();
  39. reader.close();
  40. }
  41. catch(Exception e)
  42. {
  43. e.printStackTrace();//this needs to be perfected with more catches.
  44. return;
  45. }
  46.  
  47. boolean toAdd = true;
  48.  
  49. for(int i = 0; i < tokens.length; i++) {
  50.  
  51. for(int a = 0; a < array.size(); a++) {
  52.  
  53. if(tokens[i] == (Character)(array.get(a).getValue())) {
  54. array.get(a).setWeight(array.get(a).getWeight() + 1);
  55. toAdd = false;
  56. break;
  57. }
  58. }
  59.  
  60. if(toAdd) {
  61. array.add(new TreeNode(tokens[i], 1));
  62. }
  63.  
  64. toAdd = true;
  65. }
  66.  
  67. queue = new PriorityQueue(array);
  68.  
  69. TreeNode c1;
  70. TreeNode c2;
  71. TreeNode c3;
  72.  
  73. while (queue.size() > 1) {
  74. c1 = (TreeNode)queue.remove();
  75. c2 = (TreeNode)queue.remove();
  76. c3 = new TreeNode(c1.getWeight() + c2.getWeight(), c1.getWeight() + c2.getWeight(), c1, c2);
  77. queue.add(c3);
  78. }
  79. }
  80.  
  81. public static void main(String[] args) {
  82. Huff p = new Huff("test.txt");
  83. p.read();
  84. }
  85.  
  86. }
Add Comment
Please, Sign In to add comment