由于哈夫曼树的创建和编码解码过程比较复杂,代码较长,在此只提供部分代码作为参考。

  1. 哈夫曼树的创建

首先需要定义哈夫曼树节点的结构体:

typedef struct node {
    int weight;           // 节点权值
    int parent, lchild, rchild;    // 父节点、左子节点、右子节点的下标
} HufNode, *HufTree;

接下来是哈夫曼树的创建函数:

// 构造哈夫曼树
void CreateHuffmanTree(HufTree *ht, int n) {
    if (n <= 1) {
        return;
    }
    int m = 2 * n - 1;    // 哈夫曼树总节点数
    *ht = (HufNode *) malloc((m + 1) * sizeof(HufNode));    // 动态分配空间
    HufTree p = *ht;
    for (int i = 1; i <= n; i++) {    // 输入n个权值
        scanf('%d', &((p + i)->weight));
        (p + i)->parent = 0;
        (p + i)->lchild = 0;
        (p + i)->rchild = 0;
    }
    for (int i = n + 1; i <= m; i++) {    // 初始化树中剩余节点
        (p + i)->weight = 0;
        (p + i)->parent = 0;
        (p + i)->lchild = 0;
        (p + i)->rchild = 0;
    }
    // 构造哈夫曼树
    for (int i = n + 1; i <= m; i++) {
        int s1 = SelectMinWeight(*ht, i - 1);    // 选择权值最小的两个节点
        int s2 = SelectMinWeight(*ht, i - 1);
        (p + s1)->parent = i;
        (p + s2)->parent = i;
        (p + i)->lchild = s1;
        (p + i)->rchild = s2;
        (p + i)->weight = (p + s1)->weight + (p + s2)->weight;
    }
}

其中SelectMinWeight函数为选择权值最小的节点:

int SelectMinWeight(HufTree ht, int n) {
    int min = INT_MAX;
    int index;
    for (int i = 1; i <= n; i++) {
        if ((ht + i)->weight < min && (ht + i)->parent == 0) {
            min = (ht + i)->weight;
            index = i;
        }
    }
    (ht + index)->parent = -1;    // 标记该节点已被选择
    return index;
}
  1. 哈夫曼编码

哈夫曼编码的实现需要用到栈,这里定义一个栈的结构体:

typedef struct {
    int data[MAX_TREE_SIZE];
    int top;
} Stack;

接下来是哈夫曼编码函数:

// 哈夫曼编码
void HuffmanCoding(HufTree ht, int n) {
    Stack s;
    s.top = -1;
    for (int i = 1; i <= n; i++) {
        int current = i;
        int parent = (ht + current)->parent;
        while (parent != 0) {    // 从叶子节点依次向上遍历
            if (current == (ht + parent)->lchild) {
                Push(&s, 0);    // 记录节点在哈夫曼树中的位置,0表示左子节点
            } else {
                Push(&s, 1);    // 1表示右子节点
            }
            current = parent;
            parent = (ht + current)->parent;
        }
        printf('%d的哈夫曼编码:', i);
        while (!IsEmpty(s)) {    // 输出当前节点的哈夫曼编码
            printf('%d', Pop(&s));
        }
        printf('\n');
    }
}

其中Push和Pop函数为栈的入栈和出栈操作:

void Push(Stack *s, int data) {
    if (s->top < MAX_TREE_SIZE - 1) {
        s->data[++s->top] = data;
    }
}

int Pop(Stack *s) {
    if (s->top >= 0) {
        return s->data[s->top--];
    }
    return -1;
}

int IsEmpty(Stack s) {
    return s.top == -1;
}
  1. 哈夫曼解码

哈夫曼解码的实现需要用到二叉树,这里定义一个二叉树节点的结构体:

typedef struct node {
    char data;
    struct node *lchild, *rchild;
} BiTNode, *BiTree;

接下来是哈夫曼解码函数:

// 哈夫曼解码
void HuffmanDecoding(HufTree ht, int n, char *str) {
    BiTree tree = CreateBiTree(ht, n);    // 由哈夫曼树创建二叉树
    BiTree p = tree;
    printf('哈夫曼编码:%s\n', str);
    printf('哈夫曼解码:');
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == '0') {
            p = p->lchild;
        } else if (str[i] == '1') {
            p = p->rchild;
        }
        if (p->lchild == NULL && p->rchild == NULL) {    // 当前节点为叶子节点,输出对应字符
            printf('%c', p->data);
            p = tree;    // 从根节点重新开始遍历
        }
    }
    printf('\n');
}

其中CreateBiTree函数为由哈夫曼树创建二叉树:

// 由哈夫曼树创建二叉树
BiTree CreateBiTree(HufTree ht, int n) {
    BiTree tree = NULL;
    for (int i = 1; i <= n; i++) {
        char data;
        printf('请输入第%d个叶子节点的字符:', i);
        scanf(' %c', &data);
        tree = InsertNode(tree, (ht + i)->weight, data);    // 将字符与权值插入二叉树
    }
    return tree;
}

// 插入节点
BiTree InsertNode(BiTree tree, int weight, char data) {
    if (tree == NULL) {
        tree = (BiTree) malloc(sizeof(BiTNode));
        tree->data = data;
        tree->lchild = NULL;
        tree->rchild = NULL;
    } else if (weight <= tree->data) {
        tree->lchild = InsertNode(tree->lchild, weight, data);
    } else {
        tree->rchild = InsertNode(tree->rchild, weight, data);
    }
    return tree;
}

完整代码:https://github.com/lvxiaoyu2005/Huffman-Tree-C-Implementation

C语言实现哈夫曼树编码解码:详细代码解析