92.7k views
4 votes
Please help me with my assigment

In the given code we are using two classes and two functions. The first class HeapNode_Min will consist of data members and a constructor. The second class Analyze will consist of a function which will compare two heap nodes and will return the result. We are also using display function to print the codes of Huffman tree from the root. HCodes function is used to build a Huffman tree, this function is using two while loops and at the end it calls display_Codes function. In main function we are using two arrays, one for frequency and the second one for alphabets. We are using size_of variable to store the size of data types after their division. At the end of the main function we will call HCodes function.

#include

using namespace std;



class HeapNode_Min { // Tree node of Huffman


public:


//Add data members here.


HeapNode_Min(char d, unsigned f)

{

//Complete the body of HeapNode_Min function

}

};


class Analyze { // two heap nodes comparison


public:

bool operator()(HeapNode_Min* l, HeapNode_Min* r)

{

(l->f > r->f); //Complete this statement

}

};


void display_Codes(HeapNode_Min* root, string s) // To print codes of huffman tree from the root.

{

if (!root)

return;


if (root->d != '$')

cout << root->d << "\t: " << s << "\\";


display_Codes(root->l, s + "0");

display_Codes( ); //Complete this statement by passing arguments


}



void HCodes(char data[], int freq[], int s) // builds a Huffman Tree

{

HeapNode_Min *t,*r, *l ; // top, right, left



priority_queue, Analyze> H_min;


int a=0;

while (a



while (H_min.size() != 1) {


l = H_min.top(); H_min.pop();

r = H_min.top(); H_min.pop();


t = new HeapNode_Min('$', r->f + l->f);


t->r = r; t->l = l;



H_min.push(t);

}


display_Codes(H_min.top(), "");

}


int main()

{

int frequency[] = { 3, 6, 11, 14, 18, 25 }; char alphabet[] = { 'A', 'L', 'O', 'R', 'T', 'Y' };

int size_of = sizeof() / sizeof(); //Complete this statement by passing data type to both sizeof operators


cout<<"Alphabet"<<":"<<"Huffman Code\\";

cout<<"--------------------------------\\";


//Call Huffman_Codes function.


return 0;

User Jourkey
by
3.7k points

1 Answer

2 votes

Answer:

#include <iostream>

#include <queue>

using namespace std;

class HeapNode_Min

{

public:

char d;

unsigned f;

HeapNode_Min* l;

HeapNode_Min* r;

HeapNode_Min(char d, unsigned f)

{

this->l = this->r = NULL;

this->d = d;

this->f = f;

}

};

class Analyze {

public:

bool operator()(HeapNode_Min* l, HeapNode_Min* r)

{

return (l->f > r->f);

}

};

void display_Codes(HeapNode_Min* root, string s)

{

if (!root)

return;

if (root->d != '$')

cout << root->d << "\t: " << s << "\\";

display_Codes(root->l, s + "0");

display_Codes(root->r, s + "1");

}

void HCodes(char data[], int freq[], int s) // builds a Huffman Tree

{

HeapNode_Min* t, *r, *l; // top, right, left

priority_queue<HeapNode_Min*, vector<HeapNode_Min*>, Analyze> H_min;

int a = 0;

while (a < s) {

H_min.push(new HeapNode_Min(data[a], freq[a]));

a++;

}

while (H_min.size() != 1)

{

l = H_min.top();

H_min.pop();

r = H_min.top();

H_min.pop();

t = new HeapNode_Min('$', r->f + l->f);

t->r = r;

t->l = l;

H_min.push(t);

}

display_Codes(H_min.top(), "");

}

int main()

{

int frequency[] = { 3, 6, 11, 14, 18, 25 };

char alphabet[] = { 'A', 'L', 'O', 'R', 'T', 'Y' };

int size_of = sizeof(alphabet) / sizeof(char); //Complete this statement by passing data type to both sizeof operators

cout << "Alphabet" << ":" << "Huffman Code\\";

cout << "--------------------------------\\";

HCodes(alphabet, frequency, size_of);

return 0;

}

Step-by-step explanation:

The expected output is:

Alphabet:Huffman Code

--------------------------------

R : 00

T : 01

A : 1000

L : 1001

O : 101

Y : 11

User CascadiaJS
by
3.3k points