- Calculate the frequency of all the characters which are present in the given string or file.
- Build a Min-Heap based on frequency of the table(using arrayList of Nodes)
- Extract Minimum Value node from minHeap = Node L
- Extract another Min. from minHeap = Node R
- Now, Node P = L + R; and add this new Node P to MinHeap again.
- Repeat from 3 to 5 recursively until we get the Root Node of the tree.
- Assign 0 to the left child and 1 to the right child.
- Assign each character at the leaf of the tree and Fetch the huffman code as shown in below diagram.
X : Characters present in given string. p(X) : Normalized frequency of all the characters; so that Sum of All p(X) = 1.
Image Source: http://www.data-compression.com/lossless.html