# Main Index - PowerPoint PPT Presentation

1 / 30

Lecture 12 – Heaps, Priority_queue, Huffman Tree. 1. Main Index. Contents. Complete Binary Tree Example Maximum and Minimum Heaps Example Heap Insertion Example pushHeap() Example popHeap() Example Adjusting popHeap() Example Heapifying Example Heap Sort Example Huffman Tree. 2.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Main Index

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

#### Presentation Transcript

Lecture 12

– Heaps, Priority_queue, Huffman Tree

1

Main Index

Contents

• Complete Binary Tree Example

• Maximum and Minimum Heaps Example

• Heap Insertion Example

• pushHeap() Example

• popHeap() Example

• Heapifying Example

• Heap Sort Example

• Huffman Tree

2

Main Index

Contents

3

Main Index

Contents

4

Main Index

Contents

### pushHeap

template <typename T, typename Compare>

void pushHeap(vector<T>& v, int last, Compare comp)

//compare为”>”，得到的堆是最大堆，否则是最小堆

{int currentPos, parentPos;

T target;

currentPos = last-1;

parentPos = (currentPos-1)/2;

target = v[last-1];

while (currentPos != 0)

{if (comp(target,v[parentPos]))//if true, 边交互边向上比较

{v[currentPos] = v[parentPos];

currentPos = parentPos;

parentPos = (currentPos-1)/2;

}

elsebreak;

}

v[currentPos] = target;

}

8

Main Index

Contents

### Example of Adjusting the heap for popHeap()

template <typename T, typename Compare>

void adjustHeap(vector<T>& v, int first, int last, Compare comp)//”>”

{int currentPos, childPos;

T target;

currentPos = first;

target = v[first];

childPos = 2 * currentPos + 1;

while (childPos <= last-1)

{if ((childPos+1 <= last-1) && comp(v[childPos+1], v[childPos]))

childPos = childPos + 1;

if (comp(v[childPos],target))

{v[currentPos] = v[childPos];

currentPos = childPos;

childPos = 2 * currentPos + 1;}

elsebreak;

}

v[currentPos] = target;

}

### popHeap

template <typename T, typename Compare>

void popHeap(vector<T>& v, int last, Compare comp)

{

T temp;

temp = v[0];

v[0] = v[last-1];

v[last-1] = temp;

}

### Heap sort

#include <iostream>

#include <vector>

#include "d_heap.h"

#include "d_util.h"

using namespace std;

int main()

{ int arr[] = {5, 9, 2, 7, 1, 3, 8};

int i, arrSize = sizeof(arr)/sizeof(int);

vector<int> vA, vB;

for (i = 0; i < arrSize; i++)//建堆

{vA.push_back(arr[i]);

pushHeap(vA, vA.size(), greater<int>());

vB.push_back(arr[i]);

pushHeap(vB, vB.size(), less<int>());

}

cout << "Maximum heap: ";

### Example of Implementing heap operation

while (!vA.empty())

{popHeap(vA, vA.size(), greater<int>());

cout << vA.back() << " ";

vA.pop_back();

}

cout << endl;

cout << "Minimum heap: ";

while (!vB.empty())

{popHeap(vB, vB.size(), less<int>());

cout << vB.back() << " ";

vB.pop_back();

}

cout << endl;

return 0;

}

### 堆序化(Heapified Tree)

int arr[] = {50, 20, 75, 35, 25};

vector<int> v(arr, 5);

14

Main Index

Contents

16

Main Index

Contents

### MakeHeap

template <typename T, typename Compare>

void makeHeap(vector<T>& v, Compare comp)

{

int heapPos, lastPos;

lastPos = v.size();

heapPos = (lastPos - 2)/2;

while (heapPos >= 0)

{

heapPos--;

}

}

### Example of Implementing heap sort

template <typename T, typename Compare>

void heapSort (vector<T>& v, Compare comp)

{

makeHeap(v, comp);

int i, n = v.size();

for(i = n; i > 1;i--)

{

popHeap(v, i, comp);

}

}

### Priority_queue(STL)

template <typename T, typename Compare = greater<T> >

class miniPQ

{

public:

miniPQ();

int size() const;

bool empty() const;

void push(const T& item);

void pop();

T& top();

const T& top() const;

private:

vector<T> pqList;

Compare comp;

};

### Priority_queue

miniPQ<int>pqMax;

miniPQ<int, less<int>> pqmin;

template <typename T, typename Compare>

void miniPQ<T,Compare>::pop()

{

if (pqList.empty())

throw underflowError("miniPQ pop(): empty list");

popHeap(pqList, pqList.size(), comp);

pqList.pop_back();

}

### Priority_queue

#include <iostream>

#include <queue>

using namespace std;

Void main ()

{ priority_queue<int, vector<int>, less<int> > ipq;

priority_queue<int, vector<int>, greater<int> > gipq;

priority_queue<int, vector<int> > pq;

ipq.push(100); ipq.push(200); ipq.push(300);

gipq.push(100); gipq.push(200); gipq.push(300);

pq.push(100); pq.push(200); pq.push(300);

cout << "size of priority_queue ipq = " << ipq.size() << endl;

cout << "ipq <int,vector<int>, less<int> > = ";

while ( !ipq.empty() ) {

cout << ipq.top() << " ";

ipq.pop();}

}

//ipq <int,vector<int>, less<int> > = 300 200 100

//gipq <int,vector<int>, greater<int> > = 100 200 300

//pq <int,vector<int> > = 300 200 100

Huffman Tree

23

Main Index

Contents

### Lossless Compression

• data compression loses no information

• original data can be recovered exactly from the compressed data.

• normally apply to "discrete" data, such as text, word processing files, computer applications, and so forth

### Lossy Compression

• loses some information during compression and the data cannot be recovered exactly

• shrink the data further than lossless compression techniques.

• Sound files often use this type of compression,

### Huffman Compression

1.Prefix codes(optimal binary codes, minimal cost)

2.greedy algorithm

Example:

(16(3)+4(3)+8(3)+6(3)+20(3)+3(3))*1000=171000 bits

171000/8=21375 bytes

(16(8)+4(8)+8(8)+6(8)+20(8)+3(8))*1000/8=57000 bytes

21375/57000=62.5%

57000/21375=2.67(compression ration)

26

Main Index

Contents

27

Main Index

Contents

29

Main Index

Contents

### Huffman Compression

(16(2)+4(4)+8(2)+6(3)+20(2)+3(4))*1000=134000 bits

134000/8=16750

57000/16750=3.4(compression ration)

Huffman算法创建最优二叉树，可产生最优前缀码，也称做最小成本树。

Huffman树的成本（也可称期望长度）

Cost=w1*l1+w2*l2……+wk*lk

Cost=4*3+4*4+6*3+8*2+16*2+20*2=134