zoukankan      html  css  js  c++  java
  • 数据结构之哈夫曼树

    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    typedef struct
    {
        string name;
        int weight;
        int parent,
            lchild,
            rchild;
        int visited;      //设置visited选项来表示每次查找最小权值后的删除,0代表未删除,1表示删除
    }HTNode,*HuffmanTree;
    
    int Min(HuffmanTree ht,int m)
    {
        int min;
        for(int i=1;i<=m;i++)      //在没有访问过的节点中选择第一个权值作为最小的权值
        {                            
            if(ht[i].visited==0)
            {
                min=i;            
                break;
            }
        }
        for(int i=1;i<=m;i++)        //将选择的最小权值与其他未访问过的权值做比较求出最小权值
        {                            //在此出错,将i的值赋给min,导致循环给min赋值出错。要在循环外给min赋值
            if(ht[i].visited==0&&ht[min].weight>ht[i].weight)
            {
                min=i;
            }
        }
                                                //输出min,验证是否找到的是最小权值的位置
        ht[min].visited=1;
    
        return min;
    }
    
    int main()
    {
        HuffmanTree ht;         //全局变量,定义哈夫曼树
        string *hc;                //全局变量,定义哈夫曼编码
        int  ht1=0,
             hc1=0;
        int choice;
        int m,
            n;
        while(1)
        {
            if(ht1==0&&hc1==0)
            {
                cout <<"1. 建立哈夫曼树"<<endl;
                cout <<"---------------------------------------------------------------------------"<<endl;
            }
            if(ht1==1&&hc1==0)
            {
                cout <<"1. 建立哈夫曼树     2. 哈夫曼树编码"<<endl;
                cout <<"---------------------------------------------------------------------------"<<endl;
            }
            if(ht1==1&&hc1==1)
            {
                cout<<"1.  建立哈夫曼树     2  哈夫曼树编码   3.  编码    4.  译码   5.  结束"<<endl;
                cout <<"---------------------------------------------------------------------------"<<endl;
            }
            cout <<"请输入你的选择:";
            cin>>choice;
            if(choice==1)
            {
                
                cout <<"请输入点的个数:";
                cin>>n;
    
                m=2*n-1;
                ht=new HTNode[m+1];
                for(int i=1;i<=m;i++)
                {
                    ht[i].lchild=0;
                    ht[i].rchild=0;
                    ht[i].parent=0;
                    ht[i].visited=0;   
                }
        
                cout <<"请输入这几个点的相应的信息和权值:"<<endl;
                for(int i=1;i<=n;i++)
                {
                    cin>>ht[i].name;
                    cin>>ht[i].weight;    
                }
    
                int    s1,
                    s2;
                for(int i=n+1;i<=m;i++)
                {
                    s1=Min(ht,i-1);
                    s2=Min(ht,i-1);
                                   //输出最小权值的位置,验证算法是否正确
                    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;
                }
    
                ht1=1;
                cout <<"哈夫曼树构造成功!"<<endl;
                cout <<endl;
            }
            else if(choice==2)
            {
                string str;
                hc=new string[n+1];
                char *cd;
                cd=new char[n];
                cd[n-1]='';
                int    start,
                    f,
                    c;
                for(int i=1;i<=n;i++)
                {
                    start=n-1;
                    c=i;
                    f=ht[i].parent;
                    while(f!=0)
                    {
                        --start;
                        if(ht[f].lchild==c)
                            cd[start]='0';
                        else
                            cd[start]='1';
                        c=f;
                        f=ht[f].parent;
                    }
                        str=cd;
                        hc[i]=str.substr(start);
                    }
                delete cd;
    
                hc1=1;
                cout <<"哈夫曼编码成功!"<<endl;
                cout <<endl;
            }
            else if(choice ==3)
            {
                int result=0;                //用来判断是否找到
                string search;
                cout<<"输入你想要查询的字符:";
                cin>>search;
                for(int i=1;i<=n;i++)
                {
                    if(ht[i].name==search)
                    {
                        cout <<"查找结果是:"<<hc[i]<<endl;
                        result=1;
                    }
                }
                if(result==0)
                {
                    cout <<"没有该信息!"<<endl;
                }
                cout <<endl;
            }
            else if(choice ==4)
            {
                int result=0;                    //用来判断是否找到
                string str;
                cout <<"请输入要查询的哈夫曼编码:";
                cin>>str;
                for(int i=1;i<=n;i++)
                {
                    if(str==hc[i])
                    {
                        cout <<"查找的结果是:"<<ht[i].name<<endl;
                        result=1;
                    }
                }
                if(result==0)
                {
                    cout <<"查找的编码不存在!"<<endl;
                }
                cout <<endl;
            }
            else if(choice==5)
            {
                cout <<"程序结束!"<<endl;
                break;
            }
            else
            {
                cout <<"无此功能!"<<endl;
            }
        }
    
        return 0;
    }
  • 相关阅读:
    关于C#静态构造函数的几点说明
    《JavaScript高级程序设计》读书笔记之一:几个与原始类型等价的引用类型的常用方法和属性
    Ajax 简介
    如何优化JavaScript脚本的性能
    摆脱混沌,建立个人能力体系——病症四起【from csdn】
    JMX理解与实例
    苹果电脑不为人所知的第三个创始人
    一些AS3中常用到的公式
    JavaScript 随笔
    解决IE6 JSONP无响应的问题。
  • 原文地址:https://www.cnblogs.com/dotacai/p/5017669.html
Copyright © 2011-2022 走看看