如题
哈夫曼基本思想百度说的更好,在此不做赘余,直接上实现思路与具体代码。
1.先实现哈夫曼树的基础 节点 以及 编码 (假设树已经建好)
public class node {
String rode ="";
node left = null;
node right = null;
int value=1;
char c;
node (){}
node (char c){
this.c=c;
}
public void add() {
this.value++;
}
public void addleft(node a) {
this.left=a;
}
public void addright(node a) {
this.right=a;
}
public void printcode() {
if(this.left==null&&this.right==null) {
System.out.println(this.c+" "+this.rode);
return ;
}
else if(this.left!=null&&this.right==null) {
this.left.rode=this.rode + "0";
}
else if(this.left==null&&this.right!=null) {
this.right.rode=this.rode+ "1";
}
else {
this.left.rode=this.rode + "0";
this.righ