Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++. Also is it possible to tell me what you name the code file. And the comp

ID: 3708470 • Letter: I

Question

In C++.

Also is it possible to tell me what you name the code file. And the compile commands that you use to run it etc...

Thank you so much

read.cpp

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]){
    unsigned char ch;
    ifstream in(argv[1],ios::in|ios::binary|ios::ate);
    size_t size = 0; // here
   size = in.tellg() ; // get the length of the file
   cout << "Size of file: " << size << endl;
   in.seekg(0, ios::beg); // set the pointer to the beginning
    for (int i =0; i<size; i++) {
        ch = in.get();
        cout << (int) ch << endl;
    }
}

decode.cpp

#include <iostream>
#include <fstream>
using namespace std;
struct treenode {
    unsigned char data;
    treenode *left, *right;
};

int strchr(unsigned char s[], unsigned char c) {
    int i=0;
    while(s[i]!=c) i++;
    return i;
}

treenode * treebuild(unsigned char pre[], unsigned char in[], int & pos, int size) {
    if (size==pos) return NULL;
    treenode *t = new treenode;
    t->data = pre[pos]; t->left = NULL; t->right = NULL;
    int loc = strchr(in, pre[pos++]);
    in[loc] = 0;
    if (loc > 0 && in[loc-1] != 0)
        t->left = treebuild(pre, in, pos, size);
    if (loc < size-1 && in[loc+1] != 0)
        t->right = treebuild(pre,in,pos, size);
    return t;
}

void parse(string c, treenode *t) {
    treenode *p = t;
    for(int pos=0; pos < c.length(); pos++) {
        p = c[pos]=='0' ? p->left : p->right;
        if (p->left == NULL) {
            cout << p->data;
            p = t;
        }
    }
}

int main(int argc, char* argv[]) {
    ifstream prefile(argv[1],ios::in|ios::binary|ios::ate), infile(argv[2],ios::in|ios::binary), codefile(argv[3]);
    size_t size = 0; // here
   size = prefile.tellg() ; // get the length of the file
   prefile.seekg(0, ios::beg); // set the pointer to the beginning
    unsigned char *pre = new unsigned char[size], *in = new unsigned char[size];
    for (int i =0; i<size; i++) {
        pre[i] = prefile.get();
        in[i] = infile.get();
    }
    string code;
    getline(codefile,code);
    int pos = 0;
    treenode *tree = treebuild(pre,in,pos,size);
    parse(code,tree);
    cout << endl;
}
   

decodebin.cpp

#include <iostream>
#include <fstream>
using namespace std;
struct treenode {
    unsigned char data;
    treenode *left, *right;
};

int strchr(unsigned char s[], unsigned char c) {
    int i=0;
    while(s[i]!=c) i++;
    return i;
}

treenode * treebuild(unsigned char pre[], unsigned char in[], int & pos, int size) {
    if (size==pos) return NULL;
    treenode *t = new treenode;
    t->data = pre[pos]; t->left = NULL; t->right = NULL;
    int loc = strchr(in, pre[pos++]);
    in[loc] = 0;
    if (loc > 0 && in[loc-1] != 0)
        t->left = treebuild(pre, in, pos, size);
    if (loc < size-1 && in[loc+1] != 0)
        t->right = treebuild(pre,in,pos, size);
    return t;
}

void parse(string c, treenode *t,short count) {
    treenode *p = t;
    for(int pos=0; pos < c.length(); pos++) {
        p = c[pos]=='0' ? p->left : p->right;
        if (p->left == NULL) {
            cout << p->data;
            p = t;
            if(--count == 0) return;
        }
    }
}

int main(int argc, char* argv[]) {
    ifstream prefile(argv[1],ios::in|ios::binary|ios::ate), infile(argv[2],ios::in|ios::binary), codefile(argv[3],ios::in|ios::binary|ios::ate);
    size_t size = 0; // here
   size = prefile.tellg() ; // get the length of the file
   prefile.seekg(0, ios::beg); // set the pointer to the beginning
    unsigned char *pre = new unsigned char[size], *in = new unsigned char[size];
    for (int i =0; i<size; i++) {
        pre[i] = prefile.get();
        in[i] = infile.get();
    }
    int pos = 0;
    short count = 0;
    treenode *tree = treebuild(pre,in,pos,size);
    string code="";
   size = codefile.tellg() ; // get the length of the file
   codefile.seekg(0, ios::beg); // set the pointer to the beginning
    codefile.read(reinterpret_cast<char *>(&count),2);
    for (int i=0; i<size-2; i++) {
        unsigned char ch = codefile.get(), mask = 128;
        for (int j=0;j<8;j++) {
            code += (ch & mask)?'1':'0';
            mask >>=1;
        }
    }
    parse(code,tree,count);
    cout << endl;
}

Example Suppose that the file "foo.txt" contains the following text: ALLALABAMAFOOTBALL Huffman Code Trees Then executing: encode foo.txt should produce output flles such as the folloing. Note that this is only an example of a correct output. The tree and codes produced by your program would likely be different You are to write a program that reads a plain text file, computes a Huffman code tree for that text file, and writes out the encoded version ofthe text .Your program should read the text from the file given as a command-line . You should compute the number of occurrences of each character in the file, . You should build a min-heap contatntngthese nodes argument. and each character Cand lts frequency) should be placed In a new tree node preorder contains the bytes (note that these arethe VALUES of the bytes in the flle to see this output, run readon your file): 133 132 076 065 131 130 066 079 129 128 084 07O 077 Build the Huffman Code Tree using the heap. As you create neintnal nodes, give them a unique character label, using values 128 and greater. Write the pre order traversal of the Huffman code tree to the file "preorder" and the in-order traversal to the fle "inorder". Writethe byte value for each node in the traversal. The internal nodes will have values greater than 127 and the leaves will have values less thn 128. Write unsigned char values using the put(int) method. inorder contains the bytes(see note above): 076 132 065 133 066 130 079 131 084 128 070 129 077 code.txt contains: 010000010001100011110111011011011100100010000 .Construct a table contatning the encoding for each character, storlng the encoding as a string. .Encode the original text, writing the encoded verston to "code.txt". This flle should be ASCII 'O, and . 1' characters (much easier to debug) 5 honus points) also create a true binary version of the encoded text, writing it to "code.bin". In the btnary file, use the first two bytes to indicate the number of characters in the text. Ifthe last character does not finish a hyte then pad the last byte with 0's. Requirenents: . You should build all the data structures that you use yourself. You must create a binary heap data structure that uses an array and implements insert andexct-in that run in OUg N) time. . The only include files allowed are iostream and fstream Your makefile should build the executable namned "encode . Zip all ofyour source code and makefile into a single zip file for submission You must use good object based organization, ie. use classes in an appropriate way.

Explanation / Answer

#include<iostream>

#include<vector>

#include<string>

using namespace std;

struct node

{

node * leftChild;

node * rightChild;

double frequency;

string content;

string code;

};

vector<node> nodeArray;// Use nodeArray to record all the nodes that may be created in the whole process

node extractMin()

{

double temp = (double) INT_MAX;

vector<node>::iterator i1,pos;

for(i1 = nodeArray.begin();i1!=nodeArray.end();i1++)

{

  

if(temp>(*i1).frequency)

{

pos = i1;

temp = (*i1).frequency;

}

}

node tempNode = (*pos);

nodeArray.erase(pos);

return tempNode;

}

node getHuffmanTree()

{

while(!nodeArray.empty())

{

  

  

node * tempNode = new node;

node * tempNode1 = new node;

node * tempNode2 = new node;

*tempNode1 = extractMin();

*tempNode2 = extractMin();

  

tempNode->leftChild = tempNode1;

tempNode->rightChild = tempNode2;

tempNode->frequency = tempNode1->frequency+tempNode2->frequency;

nodeArray.push_back(*tempNode);

if(nodeArray.size() == 1)//only the root node exsits

{

break;

}

}

return nodeArray[0];

}

void BFS(node * temproot,string s)

{

node * root1 = new node;

root1 = temproot;

  

root1->code = s;

if(root1 == NULL)

{

  

}

else if(root1->leftChild == NULL && root1->rightChild == NULL)

{

  

cout<<"the content is "<<root1->content<<endl;

cout<<"and its corresponding code is "<<root1->code<<endl;

}

else

{

root1->leftChild->code = s.append("0");

s.erase(s.end()-1);

root1->rightChild->code = s.append("1");

s.erase(s.end()-1);

BFS(root1->leftChild,s.append("0"));

s.erase(s.end()-1);

BFS(root1->rightChild,s.append("1"));

s.erase(s.end()-1);

}

}

void getHuffmanCode()

{

int size,i;

double tempDouble;

string tempString = "";

cout<<"please input the number of things you want to encode!"<<endl;

cin>>size;

for(i = 0;i<size;i++)

{

cout<<"please input the things you want to encoded and their frequencies!"<<endl;

node tempNode;

cin>>tempString;

cin>>tempDouble;

tempNode.frequency = tempDouble;

tempNode.content = tempString;

tempNode.leftChild = NULL;

tempNode.rightChild = NULL;

nodeArray.push_back(tempNode);

}

node root = getHuffmanTree();

BFS(&root,"");

}

int main()

{

vector<int> test;

test.push_back(1);

test.push_back(2);

test.push_back(3);

test.push_back(4);

vector<int>::iterator i1 = test.begin();

test.erase(i1);

  

  

getHuffmanCode();

return 0;

}