Skip to content

Commit 347d5b5

Browse files
committed
adding huffman_coding.cpp
1 parent 0422a7f commit 347d5b5

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,15 @@ Include contains single header implementation of data structures and some algori
178178
| calculate the shortest distance from the start position (Node S) to all of the other nodes in the graph using Dijkstra algorithm. | [dijkstra-shortest-reach.cpp](graph_problems/dijkstra-shortest-reach.cpp)|
179179
| Calculate total weight of Minimum Spanning Tree of a given graph ( sum of weights of edges which forms MST) using Prim's algorithm | [primsMST.cpp](graph_problems/primsMST.cpp)|
180180
| Print Minimum Spanning Tree( MST ) of a given graph using Kruskal's algorithm.| [kruskalMST.cpp](graph_problems/kruskalMST.cpp)|
181+
| Create a program to generate a Huffman encoding for each character as a table.|[huffman_encoding.cpp] (greedy_problems/huffman_encoding.cpp)|
181182

182183
### Greedy Problems
183184
| Problem | Solution |
184185
| :------------ | :----------: |
185186
| Given two integer arrays, A and B, each containing N integers. You are free to permute the order of the elements in the arrays. Is there an permutation A', B' possible of A and B, such that, A'<sub>i</sub>+B'<sub>i</sub> ≥ K for all i, where A'<sub>i</sub> denotes the i<sup>th</sup> element in the array A' and B'<sub>i</sub> denotes i<sup>th</sup> element in the array B'.| [two_arrays.cpp](greedy_problems/two_arrays.cpp)|
186187
|John is taking orders. The i<sup>th</sup> order is placed by the i<sup>th</sup> customer at t<sub>i</sub> time and it takes d<sub>i</sub> time to procees. What is the order in which the customers will get their orders? (see more details in solutions's comments)|[orders_order.cpp](greedy_problems/orders_order.cpp)|
187188

189+
188190
### Leet code Problems
189191
| Problem | Solution |
190192
| :------------ | :----------: |

greedy_problems/huffman_coding.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Huffman Encoding:
3+
* Huffman coding compresses data by using fewer bits to encode more frequently
4+
* occurring characters so that not all characters are encoded with 8 bits.
5+
* It is a way to assign binary codes to symbols that reduces the overall number of bits
6+
* used to encode a typical string of those symbols.
7+
* For example, if you use letters as symbols and have details of the frequency of occurrence
8+
* of those letters in typical strings, then you could just encode each letter with a fixed number of bits,
9+
* such as in ASCII codes. You can do better than this by encoding more frequently occurring letters such as e and a,
10+
* with smaller bit strings; and less frequently occurring letters such as q and x with longer bit strings.
11+
* More details: https://www.cs.duke.edu/csed/poop/huff/info/
12+
*/
13+
14+
#include <iostream>
15+
#include <queue>
16+
#include <vector>
17+
18+
struct HeapNode
19+
{
20+
char data;
21+
unsigned int frequency;
22+
HeapNode *left;
23+
HeapNode *right;
24+
25+
HeapNode(char d, unsigned int fr)
26+
: data {d},
27+
frequency {fr},
28+
left {nullptr},
29+
right {nullptr}
30+
{}
31+
};
32+
33+
// We need a comparator for comparing two nodes.
34+
struct compare
35+
{
36+
bool operator() (HeapNode* node1, HeapNode* node2)
37+
{
38+
return (node1->frequency > node2->frequency);
39+
}
40+
};
41+
42+
void generateHuffmanCodeUtil(HeapNode* root, std::string str)
43+
{
44+
if (!root)
45+
return;
46+
47+
if (root->data != '$')
48+
{
49+
std::cout << root->data << " : " << str << std::endl;
50+
}
51+
52+
generateHuffmanCodeUtil(root->left, str + '0');
53+
generateHuffmanCodeUtil(root->right, str + '1');
54+
}
55+
56+
void generateHuffmanCode(std::string data, std::vector<int> frequencies)
57+
{
58+
HeapNode *left, *right, *top;
59+
60+
// Create a minimum heap based on frequency and inserts all characters in it.
61+
//
62+
std::priority_queue<HeapNode*, std::vector<HeapNode*>, compare> minHeap;
63+
unsigned int size = frequencies.size();
64+
for (unsigned int i = 0; i < size; ++i)
65+
{
66+
minHeap.push(new HeapNode(data[i], frequencies[i]));
67+
}
68+
69+
while (minHeap.size() != 1)
70+
{
71+
// Get two minimum nodes from heap.
72+
left = minHeap.top();
73+
minHeap.pop();
74+
75+
right = minHeap.top();
76+
minHeap.pop();
77+
78+
// Combine the above two nodes (their frequecies) to generate a
79+
// new node.Make these two nodes as left and right children of this new node.
80+
// Add this node to the min heap, also assign in '$' as data. (character for internal nodes)
81+
//
82+
top = new HeapNode('$', left->frequency + right->frequency);
83+
top->left = left;
84+
top->right = right;
85+
minHeap.push(top);
86+
}
87+
88+
// We are done generating the tree, lets print it.
89+
//
90+
generateHuffmanCodeUtil(minHeap.top(), "");
91+
}
92+
93+
int main()
94+
{
95+
std::string data{"abcdef"};
96+
std::vector<int> frequencies = { 5, 9, 12, 13, 16, 45 };
97+
98+
generateHuffmanCode(data, frequencies);
99+
return 0;
100+
}

0 commit comments

Comments
 (0)