您的位置:首页 > 理论基础 > 数据结构算法

数据结构问题---哈夫曼树与编码问题

2009-09-25 11:37 477 查看
-------------------------------------

典型例题14:数据结构问题---哈夫曼树与编码问题

-------------------------------------

1 #include <iostream>

2 #include <cstdlib>

3 using namespace std;

4

5 typedef int ElemType;

6

7 struct BTreeNode{

8 ElemType data;

9 BTreeNode* left;

10 BTreeNode* right;

11 };

12

13 void InitBTree(BTreeNode*& BT)

14 {

15 BT = NULL;

16 }

17

18 bool EmptyBTree(BTreeNode* BT)

19 {

20 return BT == NULL;

21 }

22

23 BTreeNode* CreateHuffman(ElemType a[],int n)

24 {

25 BTreeNode **b,*q;

26 b = new BTreeNode*
;

27 int i,j;

28 for (i = 0; i < n; ++i)

29 {

30 b[i] = new BTreeNode;

31 b[i]->data = a[i];

32 b[i]->left=b[i]->right =NULL;

33 }

34 //create the Huffman tree

35 for (i = 1; i <n ; ++i)

36 {

37 int k1 =-1,k2;

38 for (j = 0; j < n; ++j)

39 {

40 if( b[j]!=NULL && k1==-1 ){k1=j;continue;}

41 if(b[j]!=NULL){ k2=j; break;}

42 }

43 //find the minnum k1 and the second minnum k2;

44 for (j = k2; j <n ; ++j)

45 {

46 if(b[j]!=NULL)

47 {

48 if(b[j]->data < b[k1]->data){k2 = k1;k1 = j;}

49 else if(b[j]->data < b[k2]->data) k2 =j;

50 }

51 }

52

53 //k1 and k2 to create a new tree

54 q = new BTreeNode;

55 q->data = b[k1]->data + b[k2]->data;

56 q->left = b[k1];q->right = b[k2];

57 b[k1] = q;b[k2] =NULL;

58 }

59 delete []b;

60 return q;

61 }

62

63 ElemType WPL(BTreeNode *FBT,int len)

64 {

65 if(FBT == NULL) return 0;

66 else{

67 if(FBT->left == NULL && FBT->right ==NULL){return FBT->data*len;}

68 else{return WPL(FBT->left,len+1)+WPL(FBT->right,len+1);}

69 }

70 }

71

72 void PrintBTree(BTreeNode * BT)

73 {

74

75 if (!EmptyBTree(BT))

76 {

77 cout<<BT->data;

78 if (BT->left!=NULL||BT->right!=NULL)

79 {

80 cout<<'(';

81 PrintBTree(BT->left);

82 if(BT->right!=NULL)

83 cout << ',';

84 PrintBTree(BT->right);

85 cout<<')';

86 }

87 }

88 }

89 int depthbtree(BTreeNode * BT)

90 {

91 if(BT == NULL)

92 return 0;

93 else{

94 int dep1=depthbtree(BT->left);

95 int dep2=depthbtree(BT->right);

96 if (dep1>dep2)

97 {

98 return dep1+1;

99 }

100 else

101 {

102 return dep2+1;

103 }

104 }

105 }

106

107 void clearbtree(BTreeNode *&BT)

108 {

109 if (!EmptyBTree(BT))

110 {

111 clearbtree(BT->left);

112 clearbtree(BT->right);

113 delete BT;

114 BT = NULL;

115 }

116 }

117

118 void HuffManCoding(BTreeNode *FBT,int len)

119 {

120 static int a[10];

121 if(FBT!=NULL){

122 //if leaf cout the a[] (contain 0 or 1 )

123 if (FBT->left == NULL && FBT->right == NULL)

124 {

125 cout<<"Node weight = "<<FBT->data<<" /tHMC:";

126 for(int i = 0;i<len;++i) cout<<a[i]<<' ';

127 cout<<endl;

128 }else{

129 a[len]=0;HuffManCoding(FBT->left,len+1);

130 a[len]=1;HuffManCoding(FBT->right,len+1);

131 }

132 }

133 }

134 int main(int argc, char * argv[])

135 {

136

137 ElemType x;

138 BTreeNode* fbt;

139

140 InitBTree(fbt);

141 ElemType a[10] = {3,5,2,4,6,7,1,10,8,9};

142 //ElemType a[6] = {3,9,5,12,6,15};

143

144 fbt = CreateHuffman(a,10);

145

146 PrintBTree(fbt);

147 cout << endl;

148

149 cout <<"The FBT Tree Depth = " <<depthbtree(fbt)<<endl;

150

151 x = WPL(fbt,0);

152 cout <<"WPL:x = "<<x<<endl;

153

154 cout << "The Tree Leaf HuffManCoding:" << endl;

155 cout << "--------------------" << endl;

156 HuffManCoding(fbt,0);

157

158 clearbtree(fbt);

159 return 0;

160

161 }

-----------------------------------

$ ./a.out

55(22(10,12(6(3,3(1,2)),6)),33(15(7,8),18(9(4,5),9)))

The FBT Tree Depth = 6

WPL:x = 173

The Tree Leaf HuffManCoding:

----------------------------------

Node weight = 10 HMC:0 0

Node weight = 3 HMC:0 1 0 0

Node weight = 1 HMC:0 1 0 1 0

Node weight = 2 HMC:0 1 0 1 1

Node weight = 6 HMC:0 1 1

Node weight = 7 HMC:1 0 0

Node weight = 8 HMC:1 0 1

Node weight = 4 HMC:1 1 0 0

Node weight = 5 HMC:1 1 0 1

Node weight = 9 HMC:1 1 1

-----------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: