1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #include <stdio.h> #include <stdlib.h> #include <string.h>
#define MAX_CHARACTERS 256
struct Node { char data; int frequency; struct Node* left, * right; };
struct MinTree { int size; int capacity; struct Node** array; };
struct Node* newNode(char data, int frequency) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->left = node->right = NULL; node->data = data; node->frequency = frequency; return node; }
struct MinTree* createMinTree(int capacity) { struct MinTree* minTree = (struct MinTree*)malloc(sizeof(struct MinTree)); minTree->size = 0; minTree->capacity = capacity; minTree->array = (struct Node**)malloc(capacity * sizeof(struct Node*)); return minTree; }
void swapNode(struct Node** a, struct Node** b) { struct Node* t = *a; *a = *b; *b = t; }
void minTreeify(struct MinTree* minTree, int idx) { int smallest = idx; int left = 2 * idx + 1; int right = 2 * idx + 2; if (left < minTree->size && minTree->array[left]->frequency < minTree->array[smallest]->frequency) smallest = left; if (right < minTree->size && minTree->array[right]->frequency < minTree->array[smallest]->frequency) smallest = right; if (smallest != idx) { swapNode(&minTree->array[smallest], &minTree->array[idx]); minTreeify(minTree, smallest); } }
int isSizeOne(struct MinTree* minTree) { return (minTree->size == 1); }
struct Node* extractMin(struct MinTree* minTree) { struct Node* temp = minTree->array[0]; minTree->array[0] = minTree->array[minTree->size - 1]; --minTree->size; minTreeify(minTree, 0); return temp; }
void insertMinTree(struct MinTree* minTree, struct Node* node) { ++minTree->size; int i = minTree->size - 1; while (i && node->frequency < minTree->array[(i - 1) / 2]->frequency) { minTree->array[i] = minTree->array[(i - 1) / 2]; i = (i - 1) / 2; } minTree->array[i] = node; }
struct Node* buildHuffmanTree(char data[], int frequency[], int size) { struct Node* left, * right, * top; struct MinTree* minTree = createMinTree(size);
for (int i = 0; i < size; ++i) { if (frequency[i] > 0) { minTree->array[minTree->size++] = newNode(data[i], frequency[i]); } }
for (int i = minTree->size / 2 - 1; i >= 0; --i) { minTreeify(minTree, i); }
while (!isSizeOne(minTree)) { left = extractMin(minTree); right = extractMin(minTree); top = newNode('$', left->frequency + right->frequency); top->left = left; top->right = right; insertMinTree(minTree, top); }
return extractMin(minTree); }
void printCodes(struct Node* root, int arr[], int top) { static int first = 1; if (!(root->left) && !(root->right)) { printf("Character: %c, Weight: %u, Huffman Code: ", root->data, root->frequency); for (int i = 0; i < top; ++i) { printf("%d", arr[i]); } printf("\n"); if (first) { first = 0; return; } } if (root->left) { arr[top] = 0; printCodes(root->left, arr, top + 1); } if (root->right) { arr[top] = 1; printCodes(root->right, arr, top + 1); } }
int main() { char characters[MAX_CHARACTERS]; int frequencies[MAX_CHARACTERS] = { 0 }; int n;
printf("Enter the number of characters: "); scanf("%d", &n);
printf("Enter %d characters: ", n); for (int i = 0; i < n; ++i) { scanf(" %c", &characters[i]); frequencies[(int)characters[i]]++; }
int L_chars = 0; for (int i = 0; i < MAX_CHARACTERS; ++i) { if (frequencies[i] > 0) { L_chars++; } }
char L_characters[100]; int L_frequencies[100]; int index = 0;
for (int i = 0; i < MAX_CHARACTERS; ++i) { if (frequencies[i] > 0) { L_characters[index] = (char)i; L_frequencies[index] = frequencies[i]; index++; } }
struct Node* root = buildHuffmanTree(L_characters, L_frequencies, L_chars);
int arr[MAX_CHARACTERS], top_index = 0; printCodes(root, arr, top_index);
return 0; }
|