Advertisement
Guest User

Untitled

a guest
Dec 7th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. input file data:
  2. a
  3. a
  4. a
  5. a
  6. d
  7. d
  8. d
  9. d
  10. d
  11. d
  12. d
  13. d
  14. k
  15. k
  16. k
  17. k
  18. k
  19. k
  20. f
  21. f
  22. f
  23. f
  24. f
  25. f
  26. h
  27. h
  28. h
  29. h
  30. h
  31. h
  32. b
  33. b
  34. b
  35. b
  36. b
  37. b
  38. b
  39. b
  40. n
  41. n
  42. n
  43. n
  44. n
  45. n
  46. n
  47. e
  48. e
  49. e
  50. e
  51. e
  52. i
  53. i
  54. i
  55. i
  56. i
  57. i
  58. i
  59. i
  60. l
  61. k
  62. j
  63. a
  64. n
  65. s
  66. g
  67. l
  68. k
  69. j
  70. a
  71. s
  72. v
  73. o
  74. i
  75. j
  76. a
  77. s
  78. d
  79. l
  80. k
  81. g
  82. k
  83. n
  84. m
  85. a
  86. s
  87. d
  88. k
  89. l
  90. o
  91. v
  92. h
  93. a
  94. s
  95. d
  96. g
  97. z
  98.  
  99.  
  100. Code:
  101.  
  102. import java.util.*;
  103. import java.io.*;
  104.  
  105. class HuffmanNode implements Comparable<HuffmanNode>{
  106. HuffmanNode right;
  107. HuffmanNode left;
  108. HuffmanNode parent;
  109. int count;
  110. String letter;
  111.  
  112. public HuffmanNode(){}
  113.  
  114. public HuffmanNode (String letter, int count){
  115. this.letter = letter;
  116. this.count = count;
  117. }
  118. public HuffmanNode (String letter, int count, HuffmanNode parent, HuffmanNode left, HuffmanNode right){
  119. this.letter = letter;
  120. this.count = count;
  121. this.left = left;
  122. this.right = right;
  123. this.parent = parent;
  124. }
  125.  
  126. public void setCount(int count){
  127. this.count = count;
  128. }
  129.  
  130. public int getCount(){
  131. return count;
  132. }
  133.  
  134. public void setRight(HuffmanNode right){
  135. this.right = right;
  136. }
  137.  
  138. public HuffmanNode getRight(HuffmanNode right){
  139. return right;
  140. }
  141.  
  142. public void setLeft(HuffmanNode left){
  143. this.left = left;
  144. }
  145.  
  146. public HuffmanNode getLeft(HuffmanNode left){
  147. return left;
  148. }
  149. public void setParent(HuffmanNode right){
  150. this.left = left;
  151. }
  152. public HuffmanNode getParent(HuffmanNode parent){
  153. return parent;
  154. }
  155.  
  156. public void buildTree(HuffmanNode node){
  157. if (node.compareTo(this) <= 0 && left != null){
  158. System.out.println(node.getCount() + ":" + this.count);
  159. left.buildTree(node);
  160. }
  161. else if (node.compareTo(this) <= 0 && left == null){
  162. this.left = node;
  163. node.parent = this;
  164. }
  165. else if (node.compareTo(this) > 0 && right != null){
  166. System.out.println(node.getCount() + ":" +this.count);
  167. right.buildTree(node);
  168. }
  169. else if (node.compareTo(this) > 0 && right == null){
  170. this.right = node;
  171. node.parent = this;
  172. }
  173. }
  174.  
  175.  
  176. public int compareTo(HuffmanNode x){
  177. return this.count - x.count;
  178. }
  179. public void genCode(String s){
  180. if(left != null){
  181. left.genCode(s + "0");
  182. }
  183. if(right != null){
  184. right.genCode(s + "1");
  185. }
  186. if (left == null && right == null){
  187. System.out.println(s);
  188. }
  189. }
  190. }
  191.  
  192. public class hw4{
  193. public static void main (String []args)throws IOException{
  194.  
  195. //ask user to enter file name
  196. System.out.printf("Enter a file location and name to encode [press Enter]: ");
  197. Scanner input = new Scanner(System.in);
  198. String filename = input.next();
  199.  
  200. //Gets file name from Scanner and checks to see if valid
  201. File file = new File(filename);
  202. //if (!file.isFile()){
  203. //System.out.printf("Enter a file location and name to encode [press Enter]: ");
  204. //}
  205. Scanner text = new Scanner(file);
  206.  
  207. String[] letters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
  208. int[] freq = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  209.  
  210. String letter;
  211. String tempStr;
  212. int tempInt;
  213.  
  214. while(text.hasNext()){
  215. letter = text.next();
  216. //System.out.printf("%s\n", letter);
  217.  
  218. char c = letter.charAt(0);
  219. int index = c - 97;
  220. freq[index]++;
  221. }
  222.  
  223. for(int i=0; i <25; i++){
  224. System.out.printf("%s:%d\n", letters[i], freq[i]);
  225. }
  226. System.out.printf("\n");
  227.  
  228. for (int n=0; n <25; n++) {
  229. for (int i=0; i <25; i++) {
  230. if (freq[i] > freq[i+1]) {
  231. // exchange elements
  232. tempInt = freq[i];
  233. tempStr = letters[i];
  234. freq[i] = freq[i+1];
  235. letters[i] = letters[i+1];
  236. freq[i+1] = tempInt;
  237. letters[i+1] = tempStr;
  238. }
  239. }
  240. }
  241.  
  242. PriorityQueue<HuffmanNode> huffmanList = new PriorityQueue<HuffmanNode>();
  243.  
  244. for(int i=0; i <26; i++){
  245. System.out.printf("%s:%d\n", letters[i], freq[i]);
  246. if(freq[i] > 0){
  247. huffmanList.add(new HuffmanNode(letters[i],freq[i]));
  248. }
  249. }
  250.  
  251. HuffmanNode root = new HuffmanNode();
  252.  
  253. while(huffmanList.size() > 1){
  254. HuffmanNode x = huffmanList.poll();
  255. HuffmanNode y = huffmanList.poll();
  256. HuffmanNode result = new HuffmanNode("-", x.getCount() + y.getCount(), null, x, y);
  257. if(root == null){
  258. root = result;
  259. }
  260. else{
  261. root.buildTree(result);
  262. }
  263. huffmanList.add(result);
  264. }
  265. root.genCode(" ");
  266. }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement