0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx

上传人:b****6 文档编号:20682053 上传时间:2023-01-25 格式:DOCX 页数:18 大小:203.57KB
下载 相关 举报
0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx_第1页
第1页 / 共18页
0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx_第2页
第2页 / 共18页
0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx_第3页
第3页 / 共18页
0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx_第4页
第4页 / 共18页
0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx

《0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx(18页珍藏版)》请在冰豆网上搜索。

0023算法笔记贪心算法哈夫曼编码问题Word文档下载推荐.docx

2.#include 

"

stdafx.h"

3.#include 

BinaryTree.h"

4.#include 

MinHeap.h"

5.#include 

<

iostream>

6.using 

namespace 

std;

7. 

8.const 

int 

6;

9. 

10.template<

class 

Type>

Huffman;

11. 

12.template<

13.BinaryTree<

int>

HuffmanTree(Type 

f[],int 

n);

14. 

15.template<

16.class 

Huffman 

17.{ 

18. 

friend 

BinaryTree<

HuffmanTree(Type[],int);

19. 

public:

20. 

operator 

Type() 

const 

21. 

22. 

return 

weight;

23. 

24. 

//private:

25. 

tree;

26. 

Type 

27.};

28. 

29.int 

main() 

30.{ 

31. 

char 

c[] 

{'

0'

'

a'

b'

c'

d'

e'

f'

};

32. 

f[] 

{0,45,13,12,16,9,5};

//下标从1开始 

33. 

HuffmanTree(f,N);

34. 

35. 

cout<

各字符出现的对应频率分别为:

endl;

36. 

for(int 

i=1;

i<

=N;

i++) 

37. 

38. 

c[i]<

:

f[i]<

;

39. 

40. 

41. 

42. 

生成二叉树的前序遍历结果为:

43. 

t.Pre_Order();

44. 

45. 

46. 

生成二叉树的中序遍历结果为:

47. 

t.In_Order();

48. 

49. 

50. 

t.DestroyTree();

51. 

0;

52.} 

53. 

54.template<

55.BinaryTree<

n) 

56.{ 

57. 

//生成单节点树 

58. 

Huffman<

*w 

new 

[n+1];

59. 

z,zero;

60. 

61. 

=n;

62. 

63. 

z.MakeTree(i,zero,zero);

64. 

w[i].weight 

f[i];

65. 

w[i].tree 

z;

66. 

67. 

68. 

//建优先队列 

69. 

MinHeap<

>

Q(n);

70. 

Q.Insert(w[i]);

71. 

72. 

//反复合并最小频率树 

73. 

x,y;

74. 

n;

75. 

76. 

Q.RemoveMin();

77. 

78. 

z.MakeTree(0,x.tree,y.tree);

79. 

x.weight 

+= 

y.weight;

80. 

x.tree 

81. 

Q.Insert(x);

82. 

83. 

84. 

85. 

86. 

delete[] 

w;

87. 

88. 

x.tree;

89.} 

(2)BinaryTree.h二叉树实现

1.#include<

2.using 

3. 

4.template<

T>

5.struct 

BTNode 

6.{ 

data;

8. 

BTNode<

*lChild,*rChild;

10. 

BTNode() 

12. 

lChild=rChild=NULL;

13. 

15. 

BTNode(const 

&

val,BTNode<

*Childl=NULL,BTNode<

*Childr=NULL) 

16. 

17. 

data=val;

lChild=Childl;

rChild=Childr;

CopyTree() 

*nl,*nr,*nn;

if(&

data==NULL) 

27. 

NULL;

29. 

nl=lChild->

CopyTree();

30. 

nr=rChild->

nn=new 

(data,nl,nr);

nn;

35.};

38.template<

39.class 

BinaryTree 

40.{ 

*root;

BinaryTree();

~BinaryTree();

void 

Pre_Order();

In_Order();

Post_Order();

TreeHeight()const;

TreeNodeCount()const;

52. 

DestroyTree();

54. 

MakeTree(T 

pData,BinaryTree<

leftTree,BinaryTree<

rightTree);

55. 

Change(BTNode<

*r);

56. 

private:

Destroy(BTNode<

*&

r);

PreOrder(BTNode<

InOrder(BTNode<

PostOrder(BTNode<

Height(const 

*r)const;

NodeCount(const 

65.};

67.template<

68.BinaryTree<

BinaryTree() 

69.{ 

root=NULL;

71.} 

73.template<

74.BinaryTree<

~BinaryTree() 

75.{ 

77.} 

79.template<

80.void 

Pre_Order() 

81.{ 

PreOrder(root);

83.} 

85.template<

86.void 

In_Order() 

87.{ 

InOrder(root);

90. 

91.template<

92.void 

Post_Order() 

93.{ 

94. 

PostOrder(root);

95.} 

96. 

97.template<

98.int 

TreeHeight()const 

99.{ 

100. 

Height(root);

101.} 

102. 

103.template<

104.int 

TreeNodeCount()const 

105.{ 

106. 

NodeCount(root);

107.} 

108. 

109.template<

110.void 

DestroyTree() 

111.{ 

112. 

Destroy(root);

113.} 

114. 

115.template<

116.void 

*r) 

117.{ 

118. 

if(r!

=NULL) 

119. 

120. 

r->

data<

'

121. 

PreOrder(r->

lChild);

122. 

rChild);

123. 

124.} 

125. 

126.template<

127.void 

128.{ 

129. 

130. 

131. 

InOrder(r->

132. 

133. 

134. 

135.} 

136. 

137.template<

138.void 

139.{ 

140. 

141. 

142. 

PostOrder(r->

143. 

144. 

145. 

146.} 

147. 

148.template<

149.int 

*r)const 

150.{ 

151. 

if(r==NULL) 

152. 

153. 

else 

154. 

1+NodeCount(r->

lChild)+NodeCount(r->

155.} 

156. 

157.template<

158.int 

159.{ 

160. 

161. 

162. 

163. 

164. 

lh,rh;

165. 

lh=Height(r->

166. 

rh=Height(r->

167. 

1+(lh>

rh?

lh:

rh);

168. 

169.} 

170. 

171.template<

172.void 

r) 

173.{ 

174. 

175. 

176. 

Destroy(r->

177. 

178. 

delete 

r;

179. 

r=NULL;

180. 

181.} 

182. 

183.template<

184.void 

*r)//将二叉树bt所有结点的左右子树交换 

185.{ 

186. 

*p;

187. 

if(r){ 

188. 

p=r->

lChild;

189. 

lChild=r->

rChild;

190. 

rChild=p;

//左右子女交换 

191. 

Change(r->

//交换左子树上所有结点的左右子树 

192. 

//交换右子树上所有结点的左右子树 

193. 

194.} 

195. 

196.template<

197.void 

rightTree) 

198.{ 

199. 

root 

();

200. 

root->

data 

pData;

201. 

lChild 

leftTree.root;

202. 

rChild 

rightTree.root;

203.} 

(3)MinHeap.h最小堆实现

1.#include 

3.template<

4.class 

MinHeap 

5.{ 

6. 

*heap;

//元素数组,0号位置也储存元素 

CurrentSize;

//目前元素个数 

MaxSize;

//可容纳的最多元素个数 

FilterDown(const 

start,const 

end);

//自上往下调整,使关键字小的节点在上 

FilterUp(int 

start);

//自下往上调整 

MinHeap(int 

n=1000);

~MinHeap();

bool 

Insert(const 

x);

//插入元素 

RemoveMin();

//删除最小元素 

GetMin();

//取最小元素 

IsEmpty() 

const;

IsFull() 

Clear();

25.};

27.template<

28.MinHeap<

29.{ 

MaxSize=n;

heap=new 

T[MaxSize];

CurrentSize=0;

33.} 

35.template<

36.MinHeap<

~MinHeap() 

37.{ 

[]heap;

39.} 

41.template<

42.void 

start) 

43.{ 

j=start,i=(j-1)/2;

//i指向j的双亲节点 

temp=heap[j];

while(j>

0) 

if(heap[i]<

=temp) 

break;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 其它课程

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1