Java saving huffman encoded text to file
I'm working on a huffman compression and decompression application in java. So far I got the encoding and decoding working. It converts a big input text to it's encoded binary text. This is a String of 1's and 0's. For example:
String originaltext = "Hello I am trying to program a huffman application and..."
String encodedtext = "1100001110001111011010100110100110...." It's a pretty long string.
Now I want to save the string to a file as binary file to reduce the size. But when I try do this, the size will be way bigger then the original text size. Instead I need the size smaller then the original file size. After saving the encodedtext to a file I need to read the binary file back in and convert it to the encodedText string to deconvert it with my huffmantree method.
How can I save the binary string to a binary file which size is then smaller then the original size? And how do read the file in and convert the binary code to the encodedString text?
1 answer

Probably you're writing your string of 1 and 0 as a string, which will result in 1 byte per 1 or 0.
You need to convert those 1 and 0 to bytes (i.e. convert groups of eight 1 or 0 into 1 byte and write those bytes.
See also questions close to this topic

How can I automate a project developed in Swift?
How can I automate project developed in Swift?

Vaadin Window resize and component inside auto alignment
I have a class that extends Window as follows. I am only giving Window related code. Window Component code is removed to keep the code and question clear. I got a window looks like in the picture. When I resize the window, I was expecting that the components inside the window will also be resized. I guess there is some API call I am missing to maintain the component ration increased together with the window. Or do I need to do it by myself programmatically? Please let me know how it can be done?
public class VisibleColumnPanel extends Window implements Window.ResizeListener{ public VisibleColumnPanel(){ center(); setResizable(true); setModal(true); setImmediate(true); //setSizeFull(); addListener(this); this.setWidth(720, Unit.PIXELS); this.setHeight(600, Unit.PIXELS); } @Override public void windowResized(ResizeEvent resizeEvent) { System.out.println("hit here now"); } }
Normal window without maximizing it:
So is there any way that the component inside the window all also expend or maintain their ration together with the window?
I could programmatically do it if I could handle maximize button click event. I implemented implements Window.ResizeListener. windowResized event only fired when I try to resize the window by dragging the mouse. Is there any way to get maximize/restore button click event?

Hash a whole object without converting to byte[] first
I want to get sha2 hash of particular java object. I don't want it to be
int
, I wantbyte[]
or at leastString
. I've got the following code to create sha2:static byte[] sha2(byte[] message) { if (message == null  message.length == 0) { throw new IllegalArgumentException("message is null"); } try { MessageDigest sha256 = MessageDigest.getInstance(SHA_256); return sha256.digest(message); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); } }
I can just convert my object to
byte[]
, but I don't think it's a good idea to store big array in memory just to create 32 byte array. So how can I compute sha2(or maybe another crypto hash function) of object? 
N base Huffman code using matlab
I want to implement Huffman coding without using any libraries or functions and my program has to take the following parameters:
the pmf of an arbitrary random variable X
an integer D >= 2,
I have the first part but I am having difficulties implementing the second. Here is the code that I have. D means whether the tree is binary ternary and so on.
clear all probabilities = [0.285, 0.238, 0.190, 0.142, 0.095, 0.047]; % Normalise the probabilities probabilities = probabilities/sum(probabilities); % For each probability... for index = 1:length(probabilities) % ...create an empty codeword codewords{index} = []; % Create a set containing only this codeword set_contents{index} = index; % Store the probability associated with this set set_probabilities(index) = probabilities(index); end % Keep going until all the sets have been merged into one while length(set_contents) > 1 % Determine which sets have the lowest total probabilities [temp, sorted_indices] = sort(set_probabilities); % Get the set having the lowest probability zero_set = set_contents{sorted_indices(1)}; % Get that probability zero_probability = set_probabilities(sorted_indices(1)); % For each codeword in the set... for codeword_index = 1:length(zero_set) % ...append a zero codewords{zero_set(codeword_index)} = [codewords{zero_set(codeword_index)}, 0]; end % Get the set having the second lowest probability one_set = set_contents{sorted_indices(2)}; % Get that probability one_probability = set_probabilities(sorted_indices(2)); % For each codeword in the set... for codeword_index = 1:length(one_set) % ...append a one codewords{one_set(codeword_index)} = [codewords{one_set(codeword_index)}, 1]; end % Remove the two sets having the lowest probabilities... set_contents(sorted_indices(1:2)) = []; % ...and merge them into a new set set_contents{length(set_contents)+1} = [zero_set, one_set]; % Remove the two lowest probabilities... set_probabilities(sorted_indices(1:2)) = []; % ...and give their sum to the new set set_probabilities(length(set_probabilities)+1) = zero_probability + one_probability; end disp('The symbols, their probabilities and the allocated Huffman codewords are:'); for index = 1:length(codewords) % ...display its bits in reverse order disp([num2str(index), ' ', num2str(probabilities(index)),' ',num2str(codewords{index}(length(codewords{index}):1:1))]); end

Huffman coding in matlab
I have this assignment where I have to implement Huffman coding without using any libraries or functions and my program has to take the following parameters:
the pmf of an arbitrary random variable X
an integer D >= 2,
I have the first part but I am having difficulties implementing the second. Here is the code that I have. D means whether the tree is binary ternary and so on.
clear all probabilities = [0.285, 0.238, 0.190, 0.142, 0.095, 0.047]; % Normalise the probabilities probabilities = probabilities/sum(probabilities); % For each probability... for index = 1:length(probabilities) % ...create an empty codeword codewords{index} = []; % Create a set containing only this codeword set_contents{index} = index; % Store the probability associated with this set set_probabilities(index) = probabilities(index); end % Keep going until all the sets have been merged into one while length(set_contents) > 1 % Determine which sets have the lowest total probabilities [temp, sorted_indices] = sort(set_probabilities); % Get the set having the lowest probability zero_set = set_contents{sorted_indices(1)}; % Get that probability zero_probability = set_probabilities(sorted_indices(1)); % For each codeword in the set... for codeword_index = 1:length(zero_set) % ...append a zero codewords{zero_set(codeword_index)} = [codewords{zero_set(codeword_index)}, 0]; end % Get the set having the second lowest probability one_set = set_contents{sorted_indices(2)}; % Get that probability one_probability = set_probabilities(sorted_indices(2)); % For each codeword in the set... for codeword_index = 1:length(one_set) % ...append a one codewords{one_set(codeword_index)} = [codewords{one_set(codeword_index)}, 1]; end % Remove the two sets having the lowest probabilities... set_contents(sorted_indices(1:2)) = []; % ...and merge them into a new set set_contents{length(set_contents)+1} = [zero_set, one_set]; % Remove the two lowest probabilities... set_probabilities(sorted_indices(1:2)) = []; % ...and give their sum to the new set set_probabilities(length(set_probabilities)+1) = zero_probability + one_probability; end disp('The symbols, their probabilities and the allocated Huffman codewords are:'); for index = 1:length(codewords) % ...display its bits in reverse order disp([num2str(index), ' ', num2str(probabilities(index)),' ',num2str(codewords{index}(length(codewords{index}):1:1))]); end

Can't locate Segmentation error in a C++ program implementing the Huffman table
I've tried to implement a Huffman table here. I understand how it works and I don't really see anything wrong with the approach I used and there aren't any syntax errors. However, I seem to be getting a segmentation fault 11 error, and I'm not sure where exactly.
I've added comments to every section to indicate what happens where. Any form of assistance, as to which portion of the code I need to overlook would be really appreciated.
On trying to backtrace with a debugger, I got an error saying:
solution std::map with 4198576 elements error reading variable: Cannot access memory at address 0x18
On further inspection, the error seems to be in the while statement while(x>data != var.first). Not sure how to fix it.
#include <iostream> #include <queue> #include <string> #include <vector> #include <map> using namespace std; class HuffmanNode{ public: char data; int count; HuffmanNode *left; HuffmanNode *right; }; //Compare function for priority queue class compare_function{ public: bool operator()(const HuffmanNode* one, const HuffmanNode* two){ return one>count> two>count; } }; /* Struct used for the final map which uses ascii value as keys and contains */ struct information{ int freq; string code; }; int main(){ //Helps build the huffman table priority_queue<HuffmanNode*, vector<HuffmanNode*>,compare_function> huffman_table; /*Use manual input replace with file input later Helps get the frequency count */ map<char, int> frequency; string text = "What is your name"; for(char c: text) frequency[c]++; /*Put the informaton from the map to the priority queue */ for(auto var: frequency){ HuffmanNode* node= new HuffmanNode; node>data=var.first; node>count=var.second; node>left=nullptr; node>right= nullptr; huffman_table.push(node); } //Building a tree HuffmanNode* combined= nullptr; // Declared outside as it can be used to combine with the last node in the priority queue while(huffman_table.size() >= 2){ HuffmanNode * one = huffman_table.top(); // First minimum node huffman_table.pop(); // Popped out of the queue HuffmanNode*two = huffman_table.top(); //Second minimum node huffman_table.pop(); //Popped out of the queue //Combining the two nodes and re inserting to the table combined= new HuffmanNode(); combined>data= '\0'; combined>count= one>count+two>count; combined>left = one; combined>right = two; //Reinset back to the table huffman_table.push(combined); } /*The root of the table is the combination of the compared node and the last node in the priority queue*/ HuffmanNode* last = huffman_table.top(); HuffmanNode* root= new HuffmanNode; root>data ='\0'; root>count=combined>count+ last>count; root>left= combined; root>right = last; //Map containing final solution map<char, information> solution; information i; //traverse the tree in search of the node and compute code HuffmanNode* x = root; string temp; for(auto var: frequency){ while(x>data != var.first) { if(x==nullptr) x=root; else if (x>count < var.second){ temp=temp+"0"; x=x>left; } else if(x>count > var.second){ temp= temp+"1"; x=x>right; } } i.code=temp; i.freq=var.second; solution[var.first]=i; x=root; temp=""; } //Prints answer here for(auto var: solution){ cout<<var.first<<" "<<var.second.freq<<" "<<var.second.code; } }