class HaffmanNode //哈夫曼树的结点类
{
int weight; //权值
int parent,left,right; //父母结点和左右孩子下标
public HaffmanNode(int weight)
this.weight = weight;
this.parent=-1;
this.left=-1;
this.right=-1;
}
public HaffmanNode()
this(0);
public String toString()
return this.weight+", "+this.parent+", "+this.left+", "+this.right;
return code;
public static void main(String[] args)
HaffmanTree htree = new HaffmanTree(weight);
System.out.println("哈夫曼树的结点数组:\n"+htree.toString());
String[] code = htree.haffmanCode();
System.out.println("哈夫曼编码:");
for (int i=0; icode.length; i++)
System.out.println(code[i]);
①.)编写函数实现选择parent为0且权值最小的两个根结点的算法
只要自己再加个类Tree就可以了.
代码如下:
public class Tree {
double lChild, rChild, parent;
public Tree (double lChild, double rChild, double parent) {
this.lChild = lChild;
this.rChild = rChild;
this.parent = parent;
public double getLchild() {
return lChild;
public void setLchild(double lChild) {
public double getRchild() {
return rChild;
public void setRchild(double rChild) {
public double getParents() {
return parent;
public void setParents(double root) {
this.parent = root;
可以在Dog与Cat类中重写Animal中的animalDo方法,通过调用animalDo方法,
然后会自动根据不同的实例调用不同类中的方法(多态知识).
以上就是土嘎嘎小编为大家整理的哈夫曼经典代码java相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!