1 Huffman Codes Drozdek Chapter 11. 2 Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 15 - Case Study: Huffman Codes.
Advertisements

Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Binary Search Trees Chapter 6.
Huffman code uses a different number of bits used to encode characters: it uses fewer bits to represent common characters and more bits to represent rare.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
1 Binary Search Trees III Delete Chapter 6. 2 Objectives You will be able to write code to delete a node from a Binary Search Tree.
11 Introduction to Object Oriented Programming (Continued) Cats II.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Binary Search Trees II Morse Code.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
 The amount of data we deal with is getting larger  Not only do larger files require more disk space, they take longer to transmit  Many times files.
Huffman Coding. Huffman codes can be used to compress information –Like WinZip – although WinZip doesn’t use the Huffman algorithm –JPEGs do use Huffman.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Ticket Booth Base Level 3 1. In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
Validation final steps Stopping gaps being entered in an input.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 15 Strings as Character Arrays
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
1 Huffman Codes. 2 ASCII use same size encoding for all characters. Variable length codes can produce shorter messages than fixed length codes Huffman.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
1 The Standard Template Library Drozdek Section 3.7.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
CSE 143 Lecture 22 Huffman slides created by Ethan Apter
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Exception Handling How to handle the runtime errors.
1 Huffman Codes Computing an Optimal Code for a Document.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
Compression and Huffman Coding. Compression Reducing the memory required to store some information. Lossless compression vs lossy compression Lossless.
1 Command Processor. Objectives You will be able to Create and understand State Diagrams. Write a text based command processor with multilevel menus.
1 Using an XML Parser. 2 Objective You will be able to use a publically available open source parser to convert an XML file into an internal data structure.
Review GitHub Project Guide Huffman Coding
C++ Programming Standard Template Library Homework 5,6,7.
Chapter 1.2 Introduction to C++ Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Data Compression If you’ve ever sent a large file to a friend, you may have compressed it into a zip archive like the one on this slide before doing so.
Huffman Compression.
Introduction to Programming
Trees Addenda.
C++ Pointers and Strings
File Compression Even though disks have gotten bigger, we are still running short on disk space A common technique is to compress files so that they take.
C++ Programming Lecture 20 Strings
C++ Pointers and Strings
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Presentation transcript:

1 Huffman Codes Drozdek Chapter 11

2 Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use for decoding is not appropriate for encoding. We need to be able to look up a letter and get the string of 0’s and 1’s that are the code for that letter. Use an STL map for this. Key is character to be encoded. Value is string of 1's and 0's.

3 Huffman_Tree.h #pragma once #include #include "Char_Freq.h" class Huffman_Tree { public: Huffman_Tree(void); ~Huffman_Tree(void) {}; // Add a single node tree to the list. void Add(char c, double frequency); void Display_List(void); void Make_Decode_Tree(void); void Display_Decode_Tree(Char_Freq* cf, int indent) const; void Display_Code(Char_Freq* cf, std::string prefix) const; std::string Decode_Msg(std::string msg) const;

4 Huffman_Tree.h void Make_Encode_Map(void); std::string Encode_Char(char ch); private: std::list node_list; Char_Freq decode_tree_root; std::map encode_map; void Display_Code(Char_Freq* cf, std::string prefix) const; void Make_Encode_Map2(Char_Freq* cf, std::string prefix); }; NOT const

5 Huffman_Tree.cpp Add at end: void Huffman_Tree::Make_Encode_Map(void) { Make_Encode_Map2(&decode_tree_root, ""); }

6 Huffman_Tree.cpp void Huffman_Tree::Make_Encode_Map2(Char_Freq* cf, string prefix) { if (cf->ch != 0) { // At a leaf char ch = cf->Ch(); encode_map[ch] = prefix; } else { // At an interior node Make_Encode_Map2(cf->left, prefix + "0"); Make_Encode_Map2(cf->right, prefix + "1"); }

7 Using the Encode Tree string Huffman_Tree::Encode_Char(char ch) { string code = encode_map[ch]; return code; } Build Project

8 main.cpp Add code to main.cpp to permit the user to enter and encode messages. void do_encode(void) { string msg; cout << "Enter message to encode\n"; getline(cin, msg); for (size_t i = 0; i < msg.length(); ++i) { char next_char = tolower(msg[i]); string code = huffman_tree.Encode_Char(next_char); cout << code; } cout << endl << endl; } _04_11_Huffman_Codes/do_encode.cpp.txt

9 main.cpp At end of main(), replacing do_decode(): huffman_tree.Make_Encode_Tree(); while (true) { do_encode(); do_decode(); } cin.get(); }

10 Program Running

11 Handle All Letters and Digits The program seems to be working. Let’s extend it to handle all letters and digits (and space.) Wikipedia has letter frequencies: Copy from Downloads area: _04_11_Huffman_Codes/build_huffman_tree.cpp.txt _04_11_Huffman_Codes/build_huffman_tree.cpp.txt

12 In main.cpp void build_huffman_tree(void) { huffman_tree.add('a',0.0401); huffman_tree.add('b',0.0073); huffman_tree.add('c',0.0137); huffman_tree.add('d',0.0209); huffman_tree.add('e',0.0624); huffman_tree.add('f',0.0109); huffman_tree.add('g',0.0099); huffman_tree.add('h',0.0299); huffman_tree.add('i',0.0342); huffman_tree.add('j',0.0008); huffman_tree.add('k',0.0038); huffman_tree.add('l',0.0198); huffman_tree.add('m',0.0118); huffman_tree.add('n',0.0331); huffman_tree.add('o',0.0369); huffman_tree.add('p',0.0095); huffman_tree.add('q',0.0005); huffman_tree.add('r',0.0294); huffman_tree.add('s',0.0311);

13 In main.cpp huffman_tree.add('t',0.0445); huffman_tree.add('u',0.0135); huffman_tree.add('v',0.0048); huffman_tree.add('w',0.0116); huffman_tree.add('x',0.0007); huffman_tree.add('y',0.0097); huffman_tree.add('z',0.0004); huffman_tree.add('0',0.0442); huffman_tree.add('1',0.0442); huffman_tree.add('2',0.0442); huffman_tree.add('3',0.0442); huffman_tree.add('4',0.0442); huffman_tree.add('5',0.0442); huffman_tree.add('6',0.0442); huffman_tree.add('7',0.0442); huffman_tree.add('8',0.0442); huffman_tree.add('9',0.0442); huffman_tree.add(' ',0.0668); }

14 main() int main(void) { cout << "This is the Huffman code demo.\n"; build_huffman_tree(); huffman_tree.display_list(); huffman_tree.make_decode_tree(); huffman_tree.make_encode_tree(); int cmd_code = 0;

15 Program in Action Saved as Huffman_Codes_with_Associative_Map.zip