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