1、2. #includestdafx.h3. #includeBinaryTree.h4. #includeMinHeap.h5. #include6. usingnamespacestd;7. 8. constintN=6;9. 10. templateHuffman;11. 12. template13. BinaryTreeHuffmanTree(Typef,intn);14. 15. template16. classHuffman17. 18. friendBinaryTreeHuffmanTree(Type,int);19. public:20. operatorType()cons
2、t21. 22. returnweight;23. 24. /private:25. tree;26. Type27. ;28. 29. intmain()30. 31. charc0,abcdef;32. f0,45,13,12,16,9,5;/下标从1开始33. tHuffmanTree(f,N);34. 35. cout各字符出现的对应频率分别为:endl;36. for(inti=1;i=N;i+)37. 38. ci:fi;39. 40. 41. 42. 生成二叉树的前序遍历结果为:43. t.Pre_Order();44. 45. 46. 生成二叉树的中序遍历结果为:47. t.I
3、n_Order();48. 49. 50. t.DestroyTree();51. 0;52. 53. 54. template55. BinaryTreen)56. 57. /生成单节点树58. Huffman*wnewn+1;59. z,zero;60. 61. =n;62. 63. z.MakeTree(i,zero,zero);64. wi.weightfi;65. wi.treez;66. 67. 68. /建优先队列69. MinHeapQ(n);70. Q.Insert(wi);71. 72. /反复合并最小频率树73. x,y;74. n;75. 76. xQ.RemoveMi
4、n();77. y78. z.MakeTree(0,x.tree,y.tree);79. x.weight+=y.weight;80. x.tree81. Q.Insert(x);82. 83. 84. 85. 86. deletew;87. 88. x.tree;89. (2)BinaryTree.h 二叉树实现1. #include2. using3. 4. template5. structBTNode6. Tdata;8. BTNode*lChild,*rChild;10. BTNode()12. lChild=rChild=NULL;13. 15. BTNode(const&val,
5、BTNode*Childl=NULL,BTNodeCopyTree();30. nr=rChild-nn=new(data,nl,nr);nn;35. ;38. template39. classBinaryTree40. *root;BinaryTree();BinaryTree();voidPre_Order();In_Order();Post_Order();TreeHeight()const;TreeNodeCount()const;52. DestroyTree();54. MakeTree(TpData,BinaryTreeleftTree,BinaryTreerightTree)
6、;55. Change(BTNode*r);56. private:Destroy(BTNode*&r);PreOrder(BTNodeInOrder(BTNodePostOrder(BTNodeHeight(const*r)const;NodeCount(const65. ;67. template68. BinaryTreeBinaryTree()69. root=NULL;71. 73. template74. BinaryTreeBinaryTree()75. 77. 79. template80. voidPre_Order()81. PreOrder(root);83. 85. t
7、emplate86. voidIn_Order()87. InOrder(root);90. 91. template92. voidPost_Order()93. 94. PostOrder(root);95. 96. 97. template98. intTreeHeight()const99. 100. Height(root);101. 102. 103. template104. intTreeNodeCount()const105. 106. NodeCount(root);107. 108. 109. template110. voidDestroyTree()111. 112.
8、 Destroy(root);113. 114. 115. templatedatalChild);122. rChild);123. 124. 125. 126. template132. 133. 134. 135. 136. 137. template143. 144. 145. 146. 147. 148. templatelChild)+NodeCount(r-155. 156. 157. template166. rh=Height(r-167. 1+(lhrh?lh:rh);168. 169. 170. 171. template177. 178. deleter;179. r=
9、NULL;180. 181. 182. 183. templatelChild;189. lChild=r-rChild;190. rChild=p;/左右子女交换191. Change(r-/交换左子树上所有结点的左右子树192. /交换右子树上所有结点的左右子树193. 194. 195. 196. templatedatapData;201. lChildleftTree.root;202. rChildrightTree.root;203. (3)MinHeap.h 最小堆实现1. #include3. template4. classMinHeap5. 6. *heap;/元素数组,
10、0号位置也储存元素CurrentSize;/目前元素个数MaxSize;/可容纳的最多元素个数FilterDown(conststart,constend);/自上往下调整,使关键字小的节点在上FilterUp(intstart);/自下往上调整MinHeap(intn=1000);MinHeap();boolInsert(constx);/插入元素RemoveMin();/删除最小元素GetMin();/取最小元素IsEmpty()const;IsFull()Clear();25. ;27. template28. MinHeap29. MaxSize=n;heap=newTMaxSize;CurrentSize=0;33. 35. template36. MinHeapMinHeap()37. heap;39. 41. template0)if(heapi=temp)break;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1