Skip to content

Commit 81460c5

Browse files
Merge pull request #570 from balasbk/master
Add files via upload
2 parents fd9aeb4 + 8d08834 commit 81460c5

File tree

1 file changed

+283
-0
lines changed

1 file changed

+283
-0
lines changed

C++/huffman coding.cpp

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
2+
#include <iostream>
3+
#include <cstdlib>
4+
using namespace std;
5+
6+
7+
#define MAX_TREE_HT 100
8+
9+
10+
struct MinHeapNode {
11+
12+
13+
char data;
14+
15+
16+
unsigned freq;
17+
18+
19+
struct MinHeapNode *left, *right;
20+
};
21+
22+
23+
struct MinHeap {
24+
25+
unsigned size;
26+
27+
unsigned capacity;
28+
29+
30+
struct MinHeapNode** array;
31+
};
32+
33+
struct MinHeapNode* newNode(char data, unsigned freq)
34+
{
35+
struct MinHeapNode* temp
36+
= (struct MinHeapNode*)malloc
37+
(sizeof(struct MinHeapNode));
38+
39+
temp->left = temp->right = NULL;
40+
temp->data = data;
41+
temp->freq = freq;
42+
43+
return temp;
44+
}
45+
46+
47+
struct MinHeap* createMinHeap(unsigned capacity)
48+
49+
{
50+
51+
struct MinHeap* minHeap
52+
= (struct MinHeap*)malloc(sizeof(struct MinHeap));
53+
54+
// current size is 0
55+
minHeap->size = 0;
56+
57+
minHeap->capacity = capacity;
58+
59+
minHeap->array
60+
= (struct MinHeapNode**)malloc(minHeap->
61+
capacity * sizeof(struct MinHeapNode*));
62+
return minHeap;
63+
}
64+
65+
// A utility function to
66+
// swap two min heap nodes
67+
void swapMinHeapNode(struct MinHeapNode** a,
68+
struct MinHeapNode** b)
69+
70+
{
71+
72+
struct MinHeapNode* t = *a;
73+
*a = *b;
74+
*b = t;
75+
}
76+
77+
// The standard minHeapify function.
78+
void minHeapify(struct MinHeap* minHeap, int idx)
79+
80+
{
81+
82+
int smallest = idx;
83+
int left = 2 * idx + 1;
84+
int right = 2 * idx + 2;
85+
86+
if (left < minHeap->size && minHeap->array[left]->
87+
freq < minHeap->array[smallest]->freq)
88+
smallest = left;
89+
90+
if (right < minHeap->size && minHeap->array[right]->
91+
freq < minHeap->array[smallest]->freq)
92+
smallest = right;
93+
94+
if (smallest != idx) {
95+
swapMinHeapNode(&minHeap->array[smallest],
96+
&minHeap->array[idx]);
97+
minHeapify(minHeap, smallest);
98+
}
99+
}
100+
101+
// A utility function to check
102+
// if size of heap is 1 or not
103+
int isSizeOne(struct MinHeap* minHeap)
104+
{
105+
106+
return (minHeap->size == 1);
107+
}
108+
109+
// A standard function to extract
110+
// minimum value node from heap
111+
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
112+
113+
{
114+
115+
struct MinHeapNode* temp = minHeap->array[0];
116+
minHeap->array[0]
117+
= minHeap->array[minHeap->size - 1];
118+
119+
--minHeap->size;
120+
minHeapify(minHeap, 0);
121+
122+
return temp;
123+
}
124+
125+
// A utility function to insert
126+
// a new node to Min Heap
127+
void insertMinHeap(struct MinHeap* minHeap,
128+
struct MinHeapNode* minHeapNode)
129+
130+
{
131+
132+
++minHeap->size;
133+
int i = minHeap->size - 1;
134+
135+
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
136+
137+
minHeap->array[i] = minHeap->array[(i - 1) / 2];
138+
i = (i - 1) / 2;
139+
}
140+
141+
minHeap->array[i] = minHeapNode;
142+
}
143+
144+
// A standard function to build min heap
145+
void buildMinHeap(struct MinHeap* minHeap)
146+
147+
{
148+
149+
int n = minHeap->size - 1;
150+
int i;
151+
152+
for (i = (n - 1) / 2; i >= 0; --i)
153+
minHeapify(minHeap, i);
154+
}
155+
156+
// A utility function to print an array of size n
157+
void printArr(int arr[], int n)
158+
{
159+
int i;
160+
for (i = 0; i < n; ++i)
161+
cout<< arr[i];
162+
163+
cout<<"\n";
164+
}
165+
166+
// Utility function to check if this node is leaf
167+
int isLeaf(struct MinHeapNode* root)
168+
169+
{
170+
171+
return !(root->left) && !(root->right);
172+
}
173+
174+
175+
struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size)
176+
177+
{
178+
179+
struct MinHeap* minHeap = createMinHeap(size);
180+
181+
for (int i = 0; i < size; ++i)
182+
minHeap->array[i] = newNode(data[i], freq[i]);
183+
184+
minHeap->size = size;
185+
buildMinHeap(minHeap);
186+
187+
return minHeap;
188+
}
189+
190+
// The main function that builds Huffman tree
191+
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size)
192+
193+
{
194+
struct MinHeapNode *left, *right, *top;
195+
196+
// Step 1: Create a min heap of capacity
197+
// equal to size. Initially, there are
198+
// modes equal to size.
199+
struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);
200+
201+
// Iterate while size of heap doesn't become 1
202+
while (!isSizeOne(minHeap)) {
203+
204+
// Step 2: Extract the two minimum
205+
// freq items from min heap
206+
left = extractMin(minHeap);
207+
right = extractMin(minHeap);
208+
209+
210+
top = newNode('$', left->freq + right->freq);
211+
212+
top->left = left;
213+
top->right = right;
214+
215+
insertMinHeap(minHeap, top);
216+
}
217+
218+
// Step 4: The remaining node is the
219+
// root node and the tree is complete.
220+
return extractMin(minHeap);
221+
}
222+
223+
// Prints huffman codes from the root of Huffman Tree.
224+
// It uses arr[] to store codes
225+
void printCodes(struct MinHeapNode* root, int arr[], int top)
226+
227+
{
228+
229+
// Assign 0 to left edge and recur
230+
if (root->left) {
231+
232+
arr[top] = 0;
233+
printCodes(root->left, arr, top + 1);
234+
}
235+
236+
// Assign 1 to right edge and recur
237+
if (root->right) {
238+
239+
arr[top] = 1;
240+
printCodes(root->right, arr, top + 1);
241+
}
242+
243+
// If this is a leaf node, then
244+
// it contains one of the input
245+
// characters, print the character
246+
// and its code from arr[]
247+
if (isLeaf(root)) {
248+
249+
cout<< root->data <<": ";
250+
printArr(arr, top);
251+
}
252+
}
253+
254+
// The main function that builds a
255+
// Huffman Tree and print codes by traversing
256+
// the built Huffman Tree
257+
void HuffmanCodes(char data[], int freq[], int size)
258+
259+
{
260+
// Construct Huffman Tree
261+
struct MinHeapNode* root
262+
= buildHuffmanTree(data, freq, size);
263+
264+
// Print Huffman codes using
265+
// the Huffman tree built above
266+
int arr[MAX_TREE_HT], top = 0;
267+
268+
printCodes(root, arr, top);
269+
}
270+
271+
// Driver program to test above functions
272+
int main()
273+
{
274+
275+
char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
276+
int freq[] = { 5, 9, 12, 13, 16, 45 };
277+
278+
int size = sizeof(arr) / sizeof(arr[0]);
279+
280+
HuffmanCodes(arr, freq, size);
281+
282+
return 0;
283+
}

0 commit comments

Comments
 (0)