资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

哈夫曼树java语言代码 C语言哈夫曼树的构造及编码

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

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

创新互联建站于2013年开始,是专业互联网技术服务公司,拥有项目成都网站制作、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元梁平做网站,已为上家服务,为梁平各地企业和个人服务,联系电话:18982081108

2)编写函数实现统计字符串中字符的种类以及各类字符的个数。

3)编写函数构造赫夫曼树。

4)编写函数实现由赫夫曼树求赫夫曼编码表。

5)编写函数实现将正文转换为相应的编码文件。

6)编写函数实现将编码文件进行译码。

7)编写主控函数,完成本实验的功能。

我用java构建哈夫曼树的时候报了空指针,代码如下

System.out.println("please input the second letter!");

char ch2 = tw.getChar();

if(ch2 == 'U') {System.out.println("Tuesday"); }

else if(ch2 == 'H') {System.out.println("Thursday"); }

哈夫曼编码与译码 java

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)

{

int[] weight={5,29,7,8,14,23,3,11}; //指定权值集合

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 哈夫曼树权值求和(代码找错)

兄弟,你把如下第28行的count++;注释掉,一切问题都可以解决!

自己先琢磨为什么,不懂的再问!

import java.util.Arrays;

import java.util.Scanner;

public class a2 {

public static int n, count = 0;

public static int he[] = new int[n + 1];// 预定义权值数组

public static void Huffman(int[] a) {

Arrays.sort(a);// 排序

// System.out.println(a.length);

if (a.length == 1)// 如果长度是1结束递归

{

he[count++] = a[0];

return;

}

if (a.length == 2)// 如果长度是2结束递归

{

he[count++] = a[0] + a[1];

return;

}

else // 长度大于2

{

int b[] = new int[a.length - 1];// 定义一个新数组,用于保存a

he[count++] = a[0] + a[1];

b[0] = he[--count];// 新数组的第一个元素为当前数组的最小的两个数之和

for (int i = 1; i  b.length; i++)

{

b[i] = a[i + 1];// 赋值除第一个元素之外的值

}

//   count++;

Huffman(b);// 递归

}

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

n = Integer.parseInt(sc.nextLine());

String s[] = sc.nextLine().trim().split(" ");// 输入n个数

int d[] = new int[n];

for (int i = 0; i  n; i++)

d[i] = Integer.parseInt(s[i]);// 将String转化为int的数组

Huffman(d);// 递归调用

int sum = 0;

for (int i = 0; i  he.length; i++)

sum += he[i];// 求和

System.out.println(sum);

}

}


分享文章:哈夫曼树java语言代码 C语言哈夫曼树的构造及编码
浏览地址:http://cdkjz.cn/article/dosoces.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220