zoukankan      html  css  js  c++  java
  • 实验六 huffman树的实现及应用

    根据表格中各字符的出现概率生成一棵huffman树,并完成每个字符的huffman编码。


    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    #define maxsize 100
    struct huff
    {
        int data; 
        int pa,lr,rc;
        char name;
    };
    struct stack
    {
        int bit[maxsize];
        int top;
    };
    typedef struct stack Stack;
    void init(Stack *s)
    {
        s->top=-1;
    }
    bool Empty(Stack *s)
    {
        if(s->top==-1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    bool full(Stack *s)
    {
        if(s->top==maxsize-1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
    void Push(Stack *s,int element)
    {
        if(!full(s))
        {
            s->top++;
            s->bit[s->top]=element;
        }
        else
        {
            printf("栈满
    ");
        }
    }
    
    //出栈
    void Pop(Stack *s)
    {
        if(!Empty(s))
        {
            s->top--;
        }
        else
        {
            printf("栈空
    ");
        }
    }
    
    //取栈顶元素
    int Top(Stack *s)
    {
        if(!Empty(s))
        {
            return s->bit[s->top];
        }
        else
        {
            printf("栈空
    ");
        }
    }
    huff t[100];
    void huffman(int n,int w[])
    {
        int x1,x2,m1,m2,k;
        for(int i=1;i<=2*n-1;i++)
        {
            t[i].lr=0;
            t[i].pa=0;
            t[i].rc=0;
            if(i<=n)
            {
                t[i].data=w[i];
            }
            else
            t[i].data=0;
        }
        for(int i=1;i<n;i++)
        {
        
           m1=m2=100000;
        x1=x2=0;
        for(int j=1;j<(n+i);j++)
        { if((t[j].data<m1)&&(t[j].pa==0))
            {
                m2=m1;
                x2=x1;
                m1=t[j].data;  
                x1=j;
            }
        else if((t[j].data<m2)&&(t[j].pa==0))
        { 
            m2=t[j].data; x2=j; }
        }
        k=n+i;
        t[x1].pa=t[x2].pa=k;
        t[k].data=m1+m2;
        t[k].lr=x1;     
        t[k].rc=x2;
       }
    }
    void code(int n,huff c[])
    {
        int f,x,y,z;
        char a='a';
        stack p;
        stack *s;
        s=&p;
        init(s);
        for(int i=1;i<=n;i++)
        {
            x=i;
            y=c[i].pa;
            while(c[y].pa!=0)
            {
                if(c[y].lr==x)
                {
                    Push(s,0);
                }
                else if(c[y].rc==x)
                {
                    Push(s,1);
                }
                x=y;
                y=c[y].pa;
            }
            if(c[y].pa==0)
            {
                if(c[y].lr==x)
                {
                    Push(s,0);
                }
                else if(c[y].rc==x)
                {
                    Push(s,1);
                }
            }
            printf("%c的编码为:",a);
            a++;
            while(s->top!=-1)
            {
                f=Top(s);
                Pop(s);
                printf("%d",f);
            }
            printf("
    ");
        }
    }
    int main()
    {
        int n, w[30];
        char c='a';
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        scanf("%d",&w[i]);
        huffman(n,w);
        for(int i=1;i<=n;i++)
        {
            t[i].name=c;
            c++;
        }
        for(int i=1;i<=2*n-1;i++)
        {
            printf("%c %6d %d %d %d
    ",t[i].name,t[i].data,t[i].lr,t[i].pa,t[i].rc);
        }
       code(n,t); 
    }
    /*26
    856
    139
    297
    378
    1304
    289
    199
    528
    627
    12
    42
    339
    249
    707
    797
    199
    12
    677
    607
    1045
    249
    92
    149
    17
    199
    8*/
  • 相关阅读:
    使用OwnCloud建立属于自己私有的云存储网盘
    Linux服务器学习----tomcat 服务配置实验报告(一)
    Linux服务器学习----haproxy+keepalived
    Docker容器版Jumpserver堡垒机搭建部署方法附Redis
    Dokcer的一些命令:
    Docker安装prometheus监控
    CentOS7安装Docker
    用Dockerfile来制作contos镜像
    CentOS7中服务器网卡配置——配置静态IP
    CentOS7中搭建rabbitmq单机
  • 原文地址:https://www.cnblogs.com/accept/p/8175941.html
Copyright © 2011-2022 走看看