huffman(greedy)
Posted Wujunde
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)的主要内容,如果未能解决你的问题,请参考以下文章