| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 309 人关注过本帖
标题:求助大侠们哈弗曼树问题(内存不足)
只看楼主 加入收藏
狮子。
Rank: 2
来 自:湖北
等 级:论坛游民
帖 子:64
专家分:58
注 册:2010-8-1
结帖率:88.89%
收藏
 问题点数:0 回复次数:0 
求助大侠们哈弗曼树问题(内存不足)
程序代码:
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
#define    MAXLEN 100

typedef struct{
    char ch;
    int weight;
    int lchild;
    int rchild;
    int parent;
}HTNode;

typedef HTNode HFMT[MAXLEN];


typedef char **HfCode;
int n;


void CreateHT(HTNode ht[],int n)
{
int i,k,lnode,rnode;
int min1,min2;
for(i=0;i<2*n-1;i++)
{
ht[i].parent=-1;
ht[i].lchild=-1;
ht[i].rchild=-1;
ht[i].weight=0;
}
for(i=0;i<n;i++)
{
printf("输入结点的第%d个权值:",i+1);
scanf("%d",&ht[i].weight);
}
for(i=n;i<2*n-1;i++)
{
min1=min2=32767;
    lnode=rnode=0;
for(k=0;k<=i-1;k++)
if(ht[k].parent==-1)
{
if(ht[k].weight<min1)
{
min2=min1;
rnode=lnode;
min1=ht[k].weight;
lnode=k;
}
else if(ht[k].weight<min2)
{
min2=ht[k].weight;
rnode=k;
}
}
ht[lnode].parent=i;
ht[rnode].parent=i;
ht[i].weight=ht[lnode].weight+ht[rnode].weight;
ht[i].lchild=lnode;
ht[i].rchild=rnode;
}
}

//======================================================================================================

void PrintHFMT(HFMT T)
{
    int i,k=0;
    for(i=0;i<2*n-1;i++)
        while(T[i].lchild!=-1)
        {
            if(!(k%2))
            {
                printf("\n");
            }
            printf("\t\t(%d%d),(%d%d)",T[i].weight,T[i].lchild,T[i].weight,T[i].rchild);
            k++;
            break;
        }
}

void printHfCode(HfCode hc)
{
    for(int i=0;i<n;i++)
    {
        printf("%s",hc[i]);
    }
}

HfCode hfEnCoding(HFMT T)
{
    int start;
    HfCode hc=new char* [(n+1)*sizeof(char*)];
    char* cd=new char [n*sizeof(char)];
    cd[n-1]='\0';
    int c;
    int f;
    for(int i=0;i<n;i++)
    {
        start=n-1;
        for(c=i,f=T[i].parent;f!=-1;c=f,f=T[i].parent)
        {
            if(T[f].lchild==c)
            {
                cd[--start]='0';
            }
            else
            {
                cd[--start]='1';
            }
        }
        hc[i]=new char[(n-start)*sizeof(char)];
        strcpy(hc[i],&cd[start]);
        printf("\n%c:%s",T[i].ch,hc[i]);
    }
    return hc;
}

void print_HuffmanTree(HFMT HT,int t,int i)
{
    if(HT[t].rchild!=-1)
    {
        print_HuffmanTree(HT,HT[t].rchild,i+1);
    }
    for(int j=1;j<3*i;j++)
    {
        printf("");
    }
    if(HT[t].lchild!=-1||HT[t].rchild!=-1)
    {
        printf("%d\n",HT[t].weight);
    }
    else
    {
        printf("%c(%d)\n",HT[t].ch,HT[t].weight);
    }
    if(HT[t].lchild!=-1)
    {
        print_HuffmanTree(HT,HT[t].lchild,i+1);
    }
}

//==============================================================================================================

void Encoder(char *original,char *codeFile,HfCode hc,HFMT HT)
{
    char *str;
    int i=0;
    char ch;
    int k=0;
    FILE *fin;
    FILE *fout;
    if((fin=fopen("C:\\original","r"))!=NULL)
    {
        fscanf(fin,"%c",&ch);
        while(!feof(fin))
        {
            k++;
            fscanf(fin,"%c",&ch);
        }
    }
    fclose(fin);
    str=new char[k+1];
    k=0;
    if((fin=fopen("C:\\original","r"))!=NULL)
    {
        fscanf(fin,"%c",&ch);
        while(!feof(fin))
        {
            str[k++]=ch;
            fscanf(fin,"%c",&ch);
        }
    }
    fclose(fin);
    str[k]='\0';
    printf("要编码的数据是:\n");
    printf("%s\n",str);
    k=0;
    if((fout=fopen("C:\\codeFile","w"))!=NULL)
    {
        while(str[k]!='\0')
        {
            for(i=0;i<n;i++)
            {
                if(str[k]==HT[i].ch)
                {
                    fprintf(fout,"%s",hc[i]);
                    break;
                }
            }
            k++;
        }
        printf("已编码!且存到文件codeFile.dat中!\n\n");
    }
    fclose(fout);
}

void Decoder(char *codeFile,char *textFile,HFMT HT)
{
    int i=0,k=0;
    int j=n*2-1-1;
    char *bitStr;
    FILE *fin;
    FILE *fout;
    printf("经译码的内容为:\n");
    char ch;
    if((fin=fopen("C:\\codeFile","r"))!=NULL)
    {
        fscanf(fin,"%c",&ch);
        while(!feof(fin))
        {
            k++;
            fscanf(fin,"%c",ch);
        }
    }
    fclose(fin);
    bitStr=new char[k+1];
    k=0;
    if((fin=fopen("C:\\codeFile","r"))!=NULL)
    {
        fscanf(fin,"%c",&ch);
        while(!feof(fin))
        {
            bitStr[k++]=ch;
            fscanf(fin,"%c",&ch);
        }
    }
    fclose(fin);
    bitStr[k]='\0';
    if(HT==NULL)
    {
        printf("请先编码!\n");
        return;
    }
    if((fout=fopen("C:\\textFile","w"))!=NULL)
        while(bitStr[i]=='\0')
        {
            if(bitStr[i]=='0')
                j=HT[j].lchild;
            else
                j=HT[j].rchild;
            if(HT[j].rchild==-1)
            {
                ch=HT[j].ch;
                fprintf(fout,"%c",ch);
                j=n*2-1-1;
            }
            i++;
        }
        fclose(fout);
        printf("\n译码成功且已存到文件textFile.text\n\n");
}

void main()
{
    HFMT HT;

    int n;
    printf("输入权值个数:\n");
    scanf("%d",&n);
    CreateHT(HT,n);


    PrintHFMT(HT);

    HfCode hc=hfEnCoding(HT);
    printf("\n哈弗曼树形态为:\n");
    print_HuffmanTree(HT,2*n-2,0);

    Encoder("original.text","codefile.text",hc,HT);
    Decoder("codeFile.text","textfile.text",HT);
    printf("\n");
}
运行时总报内存不足,百度了半天还是不知道怎么回事。(ps:本人基础较差,请大家见谅~)
2011-06-28 17:12
快速回复:求助大侠们哈弗曼树问题(内存不足)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035949 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved