I am trying to find the length of code generated on Huffman encoding for finding Huffman compression efficiency:
My code for Huffman encoding is:
Function call is GenerateCode(rear, "",Obj);
These Obj is used for internal purpose(you may feel it un-necessary but i am using it to have the data in current context).
public void GenerateCode(Node parentNode, string code, MainPage obj)
{
obj.listBox2.Visibility = Visibility.Visible;
if (parentNode != null)
{
GenerateCode(parentNode.left, code + "0",obj);
if (parentNode.left == null && parentNode.right == null)
obj.listBox2.Items.Add("Symbol :" + parentNode.symbol + " - " + "Code : " + code);
GenerateCode(parentNode.right, code + "1",obj);
}
}
And suppose Huffman Table obtained is this :
symbol : 0 Code : 1
symbol : 1 Code : 00
symbol : 2 Code : 011
symbol : 3 Code : 010
Now i am not able to find the logic in c# to calculate the length on these encoding corresponding to each symbols so that i would be able to multiply them with frequency to calculate the Huffman compression efficiency. Because they are string of "0 and 1" so how to calculate the length from them ?