Question

I am trying to create a Huffman tree, I already have a sorted array of frequencies in c language. Here is my structure :

struct node
{
    int value;
    char letter;                 /* symbol */
    struct node *left,*right;    /* left and right subtrees */
};
typedef struct node Node;

and inside main() i have :

int main(){
    Node *tree;
    FILE *input, *output; //file input and output i am taking because i will take a input text file containing encoding of all 27 alphabets like a= 00001 b= 00010 etc. 

    buildHuffmanTree(&tree); // see it's function call there i already have done sorting of frequencies using qsort() BUT I DON'T KNOW WHAT TO DO AFTER.
    return 0;
}

see here :

void buildHuffmanTree(Node **tree){
    Node *temp;
    Node *array[27];
    int i, subTrees = 27;
    int smallOne;

    for (i=0;i<27;i++)
    {
        array[i] = malloc(sizeof(Node));
        array[i]->value = englishLetterFrequencies[i]; //this englishLetterFrequencies[27] contains the the frequencies of all 27 alphabtets like ={81,27,1,12.....up to 27 index of this array}
        array[i]->letter = i;
        array[i]->left = NULL;
        array[i]->right = NULL;
    }
 //here is the sorting part:
 int i = 0; int d,p;
    printf("the array frequency is \n");
    for(d=0;d < 27;d++)
    printf("%d  ",array[d]->value);
    // sorting of arrays
    qsort(array,27,sizeof(*array),cmpfunc);
    //////////////////////////
    printf("\n the sorted array frequency is \n");
        for(p=0;p < 27;p++)
    printf("%d  ",array[p]->value); //So now this array[p]->value contains all the sorted frequency.
//I DON'T KNOW WHAT TO DO NOW
return;
}

Now with sorted arrays , What i have in mind is.. First i will take the first two node (that are in first and second index of my increasing order sorted array[]) and then add them and sort again and form a tree using it. But i don't know hoe to do that. I am a beginer . any one could explain how to implement it ?

Était-ce utile?

La solution

Malloc a new node. Take the two lowest frequency nodes and assign them to left and right of the new node, and put the sum of their frequencies in value of the new node. Delete the two nodes from the array by moving other elements down. Now insert the new node in the array after elements less than value and before elements greater than value, by moving the greater elements up one. The array now has one less element.

Repeat until there is one element in the array. That is the root of the tree.

Autres conseils

I'm learning HuffmanTree recently,for example:you have a array of frequencies,which is 7,9,2,6,3,after sorted,it becomes 2,3,6,7,9.I can't ad picture for my low scole... you always pick first two elements of array,so 2 and 3,

  5
 / \
2   3

you can get this and add 5 into your array and delete 2 and 3.so array now is 5,6,7,9 next step is pick 5 and 6,so you can get this:

   11
   /\
  5  6
 / \
2   3

so delete 5 and 6,and ad 11 into array,which now is 7,9,11 pick 7 and 9,you can get this:

  16
 /  \
7    9

delete 7 and 9,and add 16 into array,which now is 11,16 pick 11 and 16,you can get this:

      27
     / \
    11  16
   /\   /\
  5  6 7  9         
 /\
2  3
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top