SlideShare a Scribd company logo
Write a program to decipher messages encoded using a prefix code, given the encoding tree.
Such codes are widely used in applications that compress data, including JPEG for images, MP3
for music, and DivX for video. using Huffman Trees (Prefix Binary Trees) c++
Solution
#include #include #include #include // for CHAR_BIT #include #include const int
UniqueSymbols = 1 << CHAR_BIT; const char* SampleString = "this is an example for
huffman encoding"; typedef std::vector HuffCode; typedef std::map HuffCodeMap; class
INode { public: const int f; virtual ~INode() {} protected: INode(int f) : f(f) {} };
class InternalNode : public INode { public: INode *const left; INode *const right;
InternalNode(INode* c0, INode* c1) : INode(c0->f + c1->f), left(c0), right(c1) {}
~InternalNode() { delete left; delete right; } }; class LeafNode :
public INode { public: const char c; LeafNode(int f, char c) : INode(f), c(c) {} };
struct NodeCmp { bool operator()(const INode* lhs, const INode* rhs) const { return lhs->f
> rhs->f; } }; INode* BuildTree(const int (&frequencies)[UniqueSymbols]) {
std::priority_queue, NodeCmp> trees; for (int i = 0; i < UniqueSymbols; ++i) {
if(frequencies[i] != 0) trees.push(new LeafNode(frequencies[i], (char)i)); }
while (trees.size() > 1) { INode* childR = trees.top(); trees.pop();
INode* childL = trees.top(); trees.pop(); INode* parent = new
InternalNode(childR, childL); trees.push(parent); } return trees.top(); } void
GenerateCodes(const INode* node, const HuffCode& prefix, HuffCodeMap& outCodes) {
if (const LeafNode* lf = dynamic_cast(node)) { outCodes[lf->c] = prefix; }
else if (const InternalNode* in = dynamic_cast(node)) { HuffCode leftPrefix =
prefix; leftPrefix.push_back(false); GenerateCodes(in->left, leftPrefix,
outCodes); HuffCode rightPrefix = prefix; rightPrefix.push_back(true);
GenerateCodes(in->right, rightPrefix, outCodes); } } int main() { // Build
frequency table int frequencies[UniqueSymbols] = {0}; const char* ptr =
SampleString; while (*ptr != '0') ++frequencies[*ptr++]; INode* root =
BuildTree(frequencies); HuffCodeMap codes; GenerateCodes(root, HuffCode(),
codes); delete root; for (HuffCodeMap::const_iterator it = codes.begin(); it !=
codes.end(); ++it) { std::cout << it->first << " "; std::copy(it-
>second.begin(), it->second.end(), std::ostream_iterator(std::cout)); std::cout
<< std::endl; } return 0; }

More Related Content

Similar to Write a program to decipher messages encoded using a prefix code, gi.pdf

Automatically Describing Program Structure and Behavior (PhD Defense)
Automatically Describing Program Structure and Behavior (PhD Defense)Automatically Describing Program Structure and Behavior (PhD Defense)
Automatically Describing Program Structure and Behavior (PhD Defense)Ray Buse
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
Cquestions
Cquestions Cquestions
Cquestions
mohamed sikander
 
20-13 Programming Project #05A Given the IntNode class- define the Fin.docx
20-13 Programming Project #05A Given the IntNode class- define the Fin.docx20-13 Programming Project #05A Given the IntNode class- define the Fin.docx
20-13 Programming Project #05A Given the IntNode class- define the Fin.docx
DavidxyNSimpsons
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting StartedHadziq Fabroyir
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
Atul Setu
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)bolovv
 
Point.h header file #ifndef POINT_H #define POINT_H Po.pdf
Point.h header file #ifndef POINT_H #define POINT_H Po.pdfPoint.h header file #ifndef POINT_H #define POINT_H Po.pdf
Point.h header file #ifndef POINT_H #define POINT_H Po.pdf
ANGELMARKETINGJAIPUR
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
Andrey Karpov
 
Arduino coding class part ii
Arduino coding class part iiArduino coding class part ii
Arduino coding class part ii
Jonah Marrs
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
KamalSaini561034
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)mrecedu
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
Matt Provost
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)bolovv
 

Similar to Write a program to decipher messages encoded using a prefix code, gi.pdf (20)

Automatically Describing Program Structure and Behavior (PhD Defense)
Automatically Describing Program Structure and Behavior (PhD Defense)Automatically Describing Program Structure and Behavior (PhD Defense)
Automatically Describing Program Structure and Behavior (PhD Defense)
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Cquestions
Cquestions Cquestions
Cquestions
 
20-13 Programming Project #05A Given the IntNode class- define the Fin.docx
20-13 Programming Project #05A Given the IntNode class- define the Fin.docx20-13 Programming Project #05A Given the IntNode class- define the Fin.docx
20-13 Programming Project #05A Given the IntNode class- define the Fin.docx
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started
 
Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
Point.h header file #ifndef POINT_H #define POINT_H Po.pdf
Point.h header file #ifndef POINT_H #define POINT_H Po.pdfPoint.h header file #ifndef POINT_H #define POINT_H Po.pdf
Point.h header file #ifndef POINT_H #define POINT_H Po.pdf
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
 
Arduino coding class part ii
Arduino coding class part iiArduino coding class part ii
Arduino coding class part ii
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)
 

More from footworld1

Describe 3 functions of the cellular membrane and how are these func.pdf
Describe 3 functions of the cellular membrane and how are these func.pdfDescribe 3 functions of the cellular membrane and how are these func.pdf
Describe 3 functions of the cellular membrane and how are these func.pdf
footworld1
 
Discuss the three main types of intellectual capitalSolutionth.pdf
Discuss the three main types of intellectual capitalSolutionth.pdfDiscuss the three main types of intellectual capitalSolutionth.pdf
Discuss the three main types of intellectual capitalSolutionth.pdf
footworld1
 
Describer in detail the innate immune response to both an intercellu.pdf
Describer in detail the innate immune response to both an intercellu.pdfDescriber in detail the innate immune response to both an intercellu.pdf
Describer in detail the innate immune response to both an intercellu.pdf
footworld1
 
Describe the difference between tropic and non-tropic hormones.S.pdf
Describe the difference between tropic and non-tropic hormones.S.pdfDescribe the difference between tropic and non-tropic hormones.S.pdf
Describe the difference between tropic and non-tropic hormones.S.pdf
footworld1
 
C++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdf
C++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdfC++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdf
C++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdf
footworld1
 
Can someone explain to me what the learning curve equation (Tn=T1n^.pdf
Can someone explain to me what the learning curve equation (Tn=T1n^.pdfCan someone explain to me what the learning curve equation (Tn=T1n^.pdf
Can someone explain to me what the learning curve equation (Tn=T1n^.pdf
footworld1
 
Both influenza virus and (IFV), and Measles Virus (MV) are envel.pdf
Both influenza virus and (IFV), and Measles Virus (MV) are envel.pdfBoth influenza virus and (IFV), and Measles Virus (MV) are envel.pdf
Both influenza virus and (IFV), and Measles Virus (MV) are envel.pdf
footworld1
 
All of the following are characteristic to fatigue fracture surfaces.pdf
All of the following are characteristic to fatigue fracture surfaces.pdfAll of the following are characteristic to fatigue fracture surfaces.pdf
All of the following are characteristic to fatigue fracture surfaces.pdf
footworld1
 
A cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdf
A cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdfA cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdf
A cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdf
footworld1
 
You get a call from a customer who reinstalled Windows on his comput.pdf
You get a call from a customer who reinstalled Windows on his comput.pdfYou get a call from a customer who reinstalled Windows on his comput.pdf
You get a call from a customer who reinstalled Windows on his comput.pdf
footworld1
 
You are running the Windows installation setup for Windows 7 on a co.pdf
You are running the Windows installation setup for Windows 7 on a co.pdfYou are running the Windows installation setup for Windows 7 on a co.pdf
You are running the Windows installation setup for Windows 7 on a co.pdf
footworld1
 
With the biological species concept, the process of speciation is fre.pdf
With the biological species concept, the process of speciation is fre.pdfWith the biological species concept, the process of speciation is fre.pdf
With the biological species concept, the process of speciation is fre.pdf
footworld1
 
Write a class ArrayList that represents an array of integers. Init.pdf
Write a class ArrayList that represents an array of integers. Init.pdfWrite a class ArrayList that represents an array of integers. Init.pdf
Write a class ArrayList that represents an array of integers. Init.pdf
footworld1
 
what is the relationship between lnx and 1xSolutionThe relat.pdf
what is the relationship between lnx and 1xSolutionThe relat.pdfwhat is the relationship between lnx and 1xSolutionThe relat.pdf
what is the relationship between lnx and 1xSolutionThe relat.pdf
footworld1
 
Which of these conclusions is supported by plant phylogenies (evolut.pdf
Which of these conclusions is supported by plant phylogenies (evolut.pdfWhich of these conclusions is supported by plant phylogenies (evolut.pdf
Which of these conclusions is supported by plant phylogenies (evolut.pdf
footworld1
 
Who are some other individuals who might want to use the informa.pdf
Who are some other individuals who might want to use the informa.pdfWho are some other individuals who might want to use the informa.pdf
Who are some other individuals who might want to use the informa.pdf
footworld1
 
Which account below is not a subdivision of owners equityAccumu.pdf
Which account below is not a subdivision of owners equityAccumu.pdfWhich account below is not a subdivision of owners equityAccumu.pdf
Which account below is not a subdivision of owners equityAccumu.pdf
footworld1
 
What is the nucleoid region of a prokaryotic cell a membrane bound .pdf
What is the nucleoid region of a prokaryotic cell  a membrane bound .pdfWhat is the nucleoid region of a prokaryotic cell  a membrane bound .pdf
What is the nucleoid region of a prokaryotic cell a membrane bound .pdf
footworld1
 
What is heartwood What is sapwood What is the most abundant tissu.pdf
What is heartwood  What is sapwood  What is the most abundant tissu.pdfWhat is heartwood  What is sapwood  What is the most abundant tissu.pdf
What is heartwood What is sapwood What is the most abundant tissu.pdf
footworld1
 
Use arrays to store data for analysis. Use functions to perform the .pdf
Use arrays to store data for analysis. Use functions to perform the .pdfUse arrays to store data for analysis. Use functions to perform the .pdf
Use arrays to store data for analysis. Use functions to perform the .pdf
footworld1
 

More from footworld1 (20)

Describe 3 functions of the cellular membrane and how are these func.pdf
Describe 3 functions of the cellular membrane and how are these func.pdfDescribe 3 functions of the cellular membrane and how are these func.pdf
Describe 3 functions of the cellular membrane and how are these func.pdf
 
Discuss the three main types of intellectual capitalSolutionth.pdf
Discuss the three main types of intellectual capitalSolutionth.pdfDiscuss the three main types of intellectual capitalSolutionth.pdf
Discuss the three main types of intellectual capitalSolutionth.pdf
 
Describer in detail the innate immune response to both an intercellu.pdf
Describer in detail the innate immune response to both an intercellu.pdfDescriber in detail the innate immune response to both an intercellu.pdf
Describer in detail the innate immune response to both an intercellu.pdf
 
Describe the difference between tropic and non-tropic hormones.S.pdf
Describe the difference between tropic and non-tropic hormones.S.pdfDescribe the difference between tropic and non-tropic hormones.S.pdf
Describe the difference between tropic and non-tropic hormones.S.pdf
 
C++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdf
C++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdfC++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdf
C++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdf
 
Can someone explain to me what the learning curve equation (Tn=T1n^.pdf
Can someone explain to me what the learning curve equation (Tn=T1n^.pdfCan someone explain to me what the learning curve equation (Tn=T1n^.pdf
Can someone explain to me what the learning curve equation (Tn=T1n^.pdf
 
Both influenza virus and (IFV), and Measles Virus (MV) are envel.pdf
Both influenza virus and (IFV), and Measles Virus (MV) are envel.pdfBoth influenza virus and (IFV), and Measles Virus (MV) are envel.pdf
Both influenza virus and (IFV), and Measles Virus (MV) are envel.pdf
 
All of the following are characteristic to fatigue fracture surfaces.pdf
All of the following are characteristic to fatigue fracture surfaces.pdfAll of the following are characteristic to fatigue fracture surfaces.pdf
All of the following are characteristic to fatigue fracture surfaces.pdf
 
A cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdf
A cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdfA cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdf
A cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdf
 
You get a call from a customer who reinstalled Windows on his comput.pdf
You get a call from a customer who reinstalled Windows on his comput.pdfYou get a call from a customer who reinstalled Windows on his comput.pdf
You get a call from a customer who reinstalled Windows on his comput.pdf
 
You are running the Windows installation setup for Windows 7 on a co.pdf
You are running the Windows installation setup for Windows 7 on a co.pdfYou are running the Windows installation setup for Windows 7 on a co.pdf
You are running the Windows installation setup for Windows 7 on a co.pdf
 
With the biological species concept, the process of speciation is fre.pdf
With the biological species concept, the process of speciation is fre.pdfWith the biological species concept, the process of speciation is fre.pdf
With the biological species concept, the process of speciation is fre.pdf
 
Write a class ArrayList that represents an array of integers. Init.pdf
Write a class ArrayList that represents an array of integers. Init.pdfWrite a class ArrayList that represents an array of integers. Init.pdf
Write a class ArrayList that represents an array of integers. Init.pdf
 
what is the relationship between lnx and 1xSolutionThe relat.pdf
what is the relationship between lnx and 1xSolutionThe relat.pdfwhat is the relationship between lnx and 1xSolutionThe relat.pdf
what is the relationship between lnx and 1xSolutionThe relat.pdf
 
Which of these conclusions is supported by plant phylogenies (evolut.pdf
Which of these conclusions is supported by plant phylogenies (evolut.pdfWhich of these conclusions is supported by plant phylogenies (evolut.pdf
Which of these conclusions is supported by plant phylogenies (evolut.pdf
 
Who are some other individuals who might want to use the informa.pdf
Who are some other individuals who might want to use the informa.pdfWho are some other individuals who might want to use the informa.pdf
Who are some other individuals who might want to use the informa.pdf
 
Which account below is not a subdivision of owners equityAccumu.pdf
Which account below is not a subdivision of owners equityAccumu.pdfWhich account below is not a subdivision of owners equityAccumu.pdf
Which account below is not a subdivision of owners equityAccumu.pdf
 
What is the nucleoid region of a prokaryotic cell a membrane bound .pdf
What is the nucleoid region of a prokaryotic cell  a membrane bound .pdfWhat is the nucleoid region of a prokaryotic cell  a membrane bound .pdf
What is the nucleoid region of a prokaryotic cell a membrane bound .pdf
 
What is heartwood What is sapwood What is the most abundant tissu.pdf
What is heartwood  What is sapwood  What is the most abundant tissu.pdfWhat is heartwood  What is sapwood  What is the most abundant tissu.pdf
What is heartwood What is sapwood What is the most abundant tissu.pdf
 
Use arrays to store data for analysis. Use functions to perform the .pdf
Use arrays to store data for analysis. Use functions to perform the .pdfUse arrays to store data for analysis. Use functions to perform the .pdf
Use arrays to store data for analysis. Use functions to perform the .pdf
 

Recently uploaded

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 

Recently uploaded (20)

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 

Write a program to decipher messages encoded using a prefix code, gi.pdf

  • 1. Write a program to decipher messages encoded using a prefix code, given the encoding tree. Such codes are widely used in applications that compress data, including JPEG for images, MP3 for music, and DivX for video. using Huffman Trees (Prefix Binary Trees) c++ Solution #include #include #include #include // for CHAR_BIT #include #include const int UniqueSymbols = 1 << CHAR_BIT; const char* SampleString = "this is an example for huffman encoding"; typedef std::vector HuffCode; typedef std::map HuffCodeMap; class INode { public: const int f; virtual ~INode() {} protected: INode(int f) : f(f) {} }; class InternalNode : public INode { public: INode *const left; INode *const right; InternalNode(INode* c0, INode* c1) : INode(c0->f + c1->f), left(c0), right(c1) {} ~InternalNode() { delete left; delete right; } }; class LeafNode : public INode { public: const char c; LeafNode(int f, char c) : INode(f), c(c) {} }; struct NodeCmp { bool operator()(const INode* lhs, const INode* rhs) const { return lhs->f > rhs->f; } }; INode* BuildTree(const int (&frequencies)[UniqueSymbols]) { std::priority_queue, NodeCmp> trees; for (int i = 0; i < UniqueSymbols; ++i) { if(frequencies[i] != 0) trees.push(new LeafNode(frequencies[i], (char)i)); } while (trees.size() > 1) { INode* childR = trees.top(); trees.pop(); INode* childL = trees.top(); trees.pop(); INode* parent = new InternalNode(childR, childL); trees.push(parent); } return trees.top(); } void GenerateCodes(const INode* node, const HuffCode& prefix, HuffCodeMap& outCodes) { if (const LeafNode* lf = dynamic_cast(node)) { outCodes[lf->c] = prefix; } else if (const InternalNode* in = dynamic_cast(node)) { HuffCode leftPrefix = prefix; leftPrefix.push_back(false); GenerateCodes(in->left, leftPrefix, outCodes); HuffCode rightPrefix = prefix; rightPrefix.push_back(true); GenerateCodes(in->right, rightPrefix, outCodes); } } int main() { // Build frequency table int frequencies[UniqueSymbols] = {0}; const char* ptr = SampleString; while (*ptr != '0') ++frequencies[*ptr++]; INode* root = BuildTree(frequencies); HuffCodeMap codes; GenerateCodes(root, HuffCode(), codes); delete root; for (HuffCodeMap::const_iterator it = codes.begin(); it != codes.end(); ++it) { std::cout << it->first << " "; std::copy(it- >second.begin(), it->second.end(), std::ostream_iterator(std::cout)); std::cout << std::endl; } return 0; }