Presentation is loading. Please wait.

Presentation is loading. Please wait.

Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum.

Similar presentations


Presentation on theme: "Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum."— Presentation transcript:

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

2 Main Index Contents 22 Main Index Contents Example of Complete Binary Tree for a Vector

3 Main Index Contents 33 Main Index Contents Maximum and Minimum Heaps Example

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

5 Main Index Contents 5 Example of Reorder the tree in pushHeap()

6 Main Index Contents 6 Example of Exchanging elements in popHeap()

7 Main Index Contents 77 Main Index Contents Example of Adjusting the heap for popHeap()

8 Main Index Contents 8 Example of Implementing heap sort int arr[] = {50, 20, 75, 35, 25}; vector v(arr, 5);

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

10 Main Index Contents 10 Example of Heapifying a Vector

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

12 Main Index Contents 12 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

13 Main Index Contents 13 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().

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

15 Main Index Contents 15 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

16 Main Index Contents 16 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,

17 Main Index Contents 17 Main Index Contents Building Huffman Tree

18 Main Index Contents 18 Main Index Contents Building Huffman Tree (Cont…)

19 Main Index Contents 19 Building Huffman Tree (Cont…)

20 Main Index Contents 20 Main Index Contents Building Huffman Tree (Cont…)

21 Main Index Contents 21 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.

22 Main Index Contents 22 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(log 2 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 log 2 n) heapsort algorithm heapifies a vector and erases repeatedly from the heap, locating each deleted value in its final position.

23 Main Index Contents 23 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 contains the operations to support these types of files. - the open() function must use the attribute ios::binary

24 Main Index Contents 24 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

25 Main Index Contents 25 Main Index Contents Summary Slide 5 §- Bit Manipulation Operators - | (OR), & (AND), ^ (XOR), ~ (NOT), > (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.

26 Main Index Contents 26 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.

27 Main Index Contents 27 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.

28 Main Index Contents 28 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.


Download ppt "Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum."

Similar presentations


Ads by Google