数据结构-二叉树(Huffman算法)
huffman算法即最优二叉树的实现:(其中用到的堆栈算法在前面的文章中以列出)
1 // Huffman.cpp : 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include "stack.h"
6 #include <stdlib.h>
7 #define MAXINT 2147483647
8 #define MAXNODE 100
9
10 struct HtNode
11 {
12 int ww;
13 int parent, llink, rlink;
14 };
15
16 struct HtTree
17 {
18 struct HtNode ht[MAXNODE];
19 int root;
20 int count;
21 };
22
23 typedef struct HtTree * PHtTree;
24
25 PHtTree huffman(int m, int * w)
26 /* 构造具有m 个叶子节点的哈夫曼树*/
27 {
28 PHtTree pht;
29 int i, j, x1, x2, m1, m2;
30 pht = (PHtTree)malloc(sizeof(struct HtTree));
31 if(pht == NULL)
32 {
33 printf("Out of space!!\n");
34 return pht;
35 }
36 pht->count = m;
37 for(i = 0; i < 2 * m - 1; i++)
38 {
39 pht->ht[i].llink = -1;
40 pht->ht[i].rlink = -1;
41 pht->ht[i].parent = -1;
42 if(i < m)
43 pht->ht[i].ww = w[i];
44 else
45 pht->ht[i].ww = -1;
46 }
47
48 for(i = 0; i < m - 1; i++)
49 {
50 m1 = MAXINT, m2 = MAXINT;
51 x1 = -1; x2 = -1;
52 for(j = 0; j < m + i; j++)
53 {
54 if(pht->ht[j].ww < m1 && pht->ht[j].parent == -1)
55 {
56 m2 = m1;
57 x2 = x1;
58 m1 = pht->ht[j].ww;
59 x1 = j;
60 }
61 else
62 {
63 if(pht->ht[j].ww < m2 && pht->ht[j].parent == -1)
64 {
65 m2 = pht->ht[j].ww;
66 x2 = j;
67 }
68 }
69 }
70 pht->ht[x1].parent = m + i; /*构造一个内部节点*/
71 pht->ht[x2].parent = m + i;
72 pht->ht[m + i].ww = m1 + m2;
73 pht->ht[m + i].llink = x1;
74 pht->ht[m + i].rlink = x2;
75 pht->root = m + i;
76 }
77 return pht;
78 }
79
80 void printThreeEncode(PHtTree tree)
81 {
82 PSeqStack pastack = createEmptyStack_seq();
83 int j;
84 for(int i = 0; i < tree->count; i++)
85 {
86 printf("Eoncode for W %d is: ",tree->ht[i].ww );
87 j = i;
88 HtNode node = tree->ht[i];
89 do
90 {
91 if(tree->ht[node.parent].llink == j )
92 {
93 push_seq(pastack,0);
94 }
95 else
96 {
97 push_seq(pastack,1);
98 }
99 j = node.parent;
100 node = tree->ht[j];
101 }
102 while(j != tree->root);
103 int code;
104 while(!isEmptyStack_seq(pastack))
105 {
106 code = top_seq(pastack);
107 pop_seq(pastack);
108 printf(" %d ", code);
109 }
110 printf("\n");
111
112 }
113 }
114
115 int _tmain(int argc, _TCHAR* argv[])
116 {
117 int w[] = {2,3,5,7,11,13,17,19,23,29,31,37,41};
118 printThreeEncode(huffman(13,w));
119 return 0;
120 }
2 //
3
4 #include "stdafx.h"
5 #include "stack.h"
6 #include <stdlib.h>
7 #define MAXINT 2147483647
8 #define MAXNODE 100
9
10 struct HtNode
11 {
12 int ww;
13 int parent, llink, rlink;
14 };
15
16 struct HtTree
17 {
18 struct HtNode ht[MAXNODE];
19 int root;
20 int count;
21 };
22
23 typedef struct HtTree * PHtTree;
24
25 PHtTree huffman(int m, int * w)
26 /* 构造具有m 个叶子节点的哈夫曼树*/
27 {
28 PHtTree pht;
29 int i, j, x1, x2, m1, m2;
30 pht = (PHtTree)malloc(sizeof(struct HtTree));
31 if(pht == NULL)
32 {
33 printf("Out of space!!\n");
34 return pht;
35 }
36 pht->count = m;
37 for(i = 0; i < 2 * m - 1; i++)
38 {
39 pht->ht[i].llink = -1;
40 pht->ht[i].rlink = -1;
41 pht->ht[i].parent = -1;
42 if(i < m)
43 pht->ht[i].ww = w[i];
44 else
45 pht->ht[i].ww = -1;
46 }
47
48 for(i = 0; i < m - 1; i++)
49 {
50 m1 = MAXINT, m2 = MAXINT;
51 x1 = -1; x2 = -1;
52 for(j = 0; j < m + i; j++)
53 {
54 if(pht->ht[j].ww < m1 && pht->ht[j].parent == -1)
55 {
56 m2 = m1;
57 x2 = x1;
58 m1 = pht->ht[j].ww;
59 x1 = j;
60 }
61 else
62 {
63 if(pht->ht[j].ww < m2 && pht->ht[j].parent == -1)
64 {
65 m2 = pht->ht[j].ww;
66 x2 = j;
67 }
68 }
69 }
70 pht->ht[x1].parent = m + i; /*构造一个内部节点*/
71 pht->ht[x2].parent = m + i;
72 pht->ht[m + i].ww = m1 + m2;
73 pht->ht[m + i].llink = x1;
74 pht->ht[m + i].rlink = x2;
75 pht->root = m + i;
76 }
77 return pht;
78 }
79
80 void printThreeEncode(PHtTree tree)
81 {
82 PSeqStack pastack = createEmptyStack_seq();
83 int j;
84 for(int i = 0; i < tree->count; i++)
85 {
86 printf("Eoncode for W %d is: ",tree->ht[i].ww );
87 j = i;
88 HtNode node = tree->ht[i];
89 do
90 {
91 if(tree->ht[node.parent].llink == j )
92 {
93 push_seq(pastack,0);
94 }
95 else
96 {
97 push_seq(pastack,1);
98 }
99 j = node.parent;
100 node = tree->ht[j];
101 }
102 while(j != tree->root);
103 int code;
104 while(!isEmptyStack_seq(pastack))
105 {
106 code = top_seq(pastack);
107 pop_seq(pastack);
108 printf(" %d ", code);
109 }
110 printf("\n");
111
112 }
113 }
114
115 int _tmain(int argc, _TCHAR* argv[])
116 {
117 int w[] = {2,3,5,7,11,13,17,19,23,29,31,37,41};
118 printThreeEncode(huffman(13,w));
119 return 0;
120 }
121
运行结果为:
Eoncode for W 2 is: 1 0 1 1 1 1 0
Eoncode for W 3 is: 1 0 1 1 1 1 1
Eoncode for W 5 is: 1 0 1 1 1 0
Eoncode for W 7 is: 1 0 1 1 0
Eoncode for W 11 is: 0 1 0 0
Eoncode for W 13 is: 0 1 0 1
Eoncode for W 17 is: 1 0 1 0
Eoncode for W 19 is: 0 0 0
Eoncode for W 23 is: 0 0 1
Eoncode for W 29 is: 0 1 1
Eoncode for W 31 is: 1 0 0
Eoncode for W 37 is: 1 1 0
Eoncode for W 41 is: 1 1 1