C语言 Huffman树编码的问题

我已经读入文本,然后定义了一个指针栈,把所有的结点都按频率存入栈中,不知道现在如何利用现有的数组,来构建一个Huffman树?请大神指点。
部分代码如下,还没写完。

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tnode{
    char word;
    int count;
    int flag;
    struct tnode *left;
    struct tnode *right;
};
struct tnode *p,*q;
struct tnode str[128];
struct tnode *arr[128];
int curr=0;
int temp;
int main()
{
    FILE *IN,*OUT;
    IN=fopen("input.txt","r");
    char c;
    int i,num,j;
    int temp;
    while((c=fgetc(IN))!=EOF){
        str[(int)c].word=c;
        str[(int)c].count++;
    }
    str[10].count=0;
    /*for(i=0;i<128;i++){
        printf("%d  %c  %d\n",i,str[i].word,str[i].count);
    }*/
    for(i=0;i<128;i++){
        if(str[i].count!=0){
            p=(struct tnode*)malloc(sizeof(struct tnode));
            p->count=str[i].count;
            p->word=str[i].word;
            p->left=p->right=NULL;
            arr[curr++]=p;
        }
    }
    for(i=0;i<curr;i++){
        for(j=i+1;j<curr;j++){
            if((arr[j]->count<arr[i]->count)||((arr[j]->count==arr[i]->count)&&(arr[j]->word<arr[i]->word))){
                q=arr[i];
                arr[i]=arr[j];
                arr[j]=q;
            }
        }
    }
    /*for(i=0;i<curr;i++){
        printf("%2d  %c  %d\n",i,arr[i]->word,arr[i]->count);
    }*/
    fseek(IN,0,SEEK_SET);
    while(c=fgetc(IN)!=EOF){
        if(c=='\n'){
            continue;
        }

    }
    return 0;
}

没明白你的意思,题目的意思就是为了构建霍夫曼树?