Description:

  1. Calculate the frequency of each unique character in the input data.
  2. Consider each character as a leaf node in a binary tree that you will construct.
  3. Repeat the following steps until all nodes are added to the rooted binary tree:
    1. Choose two nodes with the smallest frequencies to create a new node.
    2. Combine the two nodes by assigning them as the left and right branches of the new node.
      • Include the nodes just combined
    3. Set the frequency of the new node as the sum of the frequencies of its component nodes.
    4. Assign ‘0‘ symbol to the left branch of the new node and ‘1‘ symbol to the right branch of the new node.
  4. 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