Question

In: Computer Science

Please if you are able to answer the question below: Write C++ program using native C++...

Please if you are able to answer the question below:

Write C++ program using native C++ (you can use STL)  that produces Huffman code for a string of text entered by the user.  Must accept all ASCII characters.  Pleas explain how you got frequencies of characters, how you sorted them, how you got codes.

Solutions

Expert Solution

Character ASCII Value ASCII Binary Huffman Binary
' ' 32 00100000 10
'a' 97 01100001 0001
'b' 98 01100010 0111010
'c' 99 01100011 001100
'e' 101 01100101 1100
'z' 122 01111010 00100011010

#include <bits/stdc++.h>

#include <stdlib.h>

// This constant can be avoided by explicitly

// calculating height of Huffman Tree

#define MAX_TREE_HT 100

// A Huffman tree node

struct MinHeapNode {

    // One of the input characters

    char data;

    // Frequency of the character

    unsigned freq;

    // Left and right child of this node

    struct MinHeapNode *left, *right;

};

// A Min Heap: Collection of

// min-heap (or Huffman tree) nodes

struct MinHeap {

    // Current size of min heap

    unsigned size;

    // capacity of min heap

    unsigned capacity;

    // Array of minheap node pointers

    struct MinHeapNode** array;

};

// A utility function allocate a new

// min heap node with given character

// and frequency of the character

struct MinHeapNode* newNode(char data, unsigned freq)

{

    struct MinHeapNode* temp

        = (struct MinHeapNode*)malloc

(sizeof(struct MinHeapNode));

    temp->left = temp->right = NULL;

    temp->data = data;

    temp->freq = freq;

    return temp;

}

// A utility function to create

// a min heap of given capacity

struct MinHeap* createMinHeap(unsigned capacity)

{

    struct MinHeap* minHeap

        = (struct MinHeap*)malloc(sizeof(struct MinHeap));

    // current size is 0

    minHeap->size = 0;

    minHeap->capacity = capacity;

    minHeap->array

        = (struct MinHeapNode**)malloc(minHeap->

capacity * sizeof(struct MinHeapNode*));

    return minHeap;

}

// A utility function to

// swap two min heap nodes

void swapMinHeapNode(struct MinHeapNode** a,

                     struct MinHeapNode** b)

{

    struct MinHeapNode* t = *a;

    *a = *b;

    *b = t;

}

// The standard minHeapify function.

void minHeapify(struct MinHeap* minHeap, int idx)

{

    int smallest = idx;

    int left = 2 * idx + 1;

    int right = 2 * idx + 2;

    if (left < minHeap->size && minHeap->array[left]->

freq < minHeap->array[smallest]->freq)

        smallest = left;

    if (right < minHeap->size && minHeap->array[right]->

freq < minHeap->array[smallest]->freq)

        smallest = right;

    if (smallest != idx) {

        swapMinHeapNode(&minHeap->array[smallest],

                        &minHeap->array[idx]);

        minHeapify(minHeap, smallest);

    }

}

// A utility function to check

// if size of heap is 1 or not

int isSizeOne(struct MinHeap* minHeap)

{

    return (minHeap->size == 1);

}

// A standard function to extract

// minimum value node from heap

struct MinHeapNode* extractMin(struct MinHeap* minHeap)

{

    struct MinHeapNode* temp = minHeap->array[0];

    minHeap->array[0]

        = minHeap->array[minHeap->size - 1];

    --minHeap->size;

    minHeapify(minHeap, 0);

    return temp;

}

// A utility function to insert

// a new node to Min Heap

void insertMinHeap(struct MinHeap* minHeap,

                   struct MinHeapNode* minHeapNode)

{

    ++minHeap->size;

    int i = minHeap->size - 1;

    while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {

        minHeap->array[i] = minHeap->array[(i - 1) / 2];

        i = (i - 1) / 2;

    }

    minHeap->array[i] = minHeapNode;

}

// A standard function to build min heap

void buildMinHeap(struct MinHeap* minHeap)

{

    int n = minHeap->size - 1;

    int i;

    for (i = (n - 1) / 2; i >= 0; --i)

        minHeapify(minHeap, i);

}

// A utility function to print an array of size n

void printArr(int arr[], int n)

{

    int i;

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

        printf("%d", arr[i]);

    printf("\n");

}

// Utility function to check if this node is leaf

int isLeaf(struct MinHeapNode* root)

{

    return !(root->left) && !(root->right);

}

// Creates a min heap of capacity

// equal to size and inserts all character of

// data[] in min heap. Initially size of

// min heap is equal to capacity

struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size)

{

    struct MinHeap* minHeap = createMinHeap(size);

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

        minHeap->array[i] = newNode(data[i], freq[i]);

    minHeap->size = size;

    buildMinHeap(minHeap);

    return minHeap;

}

// The main function that builds Huffman tree

struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size)

{

    struct MinHeapNode *left, *right, *top;

    // Step 1: Create a min heap of capacity

    // equal to size. Initially, there are

    // modes equal to size.

    struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);

    // Iterate while size of heap doesn't become 1

    while (!isSizeOne(minHeap)) {

        // Step 2: Extract the two minimum

        // freq items from min heap

        left = extractMin(minHeap);

        right = extractMin(minHeap);

        // Step 3: Create a new internal

        // node with frequency equal to the

        // sum of the two nodes frequencies.

        // Make the two extracted node as

        // left and right children of this new node.

        // Add this node to the min heap

        // '$' is a special value for internal nodes, not used

        top = newNode('$', left->freq + right->freq);

        top->left = left;

        top->right = right;

        insertMinHeap(minHeap, top);

    }

    // Step 4: The remaining node is the

    // root node and the tree is complete.

    return extractMin(minHeap);

}

// Prints huffman codes from the root of Huffman Tree.

// It uses arr[] to store codes

void printCodes(struct MinHeapNode* root, int arr[], int top)

{

    // Assign 0 to left edge and recur

    if (root->left) {

        arr[top] = 0;

        printCodes(root->left, arr, top + 1);

    }

    // Assign 1 to right edge and recur

    if (root->right) {

        arr[top] = 1;

        printCodes(root->right, arr, top + 1);

    }

    // If this is a leaf node, then

    // it contains one of the input

    // characters, print the character

    // and its code from arr[]

    if (isLeaf(root)) {

        printf("%c: ", root->data);

        printArr(arr, top);

    }

}

// The main function that builds a

// Huffman Tree and print codes by traversing

// the built Huffman Tree

void HuffmanCodes(char data[], int freq[], int size)

{

    // Construct Huffman Tree

    struct MinHeapNode* root

        = buildHuffmanTree(data, freq, size);

    // Print Huffman codes using

    // the Huffman tree built above

    int arr[MAX_TREE_HT], top = 0;

    printCodes(root, arr, top);

}

// Driver program to test above functions

int main()

{

    char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };

    int freq[] = { 5, 9, 12, 13, 16, 45 };

    int size = sizeof(arr) / sizeof(arr[0]);

    HuffmanCodes(arr, freq, size);

    return 0;

}


Related Solutions

use linux or c program. please provide the answer in details. Write a program that will...
use linux or c program. please provide the answer in details. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement for a salesperson at a rate of $0.35 per mile. Your program should interact (ask the user to enter the data) with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading > 13505.2 Enter ending odometer reading > 13810.6 You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89. ** Extra credit 6 points: Format the answer (2 points),...
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Create a C++ program using native C++ (STL is acceptable) that produces Huffman code for a...
Create a C++ program using native C++ (STL is acceptable) that produces Huffman code for a string of text entered by the user. Must accept all ASCII characters. Provide an explanation for how you got frequencies of characters, how you sorted them, and how you got codes.
Hi. Please could I get a complete answer to the question below using c++ and codeblocks...
Hi. Please could I get a complete answer to the question below using c++ and codeblocks Add a recursive member function to bSearchTreeType that returns the depth of a given node whose info member contains a specific value, and returns -1 if the node is not in the tree. For example, tree in figure 11-8 on page 622 of Malik,z the depth of 40 is 4, the depth of node 50 is 1 etc. Use the following header:    template<class...
Question 1: Using the following information please to answer the question below. You are reviewing your...
Question 1: Using the following information please to answer the question below. You are reviewing your clients (Based in United Kingdom) hedging strategy for the cash flows it expects to obtain from sales in the US during the calendar year 2019. Revenue: $90,000 Cost of sales: £54,000 (Expenses are billed in GBP) Exchange Rate Bid Ask Spot rate (Scenario 1) Bid: $1.45/£ Ask: $1.465/£ Spot rate (Scenario 2) Bid:$0.88/£ Ask: $0.9/£ Options contact: Option Premium £0.025 Option Strike price £0.922...
Write an assembly language program that corresponds to the following C program ****Please give correct answer...
Write an assembly language program that corresponds to the following C program ****Please give correct answer using Pep/9 machine**** int num1; int num2; ;int main () { scanf("%d", &num1); num2 = -num1; printf("num1 = %d\n", num1); printf("num2 = %d\n", num2); return 0; }
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT