Skip to content

anonymous /gist:4409070

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree.h"
#include "utility.h"
/*check to make sure that the correct number of inputs is put in*/
int CheckArgCount(int argc){
if (argc != 3){
printf("Not enough arguments.\n Use: ipa2_5 <filename> <output_filename>\n");
return FALSE;
}
return TRUE;
}
/*open the file and make sure it opens correctly*/
FILE* OpenFile(char FileName[], char op[]){
FILE* fptr;
fptr = fopen(FileName, op);
if(fptr == NULL){
printf("Error opening file!\n");
return NULL;
}
return fptr;
}
void construct_tree(FILE *fptr,LEAF **huffman_tree){
int freq_array [255];
int value_array [255];
int test;
/*set all values in the array's to 0*/
memset(freq_array, 0, sizeof (freq_array));
memset(value_array, 0, sizeof (value_array));
/*read the file and create 2 arrays. the first of what character is located at what location and the second for the frequency of each character located at its ascii value within the array*/
fseek(fptr,0, SEEK_SET);
while ((test = fgetc(fptr))!= EOF){
if (test!= '\n'){
value_array[test] = test;
freq_array[test] += 1;
}
}
/*condence the array*/
int i;
int j =0;
int length = 0;
int org_array[255][2];
memset(org_array, 0, sizeof org_array);
for(i=0;i<255;i++){
if (value_array[i] != 0){
org_array[j][0]= value_array[i];
org_array[j][1]= freq_array[i];
length = j;
j++;
}
}
int compressed_array[length][2];
for (i=0;i<=length;i++){
compressed_array[i][0] = org_array[i][0];
compressed_array[i][1] = org_array[i][1];
}
int a;
int b;
for (j=0;j<=length;j++){
for (i=0;i<=length;i++){
if ((compressed_array[i][1] > compressed_array[i+1][1])&&(compressed_array[i+1][1]!= 0)){
a = compressed_array[i][0];
b =compressed_array[i][1];
compressed_array[i][0]=compressed_array[i+1][0];
compressed_array[i][1]=compressed_array[i+1][1];
compressed_array[i+1][0] = a;
compressed_array[i+1][1] = b;
}
}
}
/*THIS IS THE PART I NEED HELP ON!!!!!*/
/*create a node for each value within the array*/
LEAF *array[length] ;
LEAF *temp =NULL;
for(i = 0; i <=length+1; i++){
array[i] = malloc(sizeof(LEAF));
(array[i] -> value) = compressed_array[i][0];
(array[i] -> freq) = compressed_array[i][1];
(array[i] -> lchild) = NULL;
(array[i] -> rchild) = NULL;
}
/*combine the nodes in the correct order until all but one node remains*/
int max = length+1;
int fake = -1;
int location = 0;
while (max>=1){
if (array[location]->freq != 0){
temp = array[location];
array[location]->freq = temp->freq + array[location+1]->freq;
array[location]->value = 127;
array[location]->lchild = array[location+1];
array[location]->rchild = temp;
array[location+1]->value = 0;
array[location+1]->freq =0;
}
location++;
LEAF *temp2;
for (i=0;i<=length+1;i++){
if ((array[i]->freq) >= (array[i+1]->freq)&& (array[i]->value == 127)){
temp2 = array[i];
array[i]=array[i+1];
array[i+1] = temp2;
}
}
max = max + fake;
}
*huffman_tree = array[length+1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.