8000 huffman: 完全自己实现,完成,刘涵羽 · home-coder/data-abstraction-001@7425b80 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7425b80

Browse files
author
oneface
committed
huffman: 完全自己实现,完成,刘涵羽
1 parent 6905e40 commit 7425b80

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

006-huffman.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
**************************************************************************/
1313
#include <stdio.h>
1414
#include <stdlib.h>
15+
#include <string.h>
1516

1617
typedef struct {
1718
int weight;
@@ -120,27 +121,39 @@ static void inorder_traverse(hftree *h, int m, int *w)
120121

121122
static void huffman_coding_nodeorder(hftree *h, hfcode **hc, int s)
122123
{
123-
char *cd = (char *)malloc((n-1) * sizeof(char));
124+
int i, j;
125+
char *cd = (char *)malloc((s-1) * sizeof(char));
124126
*hc = (hfcode *)malloc(s * sizeof(hfcode));
127+
cd[s - 2] = '\0';
125128

126129
for (i = 1; i <= s; i++) {
127-
start = n - 1;
128-
while (h[i].parent != -1) {
129-
j = h[i].parent;
130-
if (h[j].left == i) {
130+
int c = i;
131+
int start = s - 1;
132+
133+
while (h[c].parent != -1) {
134+
j = h[c].parent;
135+
if (h[j].left == c) {
131136
cd[--start] = '0';
132137
} else {
133138
cd[--start] = '1';
134139
}
135-
136-
i = j;
140+
c = j;
137141
}
138-
(*hc)
142+
143+
(*hc)[i] = (char *)malloc((s - start) * sizeof(char));
139144
memcpy((*hc)[i], &cd[start], s - start);
140145
}
141146

142147
}
143148

149+
static void huffman_code_print(hfcode *hc, int s)
150+
{
151+
int i;
152+
for (i = 1; i <= s; i++) {
153+
printf("hc[%d]:%s\n", i, hc[i]);
154+
}
155+
}
156+
144157
int main()
145158
{
146159
int i;
@@ -163,6 +176,7 @@ int main()
163176
//from root
164177
//huffman_coding_rootorder(hft, 2 * s - 1);
165178

179+
huffman_code_print(hc, s);
166180
free(hft);
167181
return 0;
168182
}

0 commit comments

Comments
 (0)
0