[英]Huffman code decoder - send a dictionary of encoder
嗨,我是一名計算機科學專業的學生 - 在我的第二年。 在我學習期間,他們讓我做一個項目。 構建霍夫曼代碼。 我構建的大部分代碼對我來說效果很好。 但是我幾乎沒有卡住,我構建的編碼器很好,但是解碼器給我帶來了問題。
編碼工作正常。 編碼是根據每個字符的字典完成的。 解碼也需要這個字典。 我的問題是如何將字典傳遞給解碼器——而不改變原始函數的簽名。
我的困難是實現解碼器 - 不改變講師設置的 4 個功能。 只要結構保持不變,我可以為原來的 4 個函數添加任意數量的函數。
編碼器 - 接收文件 - 並將其轉換為字節 - 進行壓縮。 它遍歷原始文件,計算每個字符的印象數,然后為每個字符構建一個帶有代碼的樹。
解碼器 - 需要獲取樹 - 將字節轉換為原始文件 - 我在這里卡住了。 如何從編碼器發送解碼器的樹。
該代碼具有預定義的類和功能 - 根據講師的說明。 我將無法將簽名更改為函數。 但是,我可能會創建一些我想要的功能。
不可更改的功能:
public class HuffmanEncoderDecoder implements Compressor
{
public HuffmanEncoderDecoder()
{
// TODO Auto-generated constructor stub
}
@Override
public void Compress(String[] input_names, String[] output_names)
{
// TODO Auto-generated method stub
}
@Override
public void Decompress(String[] input_names, String[] output_names)
{
// TODO Auto-generated method stub
}
@Override
public byte[] CompressWithArray(String[] input_names, String[] output_names)
{
// TODO Auto-generated method stub
return null;
}
@Override
public byte[] DecompressWithArray(String[] input_names, String[] output_names)
{
// TODO Auto-generated method stub
return null;
}
}
我已經實現的代碼
public static void main(String[] args) {
String[] input_names = {"C:\\Documents\\files-huffman\\check.txt"};
String[] output_names = {"C:\\Documents\\files-huffman\\out.txt"};
HuffmanEncoderDecoder objectHuffman = new HuffmanEncoderDecoder();
objectHuffman.Compress(input_names, output_names);
//how to send to the objectHuffman.Decompress the tree or the map of codes
check.txt 包含:abra cadabra out.txt 包含:0110111010101011010001101110
我只構建了第一個編碼文件的 function。 以及幫助我編碼的輔助函數 - 例如計算輸入文件中字符數的 function 和構建並返回樹的 function
我的問題是編碼文件后如何 - 我可以根據項目條款對其進行解碼? 謝謝您的幫助。
我只構建了第一個編碼文件的 function。 以及幫助我編碼的輔助函數 - 例如計算輸入文件中字符數的 function 和構建並返回樹的 function
在論壇幫助之后,我能夠將樹從編碼器移動到解碼器。 我將樹添加到一個文件中,然后在解碼器中創建了一個新樹。
我添加了我編寫的代碼: 代碼顯示我正在移動的樹 - 是一棵空白樹 - 但它不可能 - 因為編碼工作正常。
為了移動 map - 我創建了 2 個函數,readTreeFile 和 writeTreeFile。 一個在編碼器中工作 - map 被復制到一個文件中。 第二個工作解碼器 - 它從文件中提取 map。
public static Map<Character, String> readTreeFile (String[] output_names)
{
Map<Character, String> codes = new HashMap<>();
try {
FileInputStream f = new FileInputStream(output_names[1]);
ObjectInputStream s = new ObjectInputStream(f);
codes = (HashMap<Character, String>) s.readObject();
s.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return codes;
}
public static void writeTreeFile (Map<Character, String> codes , String[] output_names)
{
try {
FileOutputStream f = new FileOutputStream(output_names[1]);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(codes);
s.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.