简体   繁体   English

如何在C中创建霍夫曼树(已具有排序数组)

[英]How to create a huffman tree in c (already have a sorted array)

I am trying to create a Huffman tree, I already have a sorted array of frequencies in c language. 我正在尝试创建一个霍夫曼树,我已经有一些用c语言编写的频率数组。 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 : 在main()中,我有:

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. 现在有了排序数组,我要记住的是..首先,我将采用前两个节点(在我的递增排序sortedarray []的第一个和第二个索引中),然后添加它们并再次排序,并使用它。 But i don't know hoe to do that. 但是我不知道该怎么做。 I am a beginer . 我是一个初学者。 any one could explain how to implement it ? 任何人都可以解释如何执行它吗?

Malloc a new node. M分配一个新节点。 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. 那是树的根。

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, 我最近正在学习HuffmanTree,例如:您有一个频率数组,分别是7,9,2,6,3,排序后,它变成了2,3,6,7,9。我无法投放图片为了我的卑鄙...您总是选择数组的前两个元素,所以2和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: 您可以将其添加到数组中并添加5并删除2和3.so数组现在是5,6,7,9下一步是选择5和6,因此您可以获取:

   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: 因此,删除5和6,并将广告11删除到数组中,现在是7,9,11,选择7和9,您可以得到:

  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: 删除7和9,然后将16添加到数组中,现在是11,16,选择11和16,您可以得到:

      27
     / \
    11  16
   /\   /\
  5  6 7  9         
 /\
2  3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM