Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— Presentation transcript:

1 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)

2 Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a. 2005-2006 2 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)

3 Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a. 2005-2006 3 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

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

5 Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a. 2005-2006 5 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

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

7 Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a. 2005-2006 7 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

8 Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a. 2005-2006 8 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

9 Heap

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

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

12 Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a. 2005-2006 12 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

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

14 Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a. 2005-2006 14 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

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

16 Gabriele Monfardini - Corso di Basi di Dati Multimediali a.a. 2005-2006 16 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...


Download ppt "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)"

Similar presentations


Ads by Google