Обновить sk1/compressor.c
This commit is contained in:
		
							parent
							
								
									94646354cf
								
							
						
					
					
						commit
						e1b2fa79ae
					
				| @ -45,9 +45,9 @@ void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b) { | |||||||
| 
 | 
 | ||||||
| // MinHeapify function
 | // MinHeapify function
 | ||||||
| void minHeapify(struct MinHeap* minHeap, int idx) { | void minHeapify(struct MinHeap* minHeap, int idx) { | ||||||
|     int smallest = idx; |     unsigned smallest = idx;  // Преобразуем в unsigned
 | ||||||
|     int left = 2 * idx + 1; |     unsigned left = 2 * idx + 1;  // Преобразуем в unsigned
 | ||||||
|     int right = 2 * idx + 2; |     unsigned right = 2 * idx + 2;  // Преобразуем в unsigned
 | ||||||
| 
 | 
 | ||||||
|     if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq) |     if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq) | ||||||
|         smallest = left; |         smallest = left; | ||||||
| @ -154,6 +154,25 @@ void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int t | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Updated compressFile function
 | // Updated compressFile function
 | ||||||
|  | void storeCodes(struct MinHeapNode* root, char** codes, char* currentCode, int top) { | ||||||
|  |     if (!root) return; | ||||||
|  | 
 | ||||||
|  |     if (root->left) { | ||||||
|  |         currentCode[top] = '0'; | ||||||
|  |         storeCodes(root->left, codes, currentCode, top + 1); | ||||||
|  |     } | ||||||
|  |     if (root->right) { | ||||||
|  |         currentCode[top] = '1'; | ||||||
|  |         storeCodes(root->right, codes, currentCode, top + 1); | ||||||
|  |     } | ||||||
|  |     if (!(root->left) && !(root->right)) { | ||||||
|  |         currentCode[top] = '\0'; | ||||||
|  |         codes[root->data] = (char*)malloc(strlen(currentCode) + 1);  // Use malloc instead of strdup
 | ||||||
|  |         strcpy(codes[root->data], currentCode); // Copy the string
 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Исправление для minHeap в compressFile
 | ||||||
| int compressFile(const char* input_file_name, const char* output_file_name) { | int compressFile(const char* input_file_name, const char* output_file_name) { | ||||||
|     FILE* inputFile = fopen(input_file_name, "rb"); |     FILE* inputFile = fopen(input_file_name, "rb"); | ||||||
|     if (!inputFile) { |     if (!inputFile) { | ||||||
| @ -180,6 +199,7 @@ int compressFile(const char* input_file_name, const char* output_file_name) { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     struct MinHeap* minHeap = createAndBuildMinHeap(data, frequencies, size);  // Создаем кучу
 | ||||||
|     struct MinHeapNode* root = buildHuffmanTree(data, frequencies, size); |     struct MinHeapNode* root = buildHuffmanTree(data, frequencies, size); | ||||||
|     char* codes[256] = {0}; |     char* codes[256] = {0}; | ||||||
|     char currentCode[MAX_TREE_HT] = {0}; // Initialize to avoid garbage values
 |     char currentCode[MAX_TREE_HT] = {0}; // Initialize to avoid garbage values
 | ||||||
| @ -223,12 +243,8 @@ int compressFile(const char* input_file_name, const char* output_file_name) { | |||||||
|     fclose(inputFile); |     fclose(inputFile); | ||||||
|     fclose(outputFile); |     fclose(outputFile); | ||||||
| 
 | 
 | ||||||
|     // Free memory for codes
 |     freeMinHeap(minHeap);  // Free the min heap memory
 | ||||||
|     for (int i = 0; i < 256; i++) { |     freeHuffmanCodes(codes);  // Free Huffman codes
 | ||||||
|         if (codes[i]) free(codes[i]); |  | ||||||
|     } |  | ||||||
|     freeHuffmanCodes(codes);  // Освободите память для кодов
 |  | ||||||
|     freeMinHeap(minHeap);  // Освободите память для кучи
 |  | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user