Calculate the frequency of each unique character in the input data.
Consider each character as a leaf node in a binary tree that you will construct.
Repeat the following steps until all nodes are added to the rooted binary tree:
Choose two nodes with the smallest frequencies to create a new node.
Combine the two nodes by assigning them as the left and right branches of the new node.
Include the nodes just combined
Set the frequency of the new node as the sum of the frequencies of its component nodes.
Assign ‘0‘ symbol to the left branch of the new node and ‘1‘ symbol to the right branch of the new node.
Generate the code word for each character by traversing the binary tree from
the root to the leaf node of the corresponding character and recording the sequence
of ‘0‘s and ‘1‘s along the way