注册 登录
编程论坛 数据结构与算法

送20分。。麻烦高手帮忙改错。。。

Vi_No 发布于 2010-05-02 17:27, 956 次点击
应该是蛮白痴的错误。。但是最近大脑故障了。麻烦帮忙看看。

//HuffmanCode

#include <iostream>
#include <string.h>

#define MAXSIZE 15

using namespace std;

typedef struct
{
    unsigned int weight;
    unsigned int parent, lchild, rchild;
}HTNode, *HuffmanTree;

typedef char **HuffmanCode;

void Select(HuffmanTree &HT, int n, int &s1, int &s2)
{
    //选择HT[1..n]中parent为0且weight最小的两个结点
    //序号分别赋值给s1,s2
    int tmp;
    for (int i = 1; i <= n; i++)
        if (HT[i].parent == 0)
        {
            s1 = i;
            break;
        }
    for (; i <= n; i++)
        if (HT[i].parent == 0)
        {
            s2 = i;
            break;
        }
    if (HT[s1].weight > HT[s2].weight)
    {
        tmp = s1;
        s1 = s2;
        s2 = tmp;
    }
    for (; i <= n; i++)
        if (HT[i].parent == 0 && HT[i].weight < HT[s2].weight)
            if (HT[i].weight < HT[s1].weight)
            {
                tmp = s1;
                s1 = i;
                s2 = tmp;
            }
            else
                s2 = i;
}

void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n)
{
    //建立哈夫曼树,求哈夫曼编码
    HuffmanTree p;
    char *cd;
    int i, s1, s2, start;
    unsigned int c, f;
    if (n <= 1)
        return;
    int m = 2 * n - 1;

    HT = (HuffmanTree) malloc((m + 1) * sizeof(HTNode));
    for (p = HT, i = 1; i <= n; ++i, ++p, ++w)
    {
        p->weight = *w;
        p->parent = 0;
        p->lchild = 0;
        p->rchild = 0;
    }
    for (; i <= m; ++i, ++p)
    {
        p->weight = 0;
        p->parent = 0;
        p->lchild = 0;
        p->rchild = 0;
    }
    for (i = n + 1; i <= m; ++i)
    {
        Select(HT, i-1, s1, s2);
        HT[s1].parent = i;
        HT[s2].parent = i;
        HT[i].lchild = s1;
        HT[i].rchild = s2;
        HT[i].weight = HT[s1].weight + HT[s2].weight;
    }

    HC = (HuffmanCode) malloc((n+1) * sizeof(char *));
    cd = (char *) malloc(n * sizeof(char));
    cd[n-1] = '\0';
    for (i = 1; i <= n; ++i)
    {
        start = n - 1;
        for (c = i, f = HT[c].parent; f != 0; c = f, f = HT[f].parent)
            if (HT[f].lchild == c)
                cd[--start] = '0';
            else
                cd[--start] = '1';
        HC[i] = (char *) malloc((n - start) * sizeof(char));
        strcpy(HC[i], &cd[start]);
        printf("%s\n", HC[i]);
    }
    free(cd);
}

int main()
{
    HuffmanTree HT;
    HuffmanCode HC;
    int w[MAXSIZE], n;

    cout << "请输入字符数目\n";
    cin >> n;
    cout << "请输入各字符的权值\n";
    for (int i = 0; i < n; i++)
        cin >> w[i];

    HuffmanCoding(HT, HC, w, n);

    return 0;
}
7 回复
#2
2010-05-02 17:48
20分!!
#3
qq88011032010-05-02 21:36
楼主总要说下大概的功能吧
#4
Vi_No2010-05-02 22:05
回复 3楼 qq8801103
Huffman编码呀。不是说了么。
#5
Vi_No2010-05-07 18:13
怎么没人帮忙改改类。。拜托了。。。
#6
hzh5122010-05-07 21:57
你 select 函数写错了!
下面是正确的源码:
程序代码:
#include <iostream>
#include <string.h>
#define MAXSIZE 15
using namespace std;
typedef struct
{
    unsigned int weight;
    unsigned int parent, lchild, rchild;
}HTNode, *HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree &HT, int n, int &s1, int &s2);
void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n);
int main()
{
    HuffmanTree HT;
    HuffmanCode HC; //typedef char **HuffmanCode
    int w[MAXSIZE]={5,29,7,8,14,23,3,11}, n=8;


 /* cout << "请输入字符数目\n";
    cin >> n;
    cout << "请输入各字符的权值\n";
    for (int i = 0; i < n; i++)

 cin >> w[i];

 
*/
    HuffmanCoding(HT, HC, w, n);

    return 0;
}

void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n)
{
    //建立哈夫曼树,求哈夫曼编码
    HuffmanTree p;
    char *cd;
    int i, s1, s2, start;
    unsigned int c, f;
    if (n <= 1)
        return;
    int m = 2 * n - 1;

    HT = (HuffmanTree) malloc((m + 1) * sizeof(HTNode));

 p=HT;
    for (++p, i = 1; i <= n; ++i, ++p, ++w)
    {
        p->weight = *w;
        p->parent = 0;
        p->lchild = 0;
        p->rchild = 0;
    }
    for (; i <= m; ++i, ++p)
    {
        p->weight = 0;
        p->parent = 0;
        p->lchild = 0;
        p->rchild = 0;
    }
    for (i = n + 1; i <= m; ++i)
    {
        Select(HT, i-1, s1, s2);
        HT[s1].parent = i;
        HT[s2].parent = i;
  if(s1<s2)
  {
   HT[i].lchild = s1;
   HT[i].rchild = s2;
  }
  else
  {
   HT[i].lchild = s2;
   HT[i].rchild = s1;
  }
        HT[i].weight = HT[s1].weight + HT[s2].weight;
    }

    HC = (HuffmanCode) malloc((n+1) * sizeof(char *));
    cd = (char *) malloc(n * sizeof(char));
    cd[n-1] = '\0';
    for (i = 1; i <= n; ++i)
    {
        start = n - 1;
        for (c = i, f = HT[c].parent; f != 0; c = f, f = HT[f].parent)
            if (HT[f].lchild == c)
                cd[--start] = '0';
            else
                cd[--start] = '1';
   HC[i] = (char *) malloc((n - start) * sizeof(char));
   strcpy(HC[i], &cd[start]);
   printf("%s\n", HC[i]);
    }
    free(cd);
}
void Select(HuffmanTree &HT, int n, int &s1, int &s2)
{
    //选择HT[1..n]中parent为0且weight最小的两个结点
   
//序号分别赋值给s1,s2
    for (int i = 1; i <= n; i++)

 {
  if (HT[i].parent == 0)
        {
            s1 = i;
            break;
        }

 }

 for (; i <= n; i++)

 {
  if (HT[i].weight < HT[s1].weight && HT[i].parent == 0)
   s1 = i;

 }

 for (i=1; i <= n; i++)

 {
  if (HT[i].parent == 0 && i != s1)
  {
   s2 = i;
   break;
  }

 }

 for (; i <= n; i++)

 {
  if (HT[i].weight < HT[s2].weight && HT[i].parent == 0 && i != s1)
   s2 = i;

 }
}

#7
南国利剑2010-05-07 22:02
呵呵,顶楼上!
#8
Vi_No2010-05-08 13:13
回复 6楼 hzh512
谢谢。

[ 本帖最后由 Vi_No 于 2010-5-8 13:24 编辑 ]
1