Professional Documents
Culture Documents
Huffman Coding in C#: Ferhat
Huffman Coding in C#: Ferhat
Huffman coding is an encoding algorithm used for data compression. So called Huffman trees
are used for encoding and decoding of data. The leaves of these trees contain the symbols
and their frequencies. Each parent node contains the sum of the frequencies of its children.
1. Create a list of nodes where each node holds a symbol and its frequency. Sort the list
according to frequency.
Ordered list of Huffman nodes for the input string "foo bar"
Page 1 of 7
2. Take the first two nodes in the list and create a new node (also known as the parent node)
which holds the sum of the frequencies of those nodes and the nodes as its children.
3. Add the new node to the list of nodes and remove the two nodes you’ve used to create the
parent node.
4. Repeat these steps until the list contains one node. This is the root of the Huffman tree.
Node Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HuffmanTest
{
public class Node
{
public char Symbol { get; set; }
public int Frequency { get; set; }
public Node Right { get; set; }
public Node Left { get; set; }
if (Left != null)
Page 2 of 7
{
List<bool> leftPath = new List<bool>();
leftPath.AddRange(data);
leftPath.Add(false);
if (Right != null)
{
List<bool> rightPath = new List<bool>();
rightPath.AddRange(data);
rightPath.Add(true);
right = Right.Traverse(symbol, rightPath);
}
if (left != null)
{
return left;
}
else
{
return right;
}
}
}
}
}
Page 3 of 7
HuffmanTree Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace HuffmanTest
{
public class HuffmanTree
{
private List<Node> nodes = new List<Node>();
public Node Root { get; set; }
public Dictionary<char, int> Frequencies = new Dictionary<char, int>();
Frequencies[source[i]]++;
}
if (orderedNodes.Count >= 2)
{
// Take first two items
Page 4 of 7
List<Node> taken = orderedNodes.Take(2).ToList<Node>();
nodes.Remove(taken[0]);
nodes.Remove(taken[1]);
nodes.Add(parent);
}
this.Root = nodes.FirstOrDefault();
return bits;
}
if (IsLeaf(current))
{
decoded += current.Symbol;
current = this.Root;
}
}
return decoded;
}
}
}
Page 6 of 7
Main Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace HuffmanTest
{
class Program
{
static void Main(string[] args)
{
string input = "foo bar";
HuffmanTree huffmanTree = new HuffmanTree();
// Encode
BitArray encoded = huffmanTree.Encode(input);
Console.Write("Encoded: ");
foreach (bool bit in encoded)
{
Console.Write((bit ? 1 : 0) + "");
}
Console.WriteLine();
// Decode
string decoded = huffmanTree.Decode(encoded);
Console.ReadLine();
}
}
}
Page 7 of 7