1 / 30

Main Index

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.

nolcha
Download Presentation

Main Index

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 2 Main Index Contents Example of Complete Binary Tree for a Vector

  3. 3 Main Index Contents Maximum and Minimum Heaps Example

  4. 4 Main Index Contents Example of Heap Before and After Insertion of 50

  5. Example of Reorder the tree in pushHeap()

  6. 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; } else break; } v[currentPos] = target; }

  7. Example of Exchanging elements in popHeap()

  8. 8 Main Index Contents Example of Adjusting the heap for popHeap()

  9. Pop--AdjustHeap 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; } else break; } v[currentPos] = target; }

  10. 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; adjustHeap(v, 0, last-1, comp); }

  11. 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: ";

  12. 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; }

  13. 堆序化(Heapified Tree) int arr[] = {50, 20, 75, 35, 25}; vector<int> v(arr, 5);

  14. 14 Main Index Contents Heapified Tree

  15. Example of Heapifying a Vector

  16. 16 Main Index Contents Heapifying a Vector

  17. 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) { adjustHeap(v,heapPos, lastPos, comp); heapPos--; } }

  18. 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); } }

  19. 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; };

  20. 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(); }

  21. 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

  22. Huffman Tree Huffman Tree 哈夫曼编码:1952年由D.A.Huffman提出,用来构建满足立刻可解码性,即非前缀编码。 有损压缩(Lossy compression) 无损压缩(Lossless compression)

  23. 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

  24. 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,

  25. 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. 26 Main Index Contents Building Huffman Tree

  27. 27 Main Index Contents Building Huffman Tree (Cont…)

  28. Building Huffman Tree (Cont…)

  29. 29 Main Index Contents Building Huffman Tree (Cont…)

  30. 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

More Related