my code stock.com

Snippet options

Download: Download snippet as utilscpp.cpp.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

//
//  Utils.cpp
//
//


#include "Utils.hpp"
#include "New.hpp"
using namespace std;
Utils::Utils(){
    
}

Utils::~Utils(){
}
//the comparator returns a bool, it doesn't care about the
//relationship betweeen two entities, like who's bigger.
//If the returned value is true, the new entity will be placed towards the end..
struct compare{
    bool operator()(HuffNode* l, HuffNode* r)
    {
        return (l->freq > r->freq);
    }
};


// utility function to store characters along with
// there huffman value in a hash table, here we
// have C++  map
void Utils:: storeCodes(struct HuffNode* root, string str)
{
    if (root==nullptr)
        return;
       if (root->data)
    //   if (root->data)
        codes[root->data]=str;
    storeCodes(root->left, str + "0");
    storeCodes(root->right, str + "1");
}

//  priority queue to store heap tree, with respect  to their heap root node value
priority_queue<HuffNode*, vector<HuffNode*>, compare> minHeap;

// function to build the Huffman tree and store it  in minHeap
void Utils::  HuffmanCodes(int size){
    struct HuffNode *left, *right, *top;
    for (map<char, int>::iterator v=freq.begin(); v!=freq.end(); v++)
        minHeap.push(new HuffNode(v->first, v->second));
    while (minHeap.size() != 1)
    {
        left = minHeap.top();
        minHeap.pop();
        right = minHeap.top();
        minHeap.pop();
        top = new HuffNode(NULL, left->freq + right->freq);
        top->left = left;
        top->right = right;
        minHeap.push(top);
    }
    storeCodes(minHeap.top(),"");
}

// utility function to store map each character with its  frequency in input string
void Utils:: computeFrequency(string str, int n)
{
    for (int i=0; i<str.size(); i++)
        freq[str[i]]++;
}

// function iterates through the encoded string s
// if s[i]=='1' then move to node->right
// if s[i]=='0' then move to node->left
// if leaf node append the node->data to our output string
string Utils:: decode_file(struct HuffNode* root, string s)
{
    string ans = "";
    struct HuffNode* curr = root;
    for (int i=0;i<s.size();i++)
    {
        if (s[i] == '0')
            curr = curr->left;
        else
            curr = curr->right;
        
        // reached leaf node
        if (curr->left==NULL and curr->right==NULL)
        {
            ans += curr->data;
            curr = root;
        }
    }
    return ans+'\0';
}


//HashTable sum may be same for some title, so it is saved as linkedlist. To acess the linked list, or to access the perticular
//title that is stored in same hashtable, I have used this seach function which returns me the encoded string i.e 1's and 0's
// and return the root of hashnode of that particular title
string NewHash ::search(ZippedBookNode * head, string x)
{
    ZippedBookNode * current = head;
    while (current != NULL)
    {
        if (current->title == x)
            return current->c_excerpt;
        current = current->next;
    }
    cout<<"No Title"<<endl;
}


HuffNode* NewHash::search2(ZippedBookNode * head, string x)
{
    ZippedBookNode * current = head;
    while (current != NULL)
    {
        if (current->title == x)
            return current->huff_root;
        current = current->next;
    }
    cout<<"No Title"<<endl;
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.