java实现哈夫曼树的创建和哈夫曼编码的输出

                       java与数据结构


一,首先创建HaffmanNode类
package cn.netjava.htree1;

public class HaffmanNode {
int weight;//定义权值

int parent, left, right;

public HaffmanNode(int weight) {
this.weight = weight;
this.parent = -1;//如果为-1表示为null
this.left = -1;
this.right = -1;
}
         
          //定义构造器
public HaffmanNode() {
this(0);
}

//定义打印语句
         public String toString() {
    return this.weight + ", " + this.parent + ", " + this.left + ", " + this.right;
}
}



二,再创建HaffmanTree类


package cn.netjava.htree1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class HaffmanTree {
private int leafNum;//定义树中叶子节点个数

private HaffmanNode[] hnodes;//定义节点数组

  //构造哈夫曼树
public HaffmanTree(int[] weight) {//定义int数组存储叶子节点的权值
int n = weight.length; //数组长度为n
this.leafNum = n;
this.hnodes = new HaffmanNode[2 * n - 1];
for (int i = 0; i < n; i++)
//定义每一个叶子节点的权值
this.hnodes[i] = new HaffmanNode(weight[i]);

for (int i = 0; i <n - 1; i++) {
int min1, min2, x1, x2; //定义4个整型数,用来判断
min1 = min2 = 10000;
x1 = x2 = -1;
//从所有节点中选取总是最小的两个结点
for (int j = 0; j <n + i; j++) {
if (hnodes[j].weight < min1 && hnodes[j].parent == -1) {
min2 = min1;
x2 = x1;
min1 = hnodes[j].weight;
x1 = j;
} else if (hnodes[j].weight < min2 && hnodes[j].parent == -1) {
min2 = hnodes[j].weight;
x2 = j;
}
}
         //此时x1和x2分别为节点中权值最小的2个结点的序号
hnodes[x1].parent = n + i;
hnodes[x2].parent = n + i;
//定义x1和x2的父节点
this.hnodes[n + i] = new HaffmanNode();
hnodes[n + i].weight = hnodes[x1].weight + hnodes[x2].weight;
hnodes[n + i].left = x1;
hnodes[n + i].right = x2;
}
}

//定义输出语句
public String toString() {
String str = "";
for (int i = 0; i < this.hnodes.length && hnodes[i] != null; i++)
str += i + ", " + this.hnodes[i].toString() + "\n";
return str;
}

//求哈夫曼编码
public String[] haffmanCode() {
String[] code = new String[this.leafNum];//定义一个字符串数组
for (int i = 0; i < this.leafNum; i++) {
code[i] = "";
int child = i;
int parent = hnodes[child].parent;
while (parent != -1) {
//判断如果是左结点就为0,右结点就为1
if (hnodes[parent].left == child)
code[i] = "0" + code[i];
else
code[i] = "1" + code[i];
child = parent;
parent = hnodes[child].parent;
}
}
return code;
}
  
//输入一些字符串
public static int[] Input(int m) throws IOException {
String n = new String();
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);
int a[] = new int[m];
System.out.print("请输入" + m + "个数据:");
for (int j = 0; j < m; j++) {
n = in.readLine();
a[j] = Integer.parseInt(n);
}
return a;
}


public static void main(String[] args) throws IOException {
String n = new String();
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);
System.out.print("请输入数据总数:");
n = in.readLine();
int m = Integer.parseInt(n);
int[] weight = Input(m);
HaffmanTree htree = new HaffmanTree(weight);
System.out.println("哈夫曼树的结点数组:\n" + htree.toString());
String[] code = htree.haffmanCode();
System.out.println("哈夫曼编码:");
for (int i = 0; i < code.length; i++)
System.out.println(code[i]);
}
}

可以编译运行。。。。。。。

你可能感兴趣的:(java,数据结构,J#)