my code stock.com

Hpp file

Snippet options

Download: Download snippet as utils.c.
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!

/*
Huffman coding is one of the basic memory saving technique. Every value has its individual ASCII value, and for each ASCII value computer allocated 8bits of space. So, every char, number, symbol…. Consumes 8bit of space. As Huffman coding says we can virtually allocate new frequency value to those char, number, symbol…. And via with we can give them new binary space in computer. Likewise, Hoffman follows binary tree to store each character. The ASCII code for any character is obtained by following the root-to-leaf path and contains the 0's and 1's.  Let us take an example where frequency of ‘s’ is more, and ‘z’ is least so we setup our tree in such a way that it stores ‘z’ in the end of tree and ‘s’ is the root as it is most used letter. We assign 1’s if program goes right in binary tree to find the letter and 0’s if program goes left in binary tree. It stores value temporarily until it reaches the desire letter and assign the path value i.e. 0’s and 1’s it took to reach that letter. As ‘z’ is least common word, so it may be stores in 110011010101 wish occupies 12 bits but ‘a’ be common word which is stores in 10 so if you see it consumed 2 bit less memory that it use to. To encode the letter, I pass those to check the frequency and accordance to frequency of those letter I assign them in binary tree. Build a table of per-character encodings. Let us take a table e.g., an ASCII table, or you may build the table from a Huffman coding tree or as we have used Hash Table to store the value in linked list. So, according to table we have built we store the letter. While each letter is in different position it has its own unique binary value. While an essay may consume 20000bits without using Huffman coding but if we store data using Huffman coding and compress and decompress accordingly. It may consume maybe alot less than 20000 bits. I have used priority queue to store heap tree, with respect to their heap root node value. I have iterated through frequency to store or push it into minheap which a priority queue is. It compares the frequency and store the value in hash tree. It pops out the most common letter towards root, and priority que is assigning by comparing the frequency of the letter. Then I store the code, if it goes right code assign 1 if node goes left code assign it 0. So, at end every letter has its own unique binary value. While decoding It goes through the tree and sees if the node is in right or left and assign value accordingly. Somehow using Huffman coding seems to be useful for our computer memory.

Huffman coding is the basic concept of modern day more complex, more advance compression. 

 */



#ifndef Utils_hpp
#define Utils_hpp
#include <stdio.h>
#include "stdc++.h"
#include <map>
#include <iostream>
#define MAX_TREE_HT 256
using namespace std;
map<char, string> codes;
map<char, int> freq;
struct HuffNode{
    char data; 
    int freq;
    HuffNode *left, *right;
    HuffNode(char l, int f)
    {
        left = right = NULL;
        data = l;
        freq = f;
    }
};





class Utils {
    
public:
   
    Utils();
    ~Utils();
  //  void printCodes(struct HuffNode* root, string str);
  void storeCodes(struct HuffNode* root, string str);
    void HuffmanCodes(int size);
  string decode_file(struct HuffNode* root,string s);
  //  void huffDecode();
  void computeFrequency(string str,int n);
  // string search(ZippedBookNode * head, string x);
  // HuffNode *search2(ZippedBookNode* head, string x);
  
  
    
    
};

#endif /* Utils_hpp */

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.