91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 4KB
    文件類型: .cpp
    金幣: 1
    下載: 0 次
    發布日期: 2021-06-02
  • 語言: C/C++
  • 標簽:

資源簡介

霍夫曼編碼,對輸入的字符集和各個字符對應的權值,例如A={a,b,c,d,e,f,g,h},各個字符對應的權值為{5,29,7,8,14,23,3,11},求出每個字符的霍夫曼編碼。 【輸入形式】 輸入若干個字符(1 <= n <= 26),其權值為int型。 輸入數據的第一行的整數n,表示字符數;接下來的n行是字符集,一行一個字符;最后一行是各字符的權值,以空格分隔。 【輸出形式】 每個字符(節點)的霍夫曼編碼。參見樣例輸出。 【樣例輸入】 4 a b c d 1 3 7 22 【樣例輸出】 a:000 b:001 c:01   d:1 【樣例說明】 提示: 1、將最小兩個子樹合并過程中一定要從前向后去查找兩個最小子樹,最小子樹作為新結點的左子樹,次小子樹作為新結點的右子樹,編碼過程中左子樹定義為0,右子樹定義為1 2、另外:一般原則要求:  若有重復權值結點,原來森林中的結點優先選擇(即深度小的結點優先,以確保最終總樹深較淺并相對平衡)。新生成的權值和的結點后用。

資源截圖

代碼片段和文件信息

/*輸入格式
字符數量
n:4
輸入字符:abcd“注意要連著輸入”
輸入權值:1?3?7?22
*/
#include?
#include?
#include?
#include?
#include?
typedef?char?key_type;
/*用于赫夫曼樹的結構體*/
?
using?namespace?std;
?
typedef?struct?Node
{
int?weight;
key_type?key;
struct?Node?*lchild?*rchild;
}HFMNode*HFMTree;
/*用于鏈表的結構體*/
typedef?struct?link_node
{
HFMTree?data;
struct?link_node?*next;
}LNode*link;
/*對鏈表的操作*/
void?init_link(link?*head);//初始化鏈表
void?insert_link(link?head?HFMTree?hfm);//向鏈表中插入一個元素,并按照權重排序
int?delete_link(link?headHFMTree?*hfm);//依次刪除鏈表中的數據,成功返回1,失敗返回0
/*創建赫夫曼樹str為關鍵字,w為對應的權重*/
int?creat_hfmTree(HFMTree?*rootchar?str[]int?w[]);
/*獲取赫夫曼編碼表,存儲在數組code中*/
void?hfmTree_code(HFMTree?head?int?achar?code[]);
/*譯碼譯碼結果存儲在decode數組中code輸入的報文*/
int?hfmTree_decode(HFMTree?headconst?char?code[]char?decode[]);
int?main()
{
int?bi;
HFMTree?root;
char?str[1000000];
scanf(“%d“&b);
int?*w=new?int[b];
scanf(“%s“str);
for(i=0;i scanf(“%d“&w[i]);
}
creat_hfmTree(&root?str?w);

char?code[1024]?=?{?0?};//用來存放編碼表
hfmTree_code(root?1code);
cout?< return?0;
}
?
void?init_link(link?*head)
{
(*head)?=?(link)malloc(sizeof(LNode));
(*head)->next?=?NULL;
(*head)->data?=?NULL;
}
?
void?insert_link(link?head?HFMTree?hfm)
{
assert(head?!=?NULL);
/*先構造鏈表節點*/
link?temp?=?(link)malloc(sizeof(LNode));
temp->next?=?NULL;
temp->data?=?hfm;
?
while?(head->next?!=?NULL?&&?head->next->data->weight?weight)
{
head?=?head->next;
}
if?(head->next?==?NULL)
{
head->next?=?temp;
}
else
{
temp->next?=?head->next;
head->next?=?temp;
}
}
?
int?delete_link(link?head?HFMTree?*hfm)
{
assert(head?!=?NULL);
if?(head->next?==?NULL)
return?0;
link?temp?=?head->next;
head->next?=?temp->next;
*hfm?=?temp->data;
free(temp);
return?1;
}
?
int

評論

共有 條評論