public
anonymous / gist:4409070
Created

  • Download Gist
gistfile1.c
C
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
#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];
 
 
}
 
 
 

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.