首页 > 代码库 > huffman(greedy)
huffman(greedy)
present a file by binary character code,let the less characters can be presented simplier.
1 package greedy; 2 3 import java.util.Iterator; 4 import java.util.LinkedList; 5 6 public class huffman { 7 private static class Node{ 8 private Node left ; 9 private Node right ; 10 private int freq; 11 public Node(Node left,Node right,int freq){ 12 this.left = left;this.right = right; this.freq = freq; 13 } 14 } 15 16 17 public Node Huffman(LinkedList<Node> list){ 18 int n = list.size(); 19 for(int i = 1;i <= n-1;i++){ 20 Node x = list.removeFirst(); 21 Node y = list.removeFirst(); 22 Node node = new Node(x,y,x.freq + y.freq); 23 for (int j = 0;j < list.size() - 1;j++){ 24 if(node.freq >= list.get(j).freq && node.freq <= list.get(j+1).freq){ 25 list.add(j+1,node); 26 } 27 else if( j+1 == list.size() - 1){ 28 list.addLast(node); 29 } 30 31 } 32 33 34 } 35 return list.getFirst(); 36 37 } 38 39 }
huffman(greedy)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。