我想創建一個Huffman樹,我已經有了一個用c語言編排的頻率數組。 這裏是我的結構:如何在c中創建一個huffman樹(已經有一個排序數組)
struct node
{
int value;
char letter; /* symbol */
struct node *left,*right; /* left and right subtrees */
};
typedef struct node Node;
和裏面的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;
}
在這裏看到:
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;
}
與有序陣列,我心目中是什麼
現在。 。首先,我將採用前兩個節點(位於增序排序數組[]的第一個和第二個索引中),然後添加它們並重新排序,並使用它形成樹。但我不知道鋤頭要這樣做。我是一個開始。任何人都可以解釋如何實現它?
停止大喊。這不會給你更多的答案。 – user2357112
反正這是要求(不叫喊),謝謝你的回覆 – user3085082
他意味着你不應該使用像這樣的所有帽子。相反,編輯您的問題看起來像這樣。 –