用java写一个完整的系统具有以下功能: (11:初始化(lnitialization)。从终端读入宇符集大小口,以及口个字符和口个 权值,建立哈夫曼树,并将它存于文件hfimTree中。 (②E:编码(Encoding)。利用己建好的哈夫曼树(如不在内存,则从文件 hfinTree中读人),对文件ToBeTran中的正文进行编码,然后将结果存入文件 CodeFile T• (3)D:译码(Decoding)。利用己建好的哈夫曼树将文件CodeFile中的代码进 行译码,结果存入文件TextFile中。 (④P:打印代码文件(Print。将文件CodeFile以紧湊格式显示在终端上,每 行50个代码。同时将此字符形式的编码文件写入文件CodePrin中。 (5T:打印哈夫曼树(Treeprinting)。将已在内存中的哈夫曼树以直观的方式 (树或四入表形式)显示在终端上,同时将此字符形式的哈夫曼树写入文件 TreePrint中。
时间: 2023-06-26 16:09:29 浏览: 80
以下是一个Java程序,实现了上述功能:
```
import java.io.*;
import java.util.*;
class Node implements Comparable<Node> {
char symbol;
int weight;
Node left, right;
public Node(char symbol, int weight) {
this.symbol = symbol;
this.weight = weight;
this.left = null;
this.right = null;
}
public Node(Node left, Node right) {
this.symbol = '\0';
this.weight = left.weight + right.weight;
this.left = left;
this.right = right;
}
public int compareTo(Node other) {
return weight - other.weight;
}
public boolean isLeaf() {
return left == null && right == null;
}
}
class HuffmanTree {
private Node root;
private Map<Character, String> codes;
public HuffmanTree(Map<Character, Integer> frequencyMap) {
PriorityQueue<Node> pq = new PriorityQueue<>();
for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
pq.offer(new Node(entry.getKey(), entry.getValue()));
}
while (pq.size() > 1) {
Node left = pq.poll();
Node right = pq.poll();
pq.offer(new Node(left, right));
}
root = pq.poll();
codes = new HashMap<>();
generateCodes(root, "");
}
private void generateCodes(Node node, String code) {
if (node.isLeaf()) {
codes.put(node.symbol, code);
} else {
generateCodes(node.left, code + "0");
generateCodes(node.right, code + "1");
}
}
public void saveToFile(String filename) throws IOException {
try (DataOutputStream out = new DataOutputStream(new FileOutputStream(filename))) {
writeTree(out, root);
}
}
private void writeTree(DataOutputStream out, Node node) throws IOException {
if (node.isLeaf()) {
out.writeBoolean(true);
out.writeChar(node.symbol);
} else {
out.writeBoolean(false);
writeTree(out, node.left);
writeTree(out, node.right);
}
}
public static HuffmanTree loadFromFile(String filename) throws IOException {
try (DataInputStream in = new DataInputStream(new FileInputStream(filename))) {
Node root = readTree(in);
HuffmanTree tree = new HuffmanTree(Collections.emptyMap());
tree.root = root;
tree.codes = new HashMap<>();
tree.generateCodes(root, "");
return tree;
}
}
private static Node readTree(DataInputStream in) throws IOException {
boolean isLeaf = in.readBoolean();
if (isLeaf) {
char symbol = in.readChar();
return new Node(symbol, 0);
} else {
Node left = readTree(in);
Node right = readTree(in);
return new Node(left, right);
}
}
public String encode(String text) {
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray()) {
sb.append(codes.get(c));
}
return sb.toString();
}
public String decode(String code) {
StringBuilder sb = new StringBuilder();
Node node = root;
for (char c : code.toCharArray()) {
if (c == '0') {
node = node.left;
} else {
node = node.right;
}
if (node.isLeaf()) {
sb.append(node.symbol);
node = root;
}
}
return sb.toString();
}
public void printCodeToFile(String filename, String code) throws IOException {
try (PrintWriter out = new PrintWriter(filename)) {
for (int i = 0; i < code.length(); i += 50) {
out.println(code.substring(i, Math.min(i + 50, code.length())));
}
}
}
public void printTreeToFile(String filename) throws IOException {
try (PrintWriter out = new PrintWriter(filename)) {
printTree(out, root, "");
}
}
private void printTree(PrintWriter out, Node node, String indent) {
if (node.isLeaf()) {
out.println(indent + node.symbol);
} else {
out.println(indent + "*");
printTree(out, node.left, indent + " ");
printTree(out, node.right, indent + " ");
}
}
}
public class HuffmanCoding {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter alphabet size: ");
int size = scanner.nextInt();
char[] symbols = new char[size];
int[] weights = new int[size];
for (int i = 0; i < size; i++) {
System.out.print("Enter symbol and weight: ");
symbols[i] = scanner.next().charAt(0);
weights[i] = scanner.nextInt();
}
Map<Character, Integer> frequencyMap = new HashMap<>();
for (int i = 0; i < size; i++) {
frequencyMap.put(symbols[i], weights[i]);
}
HuffmanTree tree = new HuffmanTree(frequencyMap);
System.out.print("Enter input filename: ");
String inputFilename = scanner.next();
String text = readFile(inputFilename);
String code = tree.encode(text);
System.out.print("Enter output filename for code file: ");
String codeFilename = scanner.next();
writeFile(codeFilename, code);
System.out.print("Enter output filename for text file: ");
String textFilename = scanner.next();
String decodedText = tree.decode(code);
writeFile(textFilename, decodedText);
System.out.print("Enter output filename for code printing: ");
String codePrintFilename = scanner.next();
tree.printCodeToFile(codePrintFilename, code);
System.out.print("Enter output filename for tree printing: ");
String treePrintFilename = scanner.next();
tree.printTreeToFile(treePrintFilename);
System.out.println("Done.");
}
private static String readFile(String filename) throws IOException {
try (BufferedReader in = new BufferedReader(new FileReader(filename))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
}
private static void writeFile(String filename, String text) throws IOException {
try (BufferedWriter out = new BufferedWriter(new FileWriter(filename))) {
out.write(text);
}
}
}
```
阅读全文