[ 数据结构实验 ] Huffman树

杯具……

代码
#include <iostream>
#include 
<fstream>
#include 
<cstdlib>
using namespace std;

class Huffman{
private:
    
struct __HuffmanNode{
        typedef __HuffmanNode node;

        unsigned frequency;
        unsigned idx;
        
char code;
        node 
*left;
        node 
*right;
        node 
*father;

        __HuffmanNode(){
            left 
= NULL;
            right 
= NULL;
        }
        __HuffmanNode(unsigned f, node 
*l, node *r) : frequency(f){
            left 
= l;
            right 
= r;
        }

        
bool operator < (const __HuffmanNode& x) const{
            
return frequency < x.frequency;
        }
    };

    typedef __HuffmanNode node;
    
const static unsigned MAXN = 129;
    
const static unsigned INF = (1 << 31);

    ifstream fin;
    ofstream fout;

    node lef[MAXN];
    node inn[MAXN];
    node 
*udx[MAXN];
    node 
*head;

    
void __init(){
        unsigned i;
        
for(i = 0; i < MAXN; i++){
            udx[i] 
= &lef[i];
            lef[i].code 
= i;
            lef[i].idx 
= i;
            lef[i].frequency 
= 0;
            lef[i].left 
= NULL;
            lef[i].right 
= NULL;
            inn[i].frequency 
= INF;
        }
    }
    
void __load(){
        
char buff;

        
while(fin.get(buff)){
            lef[buff].frequency 
++;
        }
        fin.close();
    }
    
void __display(unsigned depth, node *now, char* __code){
        
if(now->left == NULL && now->right == NULL){
            unsigned i;
            fout 
<< now->code << " : " << now->frequency << " ";
            
for(i = 0; i < depth; i++) fout << __code[i];
            fout 
<< endl;
        }
        
else{
            
if(now->left){
                __code[depth] 
= '0';
                __display(depth 
+ 1, now->left, __code);
            }
            
if(now->right){
                __code[depth] 
= '1';
                __display(depth 
+ 1, now->right, __code);
            }
        }
    }
    
void __decode(node* now){
        
char buff;
        
if(now->left == NULL && now->right == NULL){
            fout.put(now
->code);
        }
        
else{
            fin.
get(buff);
            
if(buff == '0') __decode(now->left);
            
else __decode(now->right);
        }
    }
    
void __encode(){
        
char buff;
        
char __stack[MAXN];
        
int i;
        node 
*p;
        
while(fin.get(buff)){
            
for(i = 0, p = udx[buff]; p->father; p = p->father, i++){
                
if(p->father->left != NULL && p->father->left == p){
                    __stack[i] 
= '0';
                }
                
else{
                    __stack[i] 
= '1';
                }
            }
            
for(i --; i >= 0; i--){
                fout 
<< __stack[i];
            }
        }
    }
    
void __make(){
        unsigned i, j, k;
        node 
*l;
        node 
*r;

        sort(lef, lef 
+ MAXN);
        
for(i = 0; i < MAXN; i++){
            udx[lef[i].idx] 
= &lef[i];
        }
        
for(i = j = k = 0; k < MAXN - 1; k ++){
            
if(i == MAXN || lef[i].frequency > inn[j].frequency){
                l 
= &inn[j];
                j 
++;
            }
            
else{
                l 
= &lef[i];
                i 
++;
            }
            
if(i == MAXN || lef[i].frequency > inn[j].frequency){
                r 
= &inn[j];
                j 
++;
            }
            
else{
                r 
= &lef[i];
                i 
++;
            }

            inn[k].frequency 
= l->frequency + r->frequency;
            inn[k].left 
= l;
            inn[k].right 
= r;
            l
->father = &inn[k];
            r
->father = &inn[k];
        }
        inn[k 
- 1].father = NULL;
        head 
= &inn[k - 1];
    }
public:
    typedef __HuffmanNode node;

    Huffman(){}

    
void decode(){
        
//const char infilename[] = "o.dat";
        
//const char outfilename[] = "ff.dat";
        char infilename[1024];
        
char outfilename[1024];

        system(
"cls");
        cout 
<< "in filename : "; cin >> infilename;
        cout 
<< "out filename : "; cin >> outfilename;
        fin.open(infilename, ifstream :: 
in);
        fout.open(outfilename, ofstream :: 
out);

        
while(!fin.eof()) __decode(head);

        fin.close();
        fout.close();
        fin.clear();
        fout.clear();
    }
    
void encode(){
        
//const char infilename[] = "dict.dat";
        
//const char outfilename[] = "o.dat";
        char infilename[1024];
        
char outfilename[1024];

        system(
"cls");
        cout 
<< "in filename : "; cin >> infilename;
        cout 
<< "out filename : "; cin >> outfilename;

        fin.open(infilename, fstream :: 
in);
        fout.open(outfilename, fstream :: 
out);

        __encode();

        fout.close();
        fin.close();
        fout.clear();
        fin.clear();
    }
    
void make(){
        
//const char dict_filename[] = "dict.dat";
        char dict_filename[1024];

        system(
"cls");
        cout 
<< "dictionary filename : "; cin >> dict_filename;

        fin.open(dict_filename, ifstream :: 
in);
        __init();
        __load();
        __make();
        fin.close();
        fin.clear();
    }
    
void display(){
        
const char tab_filename[] = "tab.dat";
        
//char tab_filename[1024];
        char __code[MAXN];

        system(
"cls");
        
//cout << "tab filename : "; cin >> tab_filename;

        fout.open(tab_filename, ofstream :: 
out);
        __display(
0, head, __code);
        fout.close();
        fout.clear();
    }
    
void process(){
        
char ins[8];
        
while(1)
        {
            system(
"cls");
            cout 
<< "1 . make" << endl;
            cout 
<< "2 . encode" << endl;
            cout 
<< "3 . decode" << endl;
            cout 
<< "4 . display" << endl;
            cout 
<< "5 . quit" << endl;
            cout 
<< "1/2/3/4/5" << endl; cin >> ins;

            
switch (ins[0]){
                
case '1':
                    make();
                    
break;
                
case '2':
                    encode();
                    
break;
                
case '3':
                    decode();
                    
break;
                
case '4':
                    display();
                    
break;
                
case '5':
                    
return;
            }
            system(
"pause");
        }
    }
};

int main(){
    Huffman one;
    one.process();
    
return 0;
}


 

posted @ 2010-04-21 12:38  刘一佳  阅读(163)  评论(0编辑  收藏  举报