网站首页 > 文章中心 > 其它

哈夫曼经典代码java

作者:小编 更新时间:2023-10-20 10:54:47 浏览量:306人看过

哈夫曼编码与译码 java

class HaffmanNode //哈夫曼树的结点类

{

哈夫曼经典代码java-图1

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);

哈夫曼经典代码java-图2

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]);

哈夫曼树编码的应用(Java语言)

①.)编写函数实现选择parent为0且权值最小的两个根结点的算法

用java实现哈夫曼编码

只要自己再加个类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() {

哈夫曼经典代码java-图3

return parent;

public void setParents(double root) {

this.parent = root;

Java实现哈夫曼算法,运行出现问题,求帮助,在线等!!!

可以在Dog与Cat类中重写Animal中的animalDo方法,通过调用animalDo方法,

然后会自动根据不同的实例调用不同类中的方法(多态知识).

以上就是土嘎嘎小编为大家整理的哈夫曼经典代码java相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章