java - Huffman Decompression -
i have been working on huffman compression , decompression program. @ stage, compression program works fine, can't seem make decompression code reproduce original file compressed. guess doing wrong. complete code below. please appreciated.
import java.io.*; import java.util.*; public class huffmain { public static priorityqueue<node> q; public static hashmap<character, string> chartocode; public static hashmap<string, character> codetochar; @suppresswarnings("resource") public static void main(string[] args) throws filenotfoundexception { // read contents of file string text = new scanner(new file("text.txt")).usedelimiter("\\a").next(); // count frequency of characters hashmap<character, integer> dict = new hashmap<character, integer>(); for(int = 0; < text.length(); i++) { char = text.charat(i); if(dict.containskey(a)) dict.put(a, dict.get(a)+1); else dict.put(a, 1); } // create forest (group of trees) adding nodes priority queue q = new priorityqueue<node>(100, new frequencycomparator()); int n = 0; for(character c : dict.keyset()) { q.add(new node(c, dict.get(c))); n++; } node root = huffmain(n); buildtable(root); string compressed = compress(text); savetofile(compressed); string decompressed = decompress(compressed); writetofile(decompressed); } // method builds tree based on frequency of every characters public static node huffmain(int n) { node x, y; for(int = 1; <= n-1; i++) { node z = new node(); z.left = x = q.poll(); z.right = y = q.poll(); z.freq = x.freq + y.freq; q.add(z); } return q.poll(); } // method builds table compression , decompression public static void buildtable(node root) { chartocode = new hashmap<character, string>(); codetochar = new hashmap<string, character>(); postorder(root, new string()); } // method used traverse root-to-leaves public static void postorder(node n, string s) { if(n == null) return; postorder(n.left, s+"0"); postorder(n.right, s+"1"); // visit nodes keys if (!character.tostring(n.alpha).equals("\0")){ system.out.println("{" + n.alpha + ":" + s + "}"); chartocode.put(n.alpha, s); codetochar.put(s, n.alpha); } } //assuming tree , dictionary built... public static string compress(string s) { string c = new string(); for(int = 0; < s.length(); i++) c = c + chartocode.get(s.charat(i)); return c; } //assuming tree , dictionary built... public static string decompress(string s) { string temp = new string(); string result = new string(); for(int = 0; < s.length(); i++) { temp = temp + s.charat(i); if(codetochar.containskey(temp)) { result = result + codetochar.get(temp); temp = new string(); } } return result; } public static void savetofile(string l) throws filenotfoundexception { printwriter ofile = new printwriter("text.txt.huf"); ofile.print(chartocode.size()); for(char s : chartocode.keyset()) ofile.println("{" +s + ":" + chartocode.get(s)+ "}"); ofile.println(l); ofile.close(); } public static void writetofile(string i) throws filenotfoundexception { printwriter ifile = new printwriter("note.txt"); ifile.print(codetochar.size()); (string s : codetochar.keyset()) ifile.println("{" +s + ":" + codetochar.get(s)+ "}"); ifile.println(i); ifile.close(); } } //creating node class class node { public char alpha; public int freq; public node left, right; public node(char a, int f) { alpha = a; freq = f; } public node() { } public string tostring() { return alpha + " " + freq; } } class frequencycomparator implements comparator<node> { public int compare(node a, node b) { int freqa = a.freq; int freqb = b.freq; return freqa-freqb; } }
the problem storing in codetochar
character 0
values unusable codes, codetochar.containskey(temp)
condition in decompress
method returns true
codes.
if added && codetochar.get(temp)!=0
condition work fine.
the method be:
public static string decompress(string s) { string temp = new string(); string result = new string(); for(int = 0; < s.length(); i++) { temp = temp + s.charat(i); character c = codetochar.get(temp); if(c!=null && c!=0) { result = result + c; temp = ""; } } return result; }
tested text "lorem ipsum" compressed "1010111011111011000101101100011110000" , decompressed fine.
Comments
Post a Comment