




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、0023算法筆記【貪心算法】哈夫曼編碼問題 1、問題描述 哈夫曼編碼是廣泛地用于數(shù)據(jù)文件壓縮的十分有效的編碼方法。其壓縮率通常在20%90%之間。哈夫曼編碼算法用字符在文件中出現(xiàn)的頻率表來建立一個用0,1串表示各字符的最優(yōu)表示方式。一個包含100,000個字符的文件,各字符出現(xiàn)頻率不同,如下表所示。 有多種方式表示文件中的信息,若用0,1碼表示字符的方法,即每個字符用唯一的一個0,1串表示。若采用定長編碼表示,則需要3位表示一個字符,整個文件編碼需要300,000位;若采用變長編碼表示
2、,給頻率高的字符較短的編碼;頻率低的字符較長的編碼,達(dá)到整體編碼減少的目的,則整個文件編碼需要(45×1+13×3+12×3+16×3+9×4+5×4)×1000=224,000位,由此可見,變長碼比定長碼方案好,總碼長減小約25%。 前綴碼:對每一個字符規(guī)定一個0,1串作為其代碼,并要求任一字符的代碼都不是其他字符代碼的前綴。這種編碼稱為前綴碼。編碼的前綴性質(zhì)可以使譯碼方法非常簡單;例如001011101可以唯一的分解為0,0,101,1101,因而其譯碼為aabe。
3、160; 譯碼過程需要方便的取出編碼的前綴,因此需要表示前綴碼的合適的數(shù)據(jù)結(jié)構(gòu)。為此,可以用二叉樹作為前綴碼的數(shù)據(jù)結(jié)構(gòu):樹葉表示給定字符;從樹根到樹葉的路徑當(dāng)作該字符的前綴碼;代碼中每一位的0或1分別作為指示某節(jié)點到左兒子或右兒子的“路標(biāo)”。 從上圖可以看出,表示最優(yōu)前綴碼的二叉樹總是一棵完全二叉樹,即樹中任意節(jié)點都有2個兒子。圖a表示定長編碼方案不是最優(yōu)的,其編碼的二叉樹不是一棵完全二叉樹。在一般情況下,若C是編碼字符集,表示其最優(yōu)前綴碼的二叉樹中恰有|C|個葉子。每個葉子對應(yīng)于字符集中的一個字符,該二叉樹有|C|-1個內(nèi)部節(jié)點。
4、 給定編碼字符集C及頻率分布f,即C中任一字符c以頻率f(c)在數(shù)據(jù)文件中出現(xiàn)。C的一個前綴碼編碼方案對應(yīng)于一棵二叉樹T。字符c在樹T中的深度記為dT(c)。dT(c)也是字符c的前綴碼長。則平均碼長定義為:使平均碼長達(dá)到最小的前綴碼編碼方案稱為C的最優(yōu)前綴碼。 2、構(gòu)造哈弗曼編碼 哈夫曼提出構(gòu)造最優(yōu)前綴碼的貪心算法,由此產(chǎn)生的編碼方案稱為哈夫曼編碼。其構(gòu)造步驟如下: (1)哈夫曼算法以自底向上的方式構(gòu)造表
5、示最優(yōu)前綴碼的二叉樹T。 (2)算法以|C|個葉結(jié)點開始,執(zhí)行|C|1次的“合并”運算后產(chǎn)生最終所要求的樹T。 (3)假設(shè)編碼字符集中每一字符c的頻率是f(c)。以f為鍵值的優(yōu)先隊列Q用在貪心選擇時有效地確定算法當(dāng)前要合并的2棵具有最小頻率的樹。一旦2棵具有最小頻率的樹合并后,產(chǎn)生一棵新的樹,其頻率為合并的2棵樹的頻率之和,并將新樹插入優(yōu)先隊列Q。經(jīng)過n1次的合并后,優(yōu)先隊列中只剩下一棵樹,即所要求的樹T。 構(gòu)造過程如圖所示: 具體代碼實現(xiàn)如
6、下: (1)4d4.cpp,程序主文件cpp view plain copy1. /4d4 貪心算法 哈夫曼算法 2. #include "stdafx.h" 3. #include "BinaryTree.h" 4. #include "MinHeap.h" 5. #include <iostream>
7、160; 6. using namespace std; 7. 8. const int N = 6; 9. 10. template<class Type> class Huffman; 11. 12. template<class Type> 13. BinaryTree<
8、;int> HuffmanTree(Type f,int n); 14. 15. template<class Type> 16. class Huffman 17. 18. friend BinaryTree<int> HuffmanTree(Type,int); 19.
9、160; public: 20. operator Type() const 21. 22. return weight;
10、;23. 24. /private: 25. BinaryTree<int> tree; 26. Type weight; 27. ;
11、0; 28. 29. int main() 30. 31. char c = '0','a','b','c','d','e','f' 32. int f = 0,45,13,12,16,9,5;/下標(biāo)從1開始
12、 33. BinaryTree<int> t = HuffmanTree(f,N); 34. 35. cout<<"各字符出現(xiàn)的對應(yīng)頻率分別為:"<<endl; 36. for(int i=1; i<=N; i+) 37.
13、; 38. cout<<ci<<":"<<fi<<" " 39. 40. cout<<endl; 41. 42.
14、; cout<<"生成二叉樹的前序遍歷結(jié)果為:"<<endl; 43. t.Pre_Order(); 44. cout<<endl; 45. 46. cout<<"生成二叉樹的中序遍歷結(jié)果為:"<<endl; 47.
15、160; t.In_Order(); 48. cout<<endl; 49. 50. t.DestroyTree(); 51. return 0; 52. 53. 54. template<class Type>
16、60;55. BinaryTree<int> HuffmanTree(Type f,int n) 56. 57. /生成單節(jié)點樹 58. Huffman<Type> *w = new Huffman<Type>n+1; 59. BinaryTree<in
17、t> z,zero; 60. 61. for(int i=1; i<=n; i+) 62. 63. z.MakeTree(i,zero,zero); 64.
18、 wi.weight = fi; 65. wi.tree = z; 66. 67. 68. /建優(yōu)先隊列 69. MinHeap<Huffman<Type>> Q
19、(n); 70. for(int i=1; i<=n; i+) Q.Insert(wi); 71. 72. /反復(fù)合并最小頻率樹 73. Huffman<Type> x,y; 74. for(int i=1;
20、;i<n; i+) 75. 76. x = Q.RemoveMin(); 77. y = Q.RemoveMin(); 78. &
21、#160;z.MakeTree(0,x.tree,y.tree); 79. x.weight += y.weight; 80. x.tree = z; 81. Q.Insert(x);
22、;82. 83. 84. x = Q.RemoveMin(); 85. 86. delete w; 87. 88. return x.tree; 89.
23、 (2)BinaryTree.h 二叉樹實現(xiàn)cpp view plain copy1. #include<iostream> 2. using namespace std; 3. 4. template<class T> 5. struct BTNode 6. 7. T data; 8.
24、 BTNode<T> *lChild,*rChild; 9. 10. BTNode() 11. 12. lChild=rChild=NULL; 13. 1
25、4. 15. BTNode(const T &val,BTNode<T> *Childl=NULL,BTNode<T> *Childr=NULL) 16. 17. data=val; 18.
26、160; lChild=Childl; 19. rChild=Childr; 20. 21. 22. BTNode<T>* CopyTree() 23. 24.
27、160; BTNode<T> *nl,*nr,*nn; 25. 26. if(&data=NULL) 27. return NULL; 28. 29.
28、0; nl=lChild->CopyTree(); 30. nr=rChild->CopyTree(); 31. 32. nn=new BTNode<T>(data,nl,nr); 33.
29、 return nn; 34. 35. ; 36. 37. 38. template<class T> 39. class BinaryTree 40. 41. public: 42.
30、 BTNode<T> *root; 43. BinaryTree(); 44. BinaryTree(); 45. 46.
31、60; void Pre_Order(); 47. void In_Order(); 48. void Post_Order(); 49. 50. int TreeHeig
32、ht()const; 51. int TreeNodeCount()const; 52. 53. void DestroyTree(); 54. void MakeTree(T pD
33、ata,BinaryTree<T> leftTree,BinaryTree<T> rightTree); 55. void Change(BTNode<T> *r); 56. 57. private: 58.
34、0; void Destroy(BTNode<T> *&r); 59. void PreOrder(BTNode<T> *r); 60. void InOrder(BTNode<T> *r); 61. &
35、#160; void PostOrder(BTNode<T> *r); 62. 63. int Height(const BTNode<T> *r)const; 64. int NodeCount(
36、const BTNode<T> *r)const; 65. ; 66. 67. template<class T> 68. BinaryTree<T>:BinaryTree() 69. 70. root=NULL; 71. 72. 73. template<class
37、60;T> 74. BinaryTree<T>:BinaryTree() 75. 76. 77. 78. 79. template<class T> 80. void BinaryTree<T>:Pre_Order() 81. 82.
38、;PreOrder(root); 83. 84. 85. template<class T> 86. void BinaryTree<T>:In_Order() 87. 88. InOrder(root); 89. 90. 91. template<class T>
39、 92. void BinaryTree<T>:Post_Order() 93. 94. PostOrder(root); 95. 96. 97. template<class T> 98. int BinaryTree<T>:TreeHeight()const 99. 100.
40、; return Height(root); 101. 102. 103. template<class T> 104. int BinaryTree<T>:TreeNodeCount()const 105. 106. return NodeCount(root); 107.
41、160;108. 109. template<class T> 110. void BinaryTree<T>:DestroyTree() 111. 112. Destroy(root); 113. 114. 115. template<class T> 116. void BinaryTr
42、ee<T>:PreOrder(BTNode<T> *r) 117. 118. if(r!=NULL) 119. 120. cout<<r->data<<' ' 121.
43、; PreOrder(r->lChild); 122. PreOrder(r->rChild); 123. 124. 125. 126. template<class T> 127. void BinaryTree&
44、lt;T>:InOrder(BTNode<T> *r) 128. 129. if(r!=NULL) 130. 131. InOrder(r->lChild); 132.
45、60;cout<<r->data<<' ' 133. InOrder(r->rChild); 134. 135. 136. 137. template<class T> 138. void BinaryTree<T&g
46、t;:PostOrder(BTNode<T> *r) 139. 140. if(r!=NULL) 141. 142. PostOrder(r->lChild); 143.
47、;PostOrder(r->rChild); 144. cout<<r->data<<' ' 145. 146. 147. 148. template<class T> 149. int BinaryTree<T>
48、;:NodeCount(const BTNode<T> *r)const 150. 151. if(r=NULL) 152. return 0; 153. else 154.
49、60; return 1+NodeCount(r->lChild)+NodeCount(r->rChild); 155. 156. 157. template<class T> 158. int BinaryTree<T>:Height(const BTNode<T> *r)const 159. 160.
50、;if(r=NULL) 161. return 0; 162. else 163. 164. int lh,rh; 165.
51、60; lh=Height(r->lChild); 166. rh=Height(r->rChild); 167. return 1+(lh>rh?lh:rh); 168. 169.
52、170. 171. template<class T> 172. void BinaryTree<T>:Destroy(BTNode<T> *&r) 173. 174. if(r!=NULL) 175. 176.
53、60; Destroy(r->lChild); 177. Destroy(r->rChild); 178. delete r; 179. r=NULL; 180.
54、160; 181. 182. 183. template<class T> 184. void BinaryTree<T>:Change(BTNode<T> *r)/將二叉樹bt所有結(jié)點的左右子樹交換 185. 186. BTNode<T> *p; 187.
55、0; if(r) 188. p=r->lChild; 189. r->lChild=r->rChild; 190. r->rChild=p; /左右子女交換 191.
56、 Change(r->lChild); /交換左子樹上所有結(jié)點的左右子樹 192. Change(r->rChild); /交換右子樹上所有結(jié)點的左右子樹 193. 194. 195. 19
57、6. template<class T> 197. void BinaryTree<T>:MakeTree(T pData,BinaryTree<T> leftTree,BinaryTree<T> rightTree) 198. 199. root = new BTNode<T>(); 200.
58、 root->data = pData; 201. root->lChild = leftTree.root; 202. root->rChild = rightTree.root; 203. (3)MinHeap.h 最小堆實現(xiàn)cpp view plain
59、60;copy1. #include <iostream> 2. using namespace std; 3. template<class T> 4. class MinHeap 5. 6. private: 7. T *heap;&
60、#160;/元素數(shù)組,0號位置也儲存元素 8. int CurrentSize; /目前元素個數(shù) 9. int MaxSize; /可容納的最多元素個數(shù) 10. 11. void
61、160;FilterDown(const int start,const int end); /自上往下調(diào)整,使關(guān)鍵字小的節(jié)點在上 12. void FilterUp(int start); /自下往上調(diào)整 13. 14. public: 15. &
62、#160; MinHeap(int n=1000); 16. MinHeap(); 17. bool Insert(const T &x); /插入元素 18. 19.
63、0; T RemoveMin(); /刪除最小元素 20. T GetMin(); /取最小元素 21. 22. bool IsEmpty() const; 23.
64、; bool IsFull() const; 24. void Clear(); 25. ; 26. 27. template<class T> 28. MinHeap<T>:MinHeap(int n) 29. 30.
65、160; MaxSize=n; 31. heap=new TMaxSize; 32. CurrentSize=0; 33. 34. 35. template<class T> 36. MinHeap<T>:MinHeap() 37. 38. &
66、#160; delete heap; 39. 40. 41. template<class T> 42. void MinHeap<T>:FilterUp(int start) /自下往上調(diào)整 43. 44. int j=start,i=(j-1)/2; /i指向j的雙親節(jié)點
67、; 45. T temp=heapj; 46. 47. while(j>0) 48. 49. if(heapi<=temp) 50.
68、0; break; 51. else 52. 53. heapj=heapi; 54.
69、160; j=i; 55. i=(i-1)/2; 56. 57. 58.
70、160;heapj=temp; 59. 60. 61. template<class T> 62. void MinHeap<T>:FilterDown(const int start,const int end) /自上往下調(diào)整,使關(guān)鍵字小的節(jié)點在上 63. 64. int i=start,j=2*i+1;
71、 65. T temp=heapi; 66. while(j<=end) 67. 68. if( (j<end) && (heapj>heapj+1) ) 69.
72、 j+; 70. if(temp<=heapj) 71. break; 72.
73、60; else 73. 74. heapi=heapj; 75. i=j; 76.
74、60; j=2*j+1; 77. 78. 79. heapi=temp; 80. 81. 82. template<class T> 83. bool MinHeap<T>:Insert(const T &x) 84. 85. if(CurrentSize=MaxSize) 8
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 高中提前考試數(shù)學(xué)試卷
- 福建省質(zhì)檢卷數(shù)學(xué)試卷
- 高一年泉州統(tǒng)考數(shù)學(xué)試卷
- 福州初中五月數(shù)學(xué)試卷
- 甘肅15高考數(shù)學(xué)試卷
- 教師課件的制作培訓(xùn)
- 2025年浙江醫(yī)療衛(wèi)生招聘金華義烏市中醫(yī)醫(yī)院招聘4人筆試歷年專業(yè)考點(難、易錯點)附帶答案詳解
- 2025年年唐都醫(yī)院住培醫(yī)師招錄筆試歷年專業(yè)考點(難、易錯點)附帶答案詳解
- 2025至2030城市建設(shè)規(guī)劃發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 二一年高考數(shù)學(xué)試卷
- 定向士官心理測試題及答案
- 2025至2030中國低溫氣體產(chǎn)品行業(yè)項目調(diào)研及市場前景預(yù)測評估報告
- e級籃球教練員理論考試試題及答案
- 高校教師資格證考試《高等教育心理學(xué)》真題及解析(2025年)
- T/CECS 10386-2024排水工程微型頂管用高性能硬聚氯乙烯管及連接件
- 店鋪轉(zhuǎn)讓合同協(xié)議書模板
- (王瑞元版本)運動生理學(xué)-課件-3-第三章-血液
- 濟南世創(chuàng)友聯(lián)有機硅科技有限公司年產(chǎn)1000 噸特種硅彈性體項目環(huán)評資料環(huán)境影響
- 2025中考(傳統(tǒng)文化)類滿分作文10篇
- 標(biāo)準(zhǔn)檢驗指導(dǎo)書(SIP)-鈑金
- 交易員心理培訓(xùn)課件
評論
0/150
提交評論