求完整的哈弗曼编码的java算术代码
答案:1 悬赏:0 手机版
解决时间 2021-11-20 18:01
- 提问者网友:我没有何以琛的痴心不悔
- 2021-11-19 19:18
求完整的哈弗曼编码的java算术代码
最佳答案
- 五星知识达人网友:猎心人
- 2021-11-19 20:17
书上不是有吗。。。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Huffman {
private List nums;
private List numsMo;
private List trees;
private String temp;
public Huffman() {
nums = new ArrayList();
numsMo = new ArrayList();
trees = new ArrayList();
temp = "";
}
public void addNums() {// 给定一组数
System.out.println("请输入一组数,中间用空格分隔:");
Scanner sca = new Scanner(System.in);
String str = sca.nextLine();
String[] strs = str.split(" ");
for (int i = 0; i < strs.length; i++) {
nums.add(Double.parseDouble(strs[i]));
numsMo.add(Double.parseDouble(strs[i]));
}
}
public void compareNum(List nums, List trees) {// 递归算法
double[] min = new double[2];
if (nums.size() > 1) {
min = minTwo(nums);
Tree t = new Tree(min[0], min[1], min[0] + min[1]);
nums.remove(Double.valueOf(min[0]));
nums.remove(Double.valueOf(min[1]));
nums.add(min[0] + min[1]);
trees.add(t);
compareNum(nums, trees);
}
}
public void print(double num) {// 递归打印编码
for (Tree t : trees) {
if (num == t.getRchild()) {
temp = 1 + temp;
print(t.getParents());
break;
} else if (num == t.getLchild()) {
temp = 0 + temp;
print(t.getParents());
break;
}
}
}
public void write(double d) {
temp = "";
System.out.print(d + " : ");
print(d);
System.out.print(temp);
System.out.println(" 码长:" + temp.length());
}
public double[] minTwo(List nums) {// 在一组数中选则最小的两个,按递增排序返回
Double temp = 0.0;
for (int j = 0; j < 2; j++) {
for (int i = 1; i < nums.size(); i++) {
if (nums.get(i - 1) < nums.get(i)) {
temp = nums.get(i);
nums.set(i, nums.get(i - 1));
nums.set(i - 1, temp);
}
}
}
double[] n = { nums.get(nums.size() - 1), nums.get(nums.size() - 2) };
return n;
}
public void start() {
addNums();
compareNum(nums, trees);
while (numsMo.size() > 1) {
double[] mins = minTwo(numsMo);
if (mins[0] != mins[1]) {
numsMo.remove(Double.valueOf(mins[0]));
write(mins[0]);
}
}
if (!numsMo.isEmpty()) {
write(numsMo.get(0));
}
}
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) {
this.lChild = lChild;
}
public double getRchild() {
return rChild;
}
public void setRchild(double rChild) {
this.rChild = rChild;
}
public double getParents() {
return parent;
}
public void setParents(double root) {
this.parent = root;
}
}
public static void main(String[] args) {
new Huffman().start();
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Huffman {
private List
private List
private List
private String temp;
public Huffman() {
nums = new ArrayList
numsMo = new ArrayList
trees = new ArrayList
temp = "";
}
public void addNums() {// 给定一组数
System.out.println("请输入一组数,中间用空格分隔:");
Scanner sca = new Scanner(System.in);
String str = sca.nextLine();
String[] strs = str.split(" ");
for (int i = 0; i < strs.length; i++) {
nums.add(Double.parseDouble(strs[i]));
numsMo.add(Double.parseDouble(strs[i]));
}
}
public void compareNum(List
double[] min = new double[2];
if (nums.size() > 1) {
min = minTwo(nums);
Tree t = new Tree(min[0], min[1], min[0] + min[1]);
nums.remove(Double.valueOf(min[0]));
nums.remove(Double.valueOf(min[1]));
nums.add(min[0] + min[1]);
trees.add(t);
compareNum(nums, trees);
}
}
public void print(double num) {// 递归打印编码
for (Tree t : trees) {
if (num == t.getRchild()) {
temp = 1 + temp;
print(t.getParents());
break;
} else if (num == t.getLchild()) {
temp = 0 + temp;
print(t.getParents());
break;
}
}
}
public void write(double d) {
temp = "";
System.out.print(d + " : ");
print(d);
System.out.print(temp);
System.out.println(" 码长:" + temp.length());
}
public double[] minTwo(List
Double temp = 0.0;
for (int j = 0; j < 2; j++) {
for (int i = 1; i < nums.size(); i++) {
if (nums.get(i - 1) < nums.get(i)) {
temp = nums.get(i);
nums.set(i, nums.get(i - 1));
nums.set(i - 1, temp);
}
}
}
double[] n = { nums.get(nums.size() - 1), nums.get(nums.size() - 2) };
return n;
}
public void start() {
addNums();
compareNum(nums, trees);
while (numsMo.size() > 1) {
double[] mins = minTwo(numsMo);
if (mins[0] != mins[1]) {
numsMo.remove(Double.valueOf(mins[0]));
write(mins[0]);
}
}
if (!numsMo.isEmpty()) {
write(numsMo.get(0));
}
}
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) {
this.lChild = lChild;
}
public double getRchild() {
return rChild;
}
public void setRchild(double rChild) {
this.rChild = rChild;
}
public double getParents() {
return parent;
}
public void setParents(double root) {
this.parent = root;
}
}
public static void main(String[] args) {
new Huffman().start();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯