# Huffman coding | Greedy Algorithm

## Steps for the Huffman coding scheme are mentioned below:

1. Steps for creating Huffman tree algorithm with given input characters and frequencies
• Step 1- Take two characters with the lowest frequency i.e., newline (\n) and S, and create two leaf nodes for \n and S.
• Step 2- Their sum of frequencies will be stored in a parent node. So, 4 (sum of \n frequency 1 and S char 3) will be stored in the parent node.
• Step 3- Taking the next lowest frequency, which is T with frequency 4 will get added beneath and the next parent node will again be the sum of T frequency (4) and the previous parent node (4), making 8.
• Step 4- Repeat step 3, until we get the highest parent node value compared to other frequencies.
• Step 5- After the parent node with value 18, create a new subtree following the step-1 and step-2.
• Step 7- Its frequency will again add to its next parent node.
• Fill the value of left traversal with value 0 and right traversal with value 1.
• Traverse down through the character from the main node.

## Advantages of Huffman Coding

• It saves a lot of storage space
• Generates shorter binary codes for encoding
• Codes generated are prefix-free

## Disadvantages of Huffman Coding

• The lossless data encoding technique, therefore, is suitable for only encoding text and programming files.
• A considerably slower process.
• Differences in binary length codes make it difficult for decoding software to detect whether the data is corrupted or not.

--

--

--

## More from Shruti Sinha

Software Engineering Student

Love podcasts or audiobooks? Learn on the go with our new app.

## Shruti Sinha

Software Engineering Student