生活随笔
收集整理的這篇文章主要介紹了
哈夫曼二叉树的构建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
1 哈夫曼編碼
哈夫曼(Huffman)編碼算法是基于二叉樹構建編碼壓縮結構的,它是數據壓縮中經典的一種算法。算法根據文本字符出現的頻率,重新對字符進行編碼。
哈夫曼編碼舉例: 假設要對“we will we will r u”進行壓縮。
2 哈夫曼二叉樹的構建
編碼實現:
Huff.h:
#pragma once#include <stdio.h>
#include <assert.h>
#include <Windows.h>
#include <iostream>
#include <iomanip>using namespace std
;#define MaxSize 1024 typedef struct _Bnode
{char value
;int weight
;struct _Bnode
*parent
;struct _Bnode
*lchild
;struct _Bnode
*rchild
;
} Btree
, Bnode
; typedef Bnode
*DataType
; typedef struct _QNode
{ int priority
;
取第一個節點DataType data
;struct _QNode
*next
;
}QNode
;typedef QNode
* QueuePtr
;typedef struct Queue
{int length
; QueuePtr front
; QueuePtr rear
;
}LinkQueue
;
void InitQueue(LinkQueue
*LQ
)
{if(!LQ
) return ;LQ
->length
= 0;LQ
->front
= LQ
->rear
= NULL;
}
int IsEmpty(LinkQueue
*LQ
)
{if(!LQ
) return 0;if (LQ
->front
== NULL){return 1;}return 0;
}
int IsFull(LinkQueue
*LQ
)
{if(!LQ
) return 0;if (LQ
->length
== MaxSize
){return 1;}return 0;
}
int EnterQueue( LinkQueue
*LQ
,DataType data
, int priority
){if(!LQ
) return 0;if(IsFull(LQ
)){cout
<<"無法插入元素 "<<data
<<", 隊列已滿!"<<endl
;return 0;}QNode
*qNode
= new QNode
;qNode
->data
= data
;qNode
->priority
= priority
;qNode
->next
= NULL;if(IsEmpty(LQ
)){LQ
->front
= LQ
->rear
= qNode
;}else {qNode
->next
= LQ
->front
;LQ
->front
= qNode
;}LQ
->length
++;return 1;
}
int PopQueue(LinkQueue
*LQ
, DataType
*data
){QNode
**prev
= NULL, *prev_node
=NULL;QNode
*last
= NULL, *tmp
= NULL;if(!LQ
|| IsEmpty(LQ
)){cout
<<"隊列為空!"<<endl
;return 0;}if(!data
) return 0;prev
= &(LQ
->front
);last
= LQ
->front
;tmp
= last
->next
;while(tmp
){if(tmp
->priority
<(*prev
)->priority
){prev
= &(last
->next
);prev_node
= last
;}last
=tmp
;tmp
=tmp
->next
;}*data
= (*prev
)->data
;tmp
= *prev
;*prev
= (*prev
)->next
;delete tmp
;LQ
->length
--;if(LQ
->length
==0){LQ
->rear
=NULL;}if(prev_node
&&prev_node
->next
==NULL){LQ
->rear
=prev_node
;}return 1;
}
void PrintQueue(LinkQueue
*LQ
)
{ QueuePtr tmp
;if(!LQ
) return ;if(LQ
->front
==NULL){cout
<<"隊列為空!";return ;}tmp
= LQ
->front
;while(tmp
){cout
<<setw(4)<<tmp
->data
->value
<<"["<<tmp
->priority
<<"]";tmp
= tmp
->next
;}cout
<<endl
;
}
int GetHead(LinkQueue
*LQ
,DataType
*data
)
{if (!LQ
|| IsEmpty(LQ
)){cout
<<"隊列為空!"<<endl
;return 0;}if(!data
) return 0;*data
= LQ
->front
->data
;return 1;
}
void ClearQueue(LinkQueue
*LQ
)
{if(!LQ
) return ;while(LQ
->front
){QueuePtr tmp
= LQ
->front
->next
;delete LQ
->front
;LQ
->front
= tmp
;}LQ
->front
= LQ
->rear
= NULL;LQ
->length
= 0;
}
int getLength(LinkQueue
* LQ
){if(!LQ
) return 0;return LQ
->length
;
}
哈夫曼編碼實現.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "Huff.h"using namespace std
;
void PreOrderRec(Btree
*root
);
void HuffmanTree
(Btree
* &huff
, int n
)
{LinkQueue
*LQ
= new LinkQueue
;int i
= 0;InitQueue(LQ
);for (i
=0; i
<n
; i
++){Bnode
* node
= new Bnode
;cout
<<"請輸入第"<<i
+1<<"個字符和出現頻率: "<<endl
;cin
>>node
->value
; cin
>>node
->weight
;node
->parent
=NULL;node
->lchild
=NULL;node
->rchild
=NULL;EnterQueue(LQ
, node
, node
->weight
);}PrintQueue(LQ
);do{Bnode
*node1
= NULL;Bnode
*node2
= NULL;if(!IsEmpty(LQ
)){PopQueue(LQ
, &node1
);printf("第一個出隊列的數:%c, 優先級: %d\n", node1
->value
,node1
->weight
);}else {break;}if(!IsEmpty(LQ
)){Bnode
*node3
= new Bnode
;PopQueue(LQ
, &node2
);printf("第二個出隊列的數:%c, 優先級: %d\n", node2
->value
,node2
->weight
);node3
->lchild
= node1
;node1
->parent
= node3
;node3
->rchild
= node2
;node2
->parent
= node3
;node3
->value
= ' ';node3
->weight
=node1
->weight
+node2
->weight
;printf("合并進隊列的數:%c, 優先級: %d\n", node3
->value
,node3
->weight
);EnterQueue(LQ
,node3
, node3
->weight
);}else {huff
= node1
;break;}}while(1);
}
void PreOrderRec(Btree
*root
)
{if (root
== NULL){return;}printf("- %c -", root
->value
);PreOrderRec(root
->lchild
);PreOrderRec(root
->rchild
);
}int main(void){Btree
* tree
= NULL;HuffmanTree(tree
, 7);PreOrderRec(tree
);system("pause");return 0;
}
參考資料:
C/C++從入門到精通-高級程序員之路【奇牛學院】
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的哈夫曼二叉树的构建的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。