SarthakKeshari / CPP-Questions-and-Solutions

This repository aims to solve and create new problems from different spheres of coding. A path to help students to get access to solutions and discuss their doubts.
MIT License
47 stars 132 forks source link

Added Huffman_coding_for_data_compression #417

Closed Parul1923 closed 2 years ago

Parul1923 commented 2 years ago

Issue Id you have worked upon -

309

Briefly explain your program logic -

1) Create a leaf node of each unique char and store it in set (i.e in the form of pair of integer and node). 2) Extract two nodes with minimum frequency from set (i.e First two elements from set). 3) Create a new node equal to sum of frequency of two nodes. Make the first extracted node as left child and second extracted node as right child. Add this node to the set. 4) Repeat step number 2 and 3 until the set contains only one element. Remaining node is the root node of Huffman Tree. 5) Now start from the root node and traverse the tree left and right. Left edge mark with 0 and right edge mark with 1. Print the char along with the code for each and every leaf node.

Screenshots(Attach 2 screenshots of your own input and output) -

Input: Screenshot (8) Output: Screenshot (10)


Checklist:

Eg - If your code follow the below guidelines. Kindly change [] to [x]

All the conditions should be fulfilled for considering your code for merging -