JAVA和数据结构大虾们帮下忙: 输入一段文字,该程序可以统计出每个字符出现的次数并对字符进行哈夫曼编码

2024-11-28 15:00:51
推荐回答(1个)
回答1:

写给你了,请发JAVA版块
package other;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* 定义了一种接口,搭闭芹要进行编码的最小单态启元类必需实现些接口
*
*/

interface Combinable extends Comparable {
T combinate(T a, T b);
}

/**
*
* the huffman tree Class 哈夫曼树,包括当前节点数据信息,左节点,右节点信息。
*/

class HuffmanTree> implements
Comparable> {

/** the root of huffman tree */

private T root;

/** the left node of huffman tree */

private HuffmanTree left;

/** the right node of huffman tree */

private HuffmanTree right;

/** 哈夫曼编码字符串,如:0000110 */

private String huffmanCodeString = "";

/** 是否对最终生成的哈夫曼进行过编码操作 */

private static boolean isSettedHuffmanCoderString = false;

public T getRoot() {
return root;
}

public void setRoot(T root) {
this.root = root;
}

public HuffmanTree getLeft() {
return left;
}

public void setLeft(HuffmanTree left) {
this.left = left;
}

public HuffmanTree getRight() {
return right;
}

public void setRight(HuffmanTree right) {
this.right = right;
}

/**
*
* 重写此方法用于递归合并节点后进行排序操作
*
*/

public int compareTo(HuffmanTree o) {
return o.getRoot().compareTo(this.getRoot());
}

public String toString() {
return "the root:" + this.getRoot() + "\r\nthe left:" + this.getLeft()
+ "\r\nthe right:" + this.getRight();
}

/**
*
* 对最终生成的树进行哈夫曼编码,使每个叶子节点生成知毕01的路径编码
*
*/

private void setHuffmanCoderString() {
HuffmanTree left = this.getLeft();
// 如果有左节点则在路径中追加"0"
if (left != null) {
left.huffmanCodeString = this.huffmanCodeString + "0";
left.setHuffmanCoderString();// 递归编码
}
HuffmanTree right = this.getRight();
// 如果有右节点则在路径中追加"1"

if (right != null) {
right.huffmanCodeString = this.huffmanCodeString + "1";
right.setHuffmanCoderString();// 递归编码
}
}

public void printHuffmanCoderString() {

// 打印最终生成树的哈夫曼编码前要进行编码操作,

// 且此操作只执行一次,所以用全局标识变量判断

if (!HuffmanTree.isSettedHuffmanCoderString) {
this.setHuffmanCoderString();
HuffmanTree.isSettedHuffmanCoderString = true;// 标识已执行过编码
}
// 如果是叶子节点(即要编码的单元),则打印出编码信息,如果是非叶子结点(中间临时生成的节点),则不打印
if (this.left == null && this.right == null)
System.out.println("the " + this.getRoot() + " huffmanCoder:"
+ this.huffmanCodeString);
if (this.left != null)
this.left.printHuffmanCoderString();// 递归打印
if (this.right != null)
this.right.printHuffmanCoderString();// 递归打印
}
}

/**
*
* 用类用于生成一个哈夫曼树
*/

class HuffmanTreeFactory> {
/** 初始时一个list列表当作要编码的单元类的容器 */
private List> HuffmanTreeCollection;

/**
*
* @param unitClasses
* 待编码的单元类集合
*
*/

public HuffmanTreeFactory(List unitClasses) {
if (unitClasses == null || unitClasses.size() == 0)
throw new IllegalArgumentException(
"the unit classes collection can't be empty");
HuffmanTreeCollection = new LinkedList>();
// 初始化哈夫曼集合容器
for (T unitClass : unitClasses) {
HuffmanTree huffmanTree = new HuffmanTree();
huffmanTree.setRoot(unitClass);
huffmanTree.setLeft(null);
huffmanTree.setLeft(null);
HuffmanTreeCollection.add(huffmanTree);
}
Collections.sort(HuffmanTreeCollection);
}

/**
* 将待编码的哈夫曼集合处理成只含有一个元素的集合,则这最后一个元素 即为最终生成的哈夫曼树
*/
private void generateHuffmanTree() {
while (true) {
if (HuffmanTreeCollection == null
|| HuffmanTreeCollection.size() <= 1)
break;
// 处理之前一定要重新排序,这是哈夫曼编码的关键原理
Collections.sort(HuffmanTreeCollection);
HuffmanTree huffmanTreeOne = HuffmanTreeCollection.remove(0);
HuffmanTree huffmanTreeTwo = HuffmanTreeCollection.remove(0);
HuffmanTree huffmanTreeNew = new HuffmanTree();
// 将集合中前面两个元素合并成一个元素插到集合中去

// 并将第一个元素和第二个元素分别作为新元素的左,右节点
huffmanTreeNew.setRoot(huffmanTreeOne.getRoot().combinate(
huffmanTreeOne.getRoot(), huffmanTreeTwo.getRoot()));
huffmanTreeNew.setLeft(huffmanTreeOne);
huffmanTreeNew.setRight(huffmanTreeTwo);
HuffmanTreeCollection.add(huffmanTreeNew);
}
}

/**
*
*
*
* @return 生成最终的哈夫曼树
*
*/

public HuffmanTree getHuffmanTree() {
generateHuffmanTree();

return this.HuffmanTreeCollection.get(0);
}
}

/**
* 自定义一个用于测试的单元类
*/

class UnitClass implements Serializable, Combinable {

/** 出现概率数据 */
private int rate;

public UnitClass(int rate) {
this.rate = rate;
}

public int getRate() {
return rate;
}

public void setRate(int rate) {
this.rate = rate;
}

/**
* implements thid compartTo() in order to sort the
*
* collection stored from unitclass
*/
public int compareTo(UnitClass o) {
return o.getRate() > this.rate ? 1 : o.getRate() < this.rate ? -1 : 0;
}

public String toString() {
return "the rate is:" + rate;
}

/**
*
* 重写此方法用于哈夫曼编码时可以合并两个分支点信息
*
*/
public UnitClass combinate(UnitClass a, UnitClass b) {
if (a == null || b == null)
return null;
return new UnitClass(a.getRate() + b.getRate());
}
}

public class Test {
public static int counter(String s, char c) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
count++;
}
}
return count;
}

public static void main(String[] args) {
String str = "你好呵呵123abbeab啊";
List l = new ArrayList();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
System.out.println(c + "出现了" + counter(str, c) + "次");
l.add(new UnitClass(c));
}
HuffmanTreeFactory factory = new HuffmanTreeFactory(
l);
factory.getHuffmanTree().printHuffmanCoderString();
}

}