main index contents 11 main index contents complete binary tree example complete binary tree example...

28
1 Main Index Conten ts 1 Main Index Conten ts Complete Binary Tree Ex ample Maximum and Minimum Hea ps Example Heap Insertion Example pushHeap () Example popHeap () Example Adjusting popHeap () Example Heap Sort Example (2 sl ides) Heapifying Example (2 s Chapter 14 Chapter 14 Heaps, Binary Files, and Bit Sets Heaps, Binary Files, and Bit Sets File Structure Direct File Access bitVector Class Lossless Compression Lossy Compression Example of Building Huf fman Tree (4 slides) Summary Slides (8 slide s)

Upload: stephen-chandler

Post on 21-Jan-2016

251 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents

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)

Chapter 14 Chapter 14 – – Heaps, Binary Files, and Bit SetsHeaps, Binary Files, and Bit Sets

File Structure

Direct File Access

bitVector Class

Lossless Compression

Lossy Compression

Example of Building Huffman Tree (4 slides)

Summary Slides (8 slides)

Page 2: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

2 Main IndexMain Index ContentsContents2 Main IndexMain Index ContentsContents

Example of Complete Binary Example of Complete Binary Tree for a Vector Tree for a Vector

5

807

4269

31

v [0 ]

v [1 ] v [2 ]

v [9 ]

v [4 ]

v [8 ]v [7 ]

v [5 ]v [3 ] v [6 ]

Page 3: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

3 Main IndexMain Index ContentsContents3 Main IndexMain Index ContentsContents

Maximum and Minimum Heaps Maximum and Minimum Heaps ExampleExample

1 0

4 0

3 01 5

4 0

1 0

3 01 5

5

2 5

5 55 22 01 1

5 01 0

2 2

5 5

2 22 0

51 11 02 5

5 25 0

(A ) M axim u m H eap (9 n o d es ) (B ) M axim u m H eap (4 n o d es )

(C ) M in im u m H eap (9 n o d es ) (D ) M in im u m H eap (4 n o d es )

Page 4: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

4 Main IndexMain Index ContentsContents4 Main IndexMain Index ContentsContents

Example of Heap Before and Example of Heap Before and After Insertion of 50After Insertion of 50

6 3

1 835

3 882 51 0

4 03 0

v [0 ]

v [1 ] v [2 ]

v [9 ]

v [4 ]

v [8 ]v [7 ]

v [5 ]v [3 ] v [6 ]

6 3

1 835

3 882 51 0

4 03 0

v [0 ]

v [1 ] v [2 ]

v [9 ]

v [4 ]

v [8 ]v [7 ]

v [5 ]v [3 ] v [6 ]

(a) (b )

5 0

v [1 0 ]

Page 5: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

5 Main IndexMain Index ContentsContents

Example of Reorder the tree in Example of Reorder the tree in pushHeap()pushHeap()

6 3

1 8 2 5

3 0

v [0 ]

v [1 ]

v [9 ]

v [4 ]

5 0

v [1 0 ]

. . .

. . .

St ep 1 C o m p are 5 0 an d 2 5(E xch an ge v [1 0 ] an d v [4 ])

6 3

1 8 2 5

3 0

v [0 ]

v [1 ]

v [9 ]

v [4 ]

5 0

v [1 0 ]

. . .

. . .6 3

1 8 2 5

3 0

v [0 ]

v [1 ]

v [9 ]

v [4 ]

5 0

v [1 0 ]

. . .

. . .

St ep 2 C o m p are 5 0 an d 3 0(E xch an ge v [4 ] an d v [1 ])

St ep 3 C o m p are 5 0 an d 6 3(5 0 in co rrect lo cat io n )

Page 6: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

6 Main IndexMain Index ContentsContents

Example of Exchanging Example of Exchanging elements in popHeap()elements in popHeap()

6 3

1 835

3 882 51 0

4 03 0

v [0 ]

v [1 ] v [2 ]

v [9 ]

v [4 ]

v [8 ]v [7 ]

v [5 ]v [3 ] v [6 ]

B efo re a d elet io n A ft er exch an gin g t h e ro o tan d las t elem en t in t h e h eap

6 3

1 8

35

3 882 51 0

4 03 0

v [0 ]

v [1 ] v [2 ]

v [9 ]

v [4 ]

v [8 ]v [7 ]

v [5 ]v [3 ] v [6 ]

Page 7: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

7 Main IndexMain Index ContentsContents7 Main IndexMain Index ContentsContents

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

St ep 1 : Exch an ge 1 8 an d 4 0

1 8

3 88

4 0

v [0 ]

v [2 ]

v [5 ] v [6 ]

. . .

St ep 2 : Exch an ge 1 8 an d 3 8

1 8

3 8

8

4 0

v [0 ]

v [2 ]

v [5 ] v [6 ]

. . .

Page 8: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

8 Main IndexMain Index ContentsContents

Example of Implementing heap Example of Implementing heap sortsort

7 5

2 52 0

5 03 5

H eap ified T ree

int arr[] = {50, 20, 75, 35, 25};vector<int> v(arr, 5);

Page 9: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

9 Main IndexMain Index ContentsContents9 Main IndexMain Index ContentsContents

Example of Implementing heap Example of Implementing heap sort (Cont….)sort (Cont….)

5 0

7 52 0

2 53 5

3 5

7 55 0

2 52 0

C allin g p o p H eap () w it h las t = 5d elet es 7 5 an d s t o res it in h [4 ]

C allin g p o p H eap () w it h las t = 4d elet es 5 0 an d s t o res it in h [3 ]

2 5

7 55 0

3 52 0

2 0

7 55 0

3 52 5

C allin g p o p H eap () w it h las t = 3d elet es 3 5 an d s t o res it in h [2 ]

C allin g p o p H eap () w it h las t = 2d elet es 2 5 an d s t o res it in h [1 ]

Page 10: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

10 Main IndexMain Index ContentsContents

Example of Heapifying a VectorExample of Heapifying a Vector

9

1 946 5

6 02 05 03 0

1 71 2

In it ial Vect o r

4

9

1 96 5

6 02 05 03 0

1 71 2

ad ju s t H eap () at 4 cau s es n o ch an ges(A )

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

11 Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Example of Heapifying a Vector Example of Heapifying a Vector (Cont…)(Cont…)

4

9

1 93 0

6 02 05 06 5

1 71 2

ad ju s t H eap () at 3 m o v es 3 0 d o w n(B )

5 0

4

9

1 93 0

1 72 06 5

6 01 2

ad ju s t H eap () at 2 m o v es 1 7 d o w n(C )

5 0

4

9

1 91 2

1 72 03 0

6 06 5

ad ju s t H eap () at 1 m o v es 1 2 d o w n t w o lev els(D )

1 9

4

6 5

91 2

1 72 03 0

6 05 0

ad ju s t H eap () at 0 m o v es 9 d o w n t h ree lev els(E )

Page 12: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

12 Main IndexMain Index ContentsContents12 Main IndexMain Index ContentsContents

File StructureFile Structure

R 0 R 1 R 2 R 3 R 4 R i R n - 2 R n - 1

c u rre n t P o s0 1 2 3 4 n -2 n -1

F ile a s a d ire c t a c c e s s s t ru c t u re

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

Page 13: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

13 Main IndexMain Index ContentsContents

Direct File AccessDirect File Access

b e g c u r e n d

o ffs e t o ffs e to ffs e to ffs e te x p a n d sfi l e

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

Page 14: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

14 Main IndexMain Index ContentsContents14 Main IndexMain Index ContentsContents

Implementing the bitVector Implementing the bitVector ClassClass

x x x x 0 x x

x x x x 1 x x

x x x

1 1 1 1 0 1 1

x 1 x x

0 0 0 0 1 0 0

x x x x 0 x x x

0

b it M as k (i)

xm em b er[v ect o rIn d ex(i)]

b it M as k (i) | m em b er[v ect o rIn d ex(i)]

x

1

~b it M as k (i)

xm em b er[v ect o rIn d ex(i)]

~b it M as k (i ) & m em b er[v ect o rIn d ex(i)]

S et b it i

C lear b it i

bitMask() returns an unsigned character value containing a 1 in the bit position representing i.

Page 15: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

15 Main IndexMain Index ContentsContents15 Main IndexMain Index ContentsContents

Lossless CompressionLossless Compression

T h is p ap er ... ... ... ... ... Su b m it t ed b y

J . Q . St u d en t

1 0 0 1 0 1 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 0 . . . . . . . . .1 0 1 0 0 1 0 1 1 0 1 1 1 0 1 0 1 1 0 0 1 1

T h is p ap er ... ... ... ... ... Su b m it t ed b y

J . Q . St u d en t

C o m p res s R eco n s t ru ct

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

Page 16: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

16 Main IndexMain Index ContentsContents

Lossy CompressionLossy Compression

1 1 0 1 0 1 1 0 1 0 1 0 1 1 1 0 1 0 0 0 1 0 . . . . . . . . .0 1 0 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 1 0 0 1

C o m p res s R eco n s t ru ct

1 0 1 1 0 1 0 1 0 1 1 1 0 1 . . .1 0 1 1 1 0 1 1 1 0 1 1 1 01 1 0 1 1 0 1 0 1 1 0 1 1 1 . . .1 1 0 1 0 1 1 0 1 1 0 1 0 11 1 0 1 1 0 1 1 0 1 1 0 1 1

1 0 1 1 0 1 1 1

1 0 1 1 0 1 0 1 0 1 1 1 0 1 . . .1 0 1 1 1 0 1 1 1 0 1 1 1 01 1 0 1 1 . . .1 1 0 1 0 1 1 0 1 1 0 1 0 11 1 0 1 1 0 1 1 0 1 1 0 1 1

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,

Page 17: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

17 Main IndexMain Index ContentsContents17 Main IndexMain Index ContentsContents

Building Huffman TreeBuilding Huffman Tree

a:1 6

f:3e:2 0

d :6

c:8

b :4

P rio rit y Q u eu e

Page 18: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

18 Main IndexMain Index ContentsContents18 Main IndexMain Index ContentsContents

Building Huffman Tree (Cont…)Building Huffman Tree (Cont…)

a:1 6

e:2 0

d :6

c:8

P rio rit y Q u eu e

f:3 b :4

7

Page 19: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

19 Main IndexMain Index ContentsContents

Building Huffman Tree (Cont…)Building Huffman Tree (Cont…)

Page 20: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

20 Main IndexMain Index ContentsContents20 Main IndexMain Index ContentsContents

Building Huffman Tree (Cont…)Building Huffman Tree (Cont…)

Page 21: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

21 Main IndexMain Index ContentsContents21 Main IndexMain Index ContentsContents

Summary Slide 1Summary 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.

Page 22: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

22 Main IndexMain Index ContentsContents22 Main IndexMain Index ContentsContents

Summary Slide 2Summary 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.

Page 23: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

23 Main IndexMain Index ContentsContents23 Main IndexMain Index ContentsContents

Summary Slide 3Summary 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

Page 24: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

24 Main IndexMain Index ContentsContents24 Main IndexMain Index ContentsContents

Summary Slide 4Summary 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

Page 25: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

25 Main IndexMain Index ContentsContents25 Main IndexMain Index ContentsContents

Summary Slide 5Summary 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.

Page 26: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

26 Main IndexMain Index ContentsContents26 Main IndexMain Index ContentsContents

Summary Slide 6Summary 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.

Page 27: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

27 Main IndexMain Index ContentsContents27 Main IndexMain Index ContentsContents

Summary Slide 7Summary 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.

Page 28: Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum

28 Main IndexMain Index ContentsContents28 Main IndexMain Index ContentsContents

Summary Slide 8Summary 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.