html5-img
1 / 48

Main Index

Chapter 14 – Heaps, Binary Files, and Bit Sets. 1. Main Index. Contents. Complete Binary Tree Example Maximum and Minimum Heaps Example Heap Insertion Example pushHeap() Example popHeap() Example Adjusting popHeap() Example Heap Sort Example (2 slides) Heapifying Example (2 slides).

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. Chapter 14 – Heaps, Binary Files, and Bit Sets 1 Main Index Contents • Complete Binary Tree Example • Maximum and Minimum Heaps Example • Heap Insertion Example • pushHeap() Example • popHeap() Example • Adjusting popHeap() Example • Heap Sort Example (2 slides) • Heapifying Example (2 slides) • File Structure • Direct File Access • bitVector Class • Lossless Compression • Lossy Compression • Example of Building Huffman Tree (4 slides) • Summary Slides (8 slides)

  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) { int currentPos, parentPos; T target; currentPos = last-1; parentPos = (currentPos-1)/2; target = v[last-1]; while (currentPos != 0) { if (comp(target,v[parentPos])) { 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. 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. Example of Implementing 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. Example of Implementing heap sort int arr[] = {50, 20, 75, 35, 25}; vector<int> v(arr, 5);

  14. 14 Main Index Contents Example of Implementing heap sort (Cont….)

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

  16. Example of Heapifying a Vector

  17. 17 Main Index Contents Example of Heapifying a Vector (Cont…)

  18. Example of Implementing heap sort 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--; } }

  19. Example of Implementing heap sort 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; }

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

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

  22. 22 Main Index Contents File Structure • A text file contains ASCII characters with a newline sequence separating lines. • A binary file consists of data objects that vary from a single character (byte) to more complex structures that include integers, floating point values, programmer-generated class objects, and arrays. • each data object in a file a record

  23. Direct File Access • The functions seekg() and seekp() allow the application to reposition the current file pointers. • The seek functions take an offset argument that measures the number of bytes from the beginning (beg), ending (end), or current position (cur) in the file. • If a file is used for both input and output, use the seek functions tellg() and seekg().

  24. 24 Main Index Contents Implementing the bitVector Class • bitMask() returns an unsigned character value containing a 1 in the bit position representing i.

  25. Bitvector(d_bitvec.h) class bitVector { public: bitVector(int numBits = 16); bitVector(int b[], int n); bitVector& operator= (int v); bitVector& operator= (char c); int size() const; int bit(int i) const; void set(int i); void clear(int i); void clear(); bitVector operator~ (); void write(fstream& ostr); void read(fstream& istr, int numBits); friend ostream& operator<< (ostream& ostr, const bitVector& x);

  26. Bitvector(d_bitvec.h) private: int numberOfBits; int vectorSize; vector<unsigned char> member; int vectorIndex(int i) const; unsigned char bitMask(int i) const; };

  27. Bitvector(d_bitvec.h) int bitVector::vectorIndex(int i) const { return i >> 3; } unsigned char bitVector::bitMask(int i) const { return 1 << (7 - (i & 7)); } bitVector::bitVector(int numBits): numberOfBits(numBits) { vectorSize = (numberOfBits+7) >> 3; member.resize(vectorSize); }

  28. Bitvector(d_bitvec.h) bitVector operator| (const bitVector& x, const bitVector& y) { int i; if (x.numberOfBits != y.numberOfBits) throw rangeError("bitVector |: bit vectors are not the same size"); bitVector tmp(x.numberOfBits); for (i = 0; i < x.vectorSize; i++) tmp.member[i] = x.member[i] | y.member[i]; return tmp; }

  29. Bitvector(d_bitvec.h) bitVector bitVector::operator~ () { bitVector tmp(numberOfBits); for (int i = 0; i < vectorSize; i++) tmp.member[i] = ~member[i]; return tmp; }

  30. Bitvector(d_bitvec.h) int bitVector::bit(int i) const { if (i < 0 || i >= numberOfBits) throw indexRangeError( "bitVector bit(): bit out of range", i, numberOfBits); if ((member[vectorIndex(i)] & bitMask(i)) != 0) return 1; else return 0; }

  31. Bitvector(d_bitvec.h) void bitVector::set(int i) { if (i < 0 || i >= numberOfBits) throw indexRangeError( "bitVector set(): bit out of range", i, numberOfBits); member[vectorIndex(i)] |= bitMask(i); }

  32. Bitvector(d_bitvec.h) void bitVector::clear(int i) { if (i < 0 || i >= numberOfBits) throw indexRangeError( "bitVector clear(): bit out of range", i, numberOfBits); member[vectorIndex(i)] &= ~bitMask(i); }

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

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

  35. 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)

  36. 36 Main Index Contents Building Huffman Tree

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

  38. Building Huffman Tree (Cont…)

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

  40. Huffman Compression (16(2)+4(4)+8(2)+6(3)+20(2)+3(4))*1000=134000 bits 57000/1340005=3.4(compression ration)

  41. 41 Main Index Contents Summary Slide 1 §-Heap - an array-based tree that has heap order - maximum heap: if v[i] is a parent, then v[i]  v[2i+1] and v[i]  v[2i+2] (a parent is  its children) - root, v[0], is the maximum value in the vector - minimum heap: the parent is  its children. - v[0] is the minimum value - Insertion: place the new value at the back of the heap and filtering it up the tree.

  42. 42 Main Index Contents Summary Slide 2 §-Heap (Cont…) - Deletion: exchanging its value with the back of the heap and then filtering the new root down the tree, which now has one less element. - Insert and delete running time: O(log2 n) - heapifying: apply the filter-down operation to the interior nodes, from the last interior node in the tree down to the root - running time: O(n) - The O(n log2 n) heapsort algorithm heapifies a vector and erases repeatedly from the heap, locating each deleted value in its final position.

  43. 43 Main Index Contents Summary Slide 3 §-Binary File - a sequence of 8-bit characters without the requirement that a character be printable and with no concern for a newline sequence that terminates lines - often organized as a sequence of records: record 0, record 1, record 2, ..., record n-1. - uses for both input and output, and the C++ file <fstream> contains the operations to support these types of files. - the open() function must use the attribute ios::binary

  44. 44 Main Index Contents Summary Slide 4 §-Binary File (Cont…) - For direct access to a file record, use the function seekg(), which moves the file pointer to a file record. - accepts an argument that specifies motion from the beginning of the file (ios::beg), from the current position of the file pointer (ios::cur), and from the end of the file (ios::end) - use read() function to inputs a sequence of bytes from the file into block of memory and write() function to output from a block of memory to a binary file

  45. 45 Main Index Contents Summary Slide 5 §-Bit Manipulation Operators - | (OR), & (AND), ^ (XOR), ~ (NOT), << (shift left), and >> (shift right) - use to perform operations on specific bits within a character or integer value. - The class, bitVector, use operator overloading - treat a sequence of bits as an array, with bit 0 the left-most bit of the sequence - bit(), set(), and clear() allow access to specific bits - the class has I/O operations for binary files and the stream operator << that outputs a bit vector as an ASCII sequence of 0 and 1 values.

  46. 46 Main Index Contents Summary Slide 6 §-File Compression Algorithm - encodes a file as sequence of characters that consume less disk space than the original file. - Two types of compression algorithms: 1) lossless compression – restores the original file. – approach: count the frequency of occurrence of each character in the file and assign a prefix bit code to each character - file size: the sum of the products of each bit-code length and the frequency of occurrence of the corresponding character.

  47. 47 Main Index Contents Summary Slide 7 §-File Compression Algorithm (Cont…) 2) lossy compression – loses some information during compression and the data cannot be recovered exactly – normally used with sound and video files - The Huffman compression algorithm builds optimal prefix codes by constructing a full tree with the most frequently occurring characters and shorter bit codes near the top of the tree. The less frequently occurring characters occur near the bottom of the tree and have longer bit codes.

  48. 48 Main Index Contents Summary Slide 8 §-File Compression Algorithm (Cont…) - If the file contains n distinct characters, the loop concludes after n-1 iterations, having built the Huffman Tree. - implementation requires the use of a heap, bit operations, and binary files - The use of the bitVector class simplifies the construction of the classes hCompress and hDecompress, which perform Huffman compression and decompression. - works better with textfiles; they tend to have fewer unique characters than binary files.

More Related