Huffman Coding An implementation using C++ STL (part of the material is due to the work of Mark Nelson, Dr. Dobb’s Journal, January 1996)

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store Bjarne Stroustrup
Advertisements

Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
COMP171 Fall 2005 Lists.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Introduction to Recursion and Recursive Algorithms
More on Canonical Huffman coding. Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a As we have seen canonical Huffman coding allows.
COL 106 Shweta Agrawal and Amit Kumar
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
ALGORITHMS FOR ISNE DR. KENNETH COSH WEEK 3.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Week 10: Heap and Priority queue. Any feature here?
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
sorting31 Sorting III: Heapsort sorting32 A good sorting algorithm is hard to find... Quadratic sorting algorithms (with running times of O(N 2 ), such.
Priority Queues, Heaps & Leftist Trees
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Heapsort Based off slides by: David Matuszek
Foundation of Computing Systems Lecture 6 Trees: Part III.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Information and Computer Sciences University of Hawaii, Manoa
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
Adaptive Huffman Coding. Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a Why Adaptive Huffman Coding? Huffman coding suffers.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
CS 361 – Chapter 5 Priority Queue ADT Heap data structure –Properties –Internal representation –Insertion –Deletion.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Partially Ordered Data ,Heap,Binary Heap
Basic Data Structures.
Priority Queues Sections 6.1 to 6.5.
structures and their relationships." - Linus Torvalds
Chapter 8 – Binary Search Tree
structures and their relationships." - Linus Torvalds
Heapsort Heap & Priority Queue.
Tree Representation Heap.
Sorting Dr. Yingwu Zhu.
structures and their relationships." - Linus Torvalds
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Priority Queues (Heaps)
Presentation transcript:

Huffman Coding An implementation using C++ STL (part of the material is due to the work of Mark Nelson, Dr. Dobb’s Journal, January 1996)

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a Why C++ No special reason, also C, Java and many more The main advantage of using C++ is the presence of the STL (Standard Template Library)

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a What is the STL It is a library of templates A template is a class which has a data type that is a parameter (by the way, this data type can be primitive, or itself a class) vector, queue

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a What is a queue? FIFO Operations: push(), pop() Very simple to implement, but sometimes we need a more sophisticated form of queue

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a What is a priority queue? In a priority queue each elements has a priority New elements are added through the function push() pop() function gets an element from the queue. In a priority queue this element is not the oldest, but the element with the maximum priority This could be useful in certain types of tasks, for example the scheduler of an operating system

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a What is a priority queue?

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a Implementation with a heap One efficient implementation of a priority queue uses a heap A heap is a data structure that keeps queue elements in a partially-sorted order. Insertion and extraction can be accomplished in log(N) time, where N is the number of elements Also memory overhead is almost null

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a Heap characteristics The elements of a heap are stored in a contiguous array (let’s say, for convenience, from position 1 to position N) The elements in the array are part of an implicit binary tree. Every element (except the root node) has a parent at location Each node has a priority that is greater then or equal to both of each children

Heap

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a Heap: push() swap

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a Heap: pop() Something similar also happens when we call the pop() functions... Details don’t matter...

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a STL priority_queue It implements a priority queue using a heap We don’t need to remember all the details about heap and resorting because... all is automatic

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a Huffman implementation - I Class node { public: int weight; unsigned char value; const node *child0; const node *child1;

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a Huffman implementation - II node(unsigned char c=0, int i=-1) { value=c; weight=i; child0=0; child1=0; } node(const node *c0, const node *c1) { value=0; weight=c0->weight + c1->weight; child0=c0; child1=c1; } Constructors

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a Huffman implementation - III Comparison operator bool operator>(const node &a) const { return weight > a.weight; }

Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a The key loop while (q.size()>1) { node *child0 = new node(q.top()); q.pop(); node *child1 = new node(q.top()); q.pop(); q.push(node(child0, child1)); } and now, let’s have a look at the results...