-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.h
47 lines (36 loc) · 1.16 KB
/
huffman.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* huffman.h - huffman's algorithm
*
* Copyright (C) 2008 Jussi Mäki <[email protected]>
*/
#ifndef __HUFFMAN_H
#define __HUFFMAN_H
struct hcnode {
/* Tree pointers */
struct hcnode *left, *right;
struct hcnode *parent;
u32 frequency;
int character; /* Only leaves have this */
/* 32 bytes is enough, the tree has at maximum 257 leaves
(256 chars + EOF) so the code length (path through the tree) can
be then at maximum 256 bits since the tree is full:
O
/ \
O O This presents the full tree of maximum depth:
/ \ 4 leaves and the longest path is (LEAVES-1) 3.
O O
/ \
O O
*/
u8 code[32];
int code_len; /* Code length in bits */
};
/* Initializes 'count' one node trees, one for each character */
struct hcnode ** huffman_init(u32 freqs[], int chars[], size_t count);
/* Frees allocated resources */
void huffman_deinit(struct hcnode **nodes, struct hcnode *root);
/* The huffman's algorithm. Returns pointer to the root of the tree */
struct hcnode * huffman(struct hcnode **nodes, size_t count);
/* Generate prefix codes by traversing the tree */
void huffman_make_codes(struct hcnode *root);
#endif