压缩文件程序

时间:2017-12-02 12:19:35

标签: c algorithm compression binary-tree huffman-code

我正在尝试构建一个压缩文件的程序。 我正在使用霍夫曼算法,我用视频研究了它:https://www.youtube.com/watch?v=dM6us854Jk0&t=436s

我尝试在位上使用相同的方法 - 首先我在Nibbles上尝试过: 我拿了每个Nibble(16选项)并给它一个随机频率,后来我建立了一个二进制树,按照Nibbles的频率排序,就像在视频中一样。

我成功地将22K的位压缩到18K,到目前为止它已经工作了。 然后我在Bytes(256选项)上尝试了它并且它不起作用 - 在开始时它有13M位并且在压缩之后它有89M。

我有一张图片显示了Nibble示例的二叉树:

img

并且还有两个指定计算Nibbles树和Bytes树的exel文件:

我用C语言实现了算法,这里有部分功能:

typedef struct INFO
{
    unsigned char binary;   //Binary number
    int amount; //Frequency
} INFO;

typedef struct TREE
{
    INFO info;
    struct TREE *prev;
    struct TREE *left;
    struct TREE *right;
} TREE;



/** Function that allocates memory and creates a tree node and initializes it */
TREE * treeNodeMalloc()
{
    TREE *p;
    p = (TREE *)malloc(sizeof(TREE));
    if (!p)
        return NULL;
    p->prev = p->left = p->right = NULL;
    return p;
}


/** Function that builds the first sub-root node consist of two binary numbers */
TREE * firstNode(INFO first, INFO second)
{
    TREE *head, *p;
    int i;
    head = treeNodeMalloc();
    if (!head) return 0;
    for (i = 1; i <= 2; i++)
    {
        p = treeNodeMalloc();
        if (!p) { freeTree(head); return 0; }
        p->prev = head;
        if (i % 2)
        {
            p->info.amount = first.amount;
            p->info.binary = first.binary;
            head->left = p;
        }
        else
        {
            p->info.amount = second.amount;
            p->info.binary = second.binary;
            head->right = p;
        }
    }
    head->info.amount = head->left->info.amount + head->right->info.amount;
    return head;
}


/** Function that builds a sub-root node that consist of a node of binary number and a sub-root of two previous binary numbers */
TREE * continuanceNode(TREE *p1, INFO info)
{
    TREE *h, *p2;
    h = treeNodeMalloc();
    if (!h) { freeTree(p1); return 0; }
    p2 = treeNodeMalloc();
    if (!p2) { free(h); freeTree(p1); return 0; }
    p2->info.amount = info.amount;
    p2->info.binary = info.binary;
    p1->prev = p2->prev = h;
    h->left = p1;
    h->right = p2;
    h->info.amount = h->left->info.amount + h->right->info.amount;
    return h;
}


/** Function that builds the last node of the tree - the main root */
TREE * LastNode(TREE *p1, TREE *p2)
{
    TREE *p3;
    p3 = treeNodeMalloc();
    if (!p3)
    {
        freeTree(p1);
        freeTree(p2);
        return NULL;
    }
    p3->left = p1;
    p3->right = p2;
    p1->prev = p2->prev = p3;
    p3->info.amount = p3->left->info.amount + p3->right->info.amount;
    return p3;
}



/** Function that builds the binary tree from the array of INFO (binary numbers and their frequencies),
The function builds the tree from bottum to the top (reverse build) */
TREE * dataToTree(INFO arr[], int size)
{
    int i;
    TREE *h, *p, *t=NULL;
    p = firstNode(arr[0], arr[1]);
    if (!p) return 0;
    for (i = 2; i < size; i++)
    {
        if (p->info.amount > arr[size - 1].amount)
            if (!t)
            {
                t = firstNode(arr[i], arr[i + 1]); 
                i++;
                if (!t) { freeTree(p); return NULL; }
            }
            else
                if (p->info.amount < t->info.amount)
                {
                    p = continuanceNode(p, arr[i]);
                    if (!p) { freeTree(t); return 0; }
                }
                else
                {
                    t = continuanceNode(t, arr[i]);
                    if (!t) { freeTree(p); return 0; }
                }
        else
        {
            p = continuanceNode(p, arr[i]);
            if (!p) { freeTree(t); return 0; }
        }
    }
    h = LastNode(p, t);
    return h;
}

每个人都说Huffman算法最适合压缩文件,所以我在这里缺少什么? 我在做什么?

1 个答案:

答案 0 :(得分:1)

你建造的霍夫曼树是错的。在每个步骤中,您需要融合所有可用根节点中具有最低频率的两个节点。所以第一个保险丝9和14给你:

  21
 /  \
9   14

下一步是融合21和20

    41
   /  \
  21  20
 /  \
9   14

然后41和50

      91
     /  \
    41  50
   /  \
  21  20
 /  \
9   14

但是在这一步中,最低的两个是70和80,所以将它们分开融合

      91     150
     /  \   /  \
    41  50 70  80
   /  \
  21  20
 /  \
9   14

然后你必须融合两个最低,91和100等

树将更加平衡&#34;,结果可能更好。

你应该知道(根据编码理论)某些文本无法压缩。对于给定的压缩算法,总是存在至少一个不能被压缩的文本。一般而言,无论您尝试使用何种算法,都至少存在一个无法压缩的文本。所有这些都需要更多的理论解释,但这大致是理论可以说的。