好久没写代码了,这个是一个朋友问的要C实现,由于不会C,就用JAVA写了个简单的。注释掉的代码属性按照原来朋友发的题里带的参数,发现没什么用就给注释掉了。
package other; import java.util.HashMap; public class Huffman { public static Bean huffmanBean = new Bean(); public static HuffCode huff=new HuffCode(); public static void main(String[] args) { Bean[] beans = initD(); beans = arrSort(beans); getHuffmanT(beans); getHuffmanCode(huffmanBean,0); } public static void getHuffmanCode(Bean node,int c){ if(node.getLeft()!=null){ huff.put(c,0); getHuffmanCode(node.getLeft(), c + 1); } if(node.getName()!=null) System.out.println("节点名:"+node.getName()+" 概率值:"+String.valueOf(node.getProb())+" 哈夫曼编码值:"+huff.toString().substring(0,c)); if(node.getRight()!=null){ huff.put(c,1); getHuffmanCode(node.getRight(), c + 1); } } /** * 获取Huffman树 * @param beans */ public static void getHuffmanT(Bean[] beans){ while(beans.length>1){ Bean tempBean = getBeanRoot(beans); Bean[] nBeans = arrInsert(beans,tempBean); getHuffmanT(nBeans); if(nBeans.length==1){ huffmanBean = nBeans[0]; } break; } } /** * 获取最小值和后的新节点 * @param beans * @return */ public static Bean getBeanRoot(Bean[] beans){ Bean newBean = new Bean(); newBean.setProb(beans[beans.length-1].getProb()+beans[beans.length-2].getProb()); newBean.setLeft(beans[beans.length-2]); newBean.setRight(beans[beans.length-1]); beans[beans.length-1].setParent(newBean); // beans[beans.length-1].setNum("1"); beans[beans.length-2].setParent(newBean); // beans[beans.length-2].setNum("0"); return newBean; } /** * 插入后重排序 * 可以改成直接插入 * @param beans * @param bean * @return */ public static Bean[] arrInsert(Bean[] beans,Bean bean){ Bean[] nBeans = new Bean[beans.length-1]; for (int i = 0; i < beans.length-2; i++) { nBeans[i]=beans[i]; } nBeans[nBeans.length-1] = bean; nBeans = arrSort(nBeans); return nBeans; } /** * 冒泡排序 * @param bean * @return */ public static Bean[] arrSort(Bean[] bean){ for (int i = 0; i < bean.length; i++) { for (int j = i+1; j < bean.length; j++) { if (bean[j].getProb()>bean[i].getProb()) { Bean temp = new Bean(); temp=bean[j]; bean[j]=bean[i]; bean[i]=temp; } } } return bean; } /** * 初始化测试数据 * @return */ public static Bean[] initD(){ Bean b1 = new Bean("a",0.2f); Bean b2 = new Bean("b",0.19f); Bean b3 = new Bean("c",0.18f); Bean b4 = new Bean("d",0.17f); Bean b5 = new Bean("e",0.15f); Bean b6 = new Bean("f",0.1f); Bean b7 = new Bean("g",0.01f); Bean[] bean = new Bean[]{b1,b2,b3,b4,b5,b7,b6}; return bean; } } class HuffCode extends HashMap{ public String toString(){ String str=""; for(int i=0;i<this.size();i++){ str+=this.get(i); } return str; } } class Bean{ private Bean left; private Bean right; // private Bean next; private Bean parent; private float prob; // private String num; private String name; public Bean(){ } public Bean(String name,float prob){ this.prob = prob; this.name = name; } public Bean getLeft() { return left; } public void setLeft(Bean left) { this.left = left; } public Bean getRight() { return right; } public void setRight(Bean right) { this.right = right; } // public Bean getNext() { // return next; // } // public void setNext(Bean next) { // this.next = next; // } public Bean getParent() { return parent; } public void setParent(Bean parent) { this.parent = parent; } public float getProb() { return prob; } public void setProb(float prob) { this.prob = prob; } // public String getNum() { // return num; // } // public void setNum(String num) { // this.num = num; // } public String getName() { return name; } public void setName(String name) { this.name = name; } }