Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void encodeFile(istream& infile, Node* encodingTree, obstream& outfile) {
- char ch;
- while(infile.get(ch)){
- string str="";
- string code=codeMaker(ch,encodingTree,str);
- // if(code.length()==0){
- // error("Your character doesnt exist");
- // }
- for(int i=0; i<code.length(); i++){
- int a=code[i]-'0';
- outfile.writeBit(a);
- }
- }
- string str="";
- string code=codeMaker(PSEUDO_EOF,encodingTree,str);
- for(int i=0; i<code.length(); i++){
- int a=code[i]-'0';
- outfile.writeBit(a);
- }
- }
- string codeMaker(ext_char ch, Node * encodingTree,string str){
- Node* first=encodingTree->one;
- Node* second=encodingTree->zero;
- if(first!=NULL && first->character==ch){
- return str+'1';
- }
- if(second!=NULL && second->character==ch){
- return str+'0';
- }
- if(first!=NULL && first->character==NOT_A_CHAR && charExists(ch,first)){
- return codeMaker(ch,first,str+'1');
- }
- if(second!=NULL && second->character==NOT_A_CHAR && charExists(ch,second)){
- return codeMaker(ch,second,str+'0');
- }
- return "";
- }
- bool charExists(char ch, Node * tree){
- Node * first=tree->one;
- Node * second=tree->zero;
- if((first==NULL && second==NULL) || ((first!=NULL && first->character!=NOT_A_CHAR && first->character!=ch) && (second!=NULL &&
- second->character!=NOT_A_CHAR && second->character!=ch))){
- return false;
- }
- if(first!=NULL && second!=NULL && (first->character==ch || second->character==ch)){
- return true;
- }else{
- if(first!=NULL && first->character==ch){
- return true;
- }
- if(second !=NULL && second->character==ch){
- return true;
- }
- }
- return charExists(ch,first) || charExists(ch,second);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement