hufftree.h

来自「Huffman编码及译码 具体的要求: 1.将给定字符文件编码:生成编码」· C头文件 代码 · 共 44 行

H
44
字号
//霍夫曼树类
#ifndef HUFFTREE_H
#define HUFFTREE_H

class HuffTree{
private:
	HuffNode* theRoot;
public:
	HuffTree(){}
	~HuffTree(){
	}
    void addleaf(char &val,int freq){
              theRoot=new LeafNode(val,freq);	
	}
	HuffTree(HuffTree* l,HuffTree* r){
		theRoot=new IntlNode(l->root(),r->root());
	}
	HuffNode* root(){return theRoot;}
	int weight(){return theRoot->weight();}
	void traverse(HuffNode *subroot){
	    if(subroot==NULL)return;
		cout<<subroot->weight()<<"  ";
		traverse(subroot->left());
		traverse(subroot->right());	
	}


void encode(HuffNode *subroot,huffcode *hcode,int& hcodecount,char*path,int &pathcount){
    if(subroot->isLeaf()){
			path[pathcount]=NULL;
			strcpy(hcode[hcodecount].code,path);
                        hcode[hcodecount++].letter=((LeafNode*)subroot)->val();
	        return;
	}
	path[pathcount++]='0';		
	encode(subroot->left(),hcode,hcodecount,path,pathcount);
	pathcount--;
	path[pathcount++]='1';
	encode(subroot->right(),hcode,hcodecount,path,pathcount);
    pathcount--;
} 

};
#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?