I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority...
Transcript of I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority...
![Page 1: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/1.jpg)
I Think You Have Some Priority Issues
ER Scheduling. How do we efficiently chose the most urgent case totreat next? Patients with more serious ailments should go first.
OS Context Switching. How does your operating system decide whichprocess to give resources to? Some applications are more important thanothers.
How can we solve these problems with the data structures we know?
1
![Page 2: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/2.jpg)
Priority Queue
Priority Queue
A collection of ordered elements that provides fast access to theminimum (or maximum) element.
public class PriorityQueue<E> implements Queue<E>
PriorityQueue<E>() constructs an empty queueadd(E value) adds value in sorted order to the queuepeek() returns minimum element in queueremove() removes/returns minimum element in queuesize() returns the number of elements in queue
Queue <String > tas = new PriorityQueue <String >();
tas.add("Sam");
tas.add("Maria");
tas.remove ();3
![Page 3: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/3.jpg)
Priority Queue
Priority Queue
A collection of ordered elements that provides fast access to theminimum (or maximum) element.
public class PriorityQueue<E> implements Queue<E>
PriorityQueue<E>() constructs an empty queueadd(E value) adds value in sorted order to the queuepeek() returns minimum element in queueremove() removes/returns minimum element in queuesize() returns the number of elements in queue
Queue <String > tas = new PriorityQueue <String >();
tas.add("Sam");
tas.add("Maria");
tas.remove (); // "Maria"3
![Page 4: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/4.jpg)
Priority Queue Example
What does this code print?
Queue <TA> tas = new PriorityQueue <TA >();
tas.add(new TA("Dylan", 7));
tas.add(new TA("Yuma", 15));
tas.add(new TA("Cherie", 3));
System.out.println(tas);
Prints: [Cherie: 3, Yuma: 15, Dylan: 7]
Common Gotchas
• Elements must be Comparable.• toString doesn’t do what you expect! Use remove instead.
4
![Page 5: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/5.jpg)
Priority Queue Example
What does this code print?
Queue <TA> tas = new PriorityQueue <TA >();
tas.add(new TA("Dylan", 7));
tas.add(new TA("Yuma", 15));
tas.add(new TA("Cherie", 3));
System.out.println(tas);
Prints: [Cherie: 3, Yuma: 15, Dylan: 7]
Common Gotchas
• Elements must be Comparable.• toString doesn’t do what you expect! Use remove instead.
4
![Page 6: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/6.jpg)
File Compression
Compression
Process of encoding information so that it takes up less space.
Compression applies to many things!
• Store photos without taking up the whole hard-drive• Reduce size of email attachment• Make web pages smaller so they load faster• Make voice calls over a low-bandwidth connection (cell, Skype)
Common compression programs:• WinZip, WinRar for Windows• zip
6
![Page 7: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/7.jpg)
ASCII
ASCII (American Standard Code for Information Interchange)
Standardized code for mapping characters to integers
We need to represent characters in binary so computers can read them.
• Most text files on your computer are in ASCII.
Every character is represented by a byte (8 bits).
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
7
![Page 8: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/8.jpg)
ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
What is the binary representation of the following String?cab z
Answer
8
![Page 9: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/9.jpg)
ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
What is the binary representation of the following String?cab z
Answer01100011
8
![Page 10: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/10.jpg)
ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
What is the binary representation of the following String?cab z
Answer01100011 01100001
8
![Page 11: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/11.jpg)
ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
What is the binary representation of the following String?cab z
Answer01100011 01100001 01100010
8
![Page 12: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/12.jpg)
ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
What is the binary representation of the following String?cab z
Answer01100011 01100001 01100010 00100000
8
![Page 13: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/13.jpg)
ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
What is the binary representation of the following String?cab z
Answer01100011 01100001 01100010 00100000 01111010
8
![Page 14: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/14.jpg)
ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
What is the binary representation of the following String?cab z
Answer0110001101100001011000100010000001111010
8
![Page 15: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/15.jpg)
Another ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
How do we read the following binary as ASCII?011000010110001101100101
Answer
9
![Page 16: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/16.jpg)
Another ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
How do we read the following binary as ASCII?01100001 01100011 01100101
Answer
9
![Page 17: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/17.jpg)
Another ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
How do we read the following binary as ASCII?01100001 01100011 01100101
Answera
9
![Page 18: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/18.jpg)
Another ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
How do we read the following binary as ASCII?01100001 01100011 01100101
Answerac
9
![Page 19: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/19.jpg)
Another ASCII Example
Character ASCII value Binary Representation‘ ’ 32 00100000‘a’ 97 01100001‘b’ 98 01100010‘c’ 99 01100011‘e’ 101 01100101‘z’ 122 01111010
How do we read the following binary as ASCII?01100001 01100011 01100101
Answerace
9
![Page 20: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/20.jpg)
Huffman Idea
Huffman’s Insight
Use variable length encodings for different characters to takeadvantage of frequencies in which characters appear.
• Make more frequent characters take up less space.• Don’t have codes for unused characters.• Some characters may end up with longer encodings,
but this should happen infrequently.
10
![Page 21: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/21.jpg)
Huffman Encoding
• Create a “Huffman Tree” that gives a good binary representation foreach character.
• The path from the root to the character leaf is the encoding for thatcharacter; left means 0, right means 1.
ASCII TableCharacter Binary Representation
‘ ’ 00100000‘a’ 01100001‘b’ 01100010‘c’ 01100011‘e’ 01100101‘z’ 01111010
Huffman Tree
0
0 1
0 1
1
‘b’
‘c’ ‘ ’
‘a’
11
![Page 22: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/22.jpg)
Homework 8: Huffman Coding
Homework 8 asks you to write a class that manages creating and usingthis Huffman code.
(A) Create a Huffman Code from a file and compress it.(B) Decompress the file to get original contents.
12
![Page 23: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/23.jpg)
Part A: Making a HuffmanCode Overview
Input File Contentsbad cab
Step 1: Count the occurrences of each character in file{‘ ’=1, ‘a’=2, ‘b’=2, ‘c’=1, ‘d’=1}
Step 2: Make leaf nodes for all the characters put them in a PriorityQueue
pq ←− ‘ ’freq: 1
‘c’freq: 1
‘d’freq: 1
‘a’freq: 2
‘b’freq: 2 ←−
Step 3: Use Huffman Tree building algorithm (described in a couple slides)Step 4: Save encoding to .code file to encode/decode later.
{‘d’=00, ‘a’=01, ‘b’=10, ‘ ’=110, ‘c’=111}
Step 5: Compress the input file using the encodingsCompressed Output: 1001001101110110
13
![Page 24: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/24.jpg)
Part A: Making a HuffmanCode Overview
Input File Contentsbad cab
Step 1: Count the occurrences of each character in file{‘ ’=1, ‘a’=2, ‘b’=2, ‘c’=1, ‘d’=1}
Step 2: Make leaf nodes for all the characters put them in a PriorityQueue
pq ←− ‘ ’freq: 1
‘c’freq: 1
‘d’freq: 1
‘a’freq: 2
‘b’freq: 2 ←−
Step 3: Use Huffman Tree building algorithm (described in a couple slides)Step 4: Save encoding to .code file to encode/decode later.
{‘d’=00, ‘a’=01, ‘b’=10, ‘ ’=110, ‘c’=111}
Step 5: Compress the input file using the encodingsCompressed Output: 1001001101110110
13
![Page 25: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/25.jpg)
Part A: Making a HuffmanCode Overview
Input File Contentsbad cab
Step 1: Count the occurrences of each character in file{‘ ’=1, ‘a’=2, ‘b’=2, ‘c’=1, ‘d’=1}
Step 2: Make leaf nodes for all the characters put them in a PriorityQueue
pq ←− ‘ ’freq: 1
‘c’freq: 1
‘d’freq: 1
‘a’freq: 2
‘b’freq: 2 ←−
Step 3: Use Huffman Tree building algorithm (described in a couple slides)Step 4: Save encoding to .code file to encode/decode later.
{‘d’=00, ‘a’=01, ‘b’=10, ‘ ’=110, ‘c’=111}
Step 5: Compress the input file using the encodingsCompressed Output: 1001001101110110
13
![Page 26: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/26.jpg)
Part A: Making a HuffmanCode Overview
Input File Contentsbad cab
Step 1: Count the occurrences of each character in file{‘ ’=1, ‘a’=2, ‘b’=2, ‘c’=1, ‘d’=1}
Step 2: Make leaf nodes for all the characters put them in a PriorityQueue
pq ←− ‘ ’freq: 1
‘c’freq: 1
‘d’freq: 1
‘a’freq: 2
‘b’freq: 2 ←−
Step 3: Use Huffman Tree building algorithm (described in a couple slides)
Step 4: Save encoding to .code file to encode/decode later.{‘d’=00, ‘a’=01, ‘b’=10, ‘ ’=110, ‘c’=111}
Step 5: Compress the input file using the encodingsCompressed Output: 1001001101110110
13
![Page 27: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/27.jpg)
Part A: Making a HuffmanCode Overview
Input File Contentsbad cab
Step 1: Count the occurrences of each character in file{‘ ’=1, ‘a’=2, ‘b’=2, ‘c’=1, ‘d’=1}
Step 2: Make leaf nodes for all the characters put them in a PriorityQueue
pq ←− ‘ ’freq: 1
‘c’freq: 1
‘d’freq: 1
‘a’freq: 2
‘b’freq: 2 ←−
Step 3: Use Huffman Tree building algorithm (described in a couple slides)Step 4: Save encoding to .code file to encode/decode later.
{‘d’=00, ‘a’=01, ‘b’=10, ‘ ’=110, ‘c’=111}
Step 5: Compress the input file using the encodingsCompressed Output: 1001001101110110
13
![Page 28: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/28.jpg)
Part A: Making a HuffmanCode Overview
Input File Contentsbad cab
Step 1: Count the occurrences of each character in file{‘ ’=1, ‘a’=2, ‘b’=2, ‘c’=1, ‘d’=1}
Step 2: Make leaf nodes for all the characters put them in a PriorityQueue
pq ←− ‘ ’freq: 1
‘c’freq: 1
‘d’freq: 1
‘a’freq: 2
‘b’freq: 2 ←−
Step 3: Use Huffman Tree building algorithm (described in a couple slides)Step 4: Save encoding to .code file to encode/decode later.
{‘d’=00, ‘a’=01, ‘b’=10, ‘ ’=110, ‘c’=111}
Step 5: Compress the input file using the encodingsCompressed Output: 1001001101110110
13
![Page 29: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/29.jpg)
Step 1: Count Character Occurrences
We do this step for you
Input Filebad cab
Generate Counts Array:
index 0 1
value 0 0...
32
1...
97 98 99 100 101
2 2 1 1 0...
This is super similar to LetterInventory but works for all characters!
14
![Page 30: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/30.jpg)
Step 2: Create PriorityQueue
• Store each character and its frequency in a HuffmanNode object.• Place all the HuffmanNodes in a PriorityQueue so that they are in
ascending order with respect to frequency
pq ←− ‘ ’freq: 1
‘c’freq: 1
‘d’freq: 1
‘a’freq: 2
‘b’freq: 2 ←−
15
![Page 31: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/31.jpg)
Step 3: Remove and Merge
pq ←− ‘ ’freq: 1
‘c’freq: 1
‘d’freq: 1
‘a’freq: 2
‘b’freq: 2 ←−
16
![Page 32: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/32.jpg)
Step 3: Remove and Merge
freq: 2
‘ ’freq: 1
‘c’freq: 1
pq ←− ‘d’freq: 1
‘a’freq: 2
‘b’freq: 2 ←−
16
![Page 33: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/33.jpg)
Step 3: Remove and Merge
pq ←− ‘d’freq: 1
‘a’freq: 2
‘b’freq: 2
freq: 2
‘ ’freq: 1
‘c’freq: 1
←−
16
![Page 34: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/34.jpg)
Step 3: Remove and Merge
freq: 3
‘d’freq: 1
‘a’freq: 2
pq ←− ‘b’freq: 2
freq: 2
‘ ’freq: 1
‘c’freq: 1
←−
16
![Page 35: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/35.jpg)
Step 3: Remove and Merge
pq ←− ‘b’freq: 2
freq: 2
‘ ’freq: 1
‘c’freq: 1
freq: 3
‘d’freq: 1
‘a’freq: 2
←−
16
![Page 36: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/36.jpg)
Step 3: Remove and Merge
freq: 4
‘b’freq: 2 freq: 2
‘ ’freq: 1
‘c’freq: 1
pq ←−
freq: 3
‘d’freq: 1
‘a’freq: 2
←−
16
![Page 37: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/37.jpg)
Step 3: Remove and Merge
pq ←−
freq: 3
‘d’freq: 1
‘a’freq: 2
freq: 4
‘b’freq: 2 freq: 2
‘ ’freq: 1
‘c’freq: 1
←−
16
![Page 38: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/38.jpg)
Step 3: Remove and Merge
freq: 7
freq: 3
‘d’freq: 1
‘a’freq: 2
freq: 4
‘b’freq: 2 freq: 2
‘ ’freq: 1
‘c’freq: 1
pq ←− ←−
16
![Page 39: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/39.jpg)
Step 3: Remove and Merge
pq ←−
freq: 7
freq: 3
‘d’freq: 1
‘a’freq: 2
freq: 4
‘b’freq: 2 freq: 2
‘ ’freq: 1
‘c’freq: 1
←−
16
![Page 40: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/40.jpg)
Step 3: Remove and Merge
pq ←−
freq: 7
freq: 3
‘d’freq: 1
‘a’freq: 2
freq: 4
‘b’freq: 2 freq: 2
‘ ’freq: 1
‘c’freq: 1
←−
• What is the relationship between frequency in file and binaryrepresentation length?
16
![Page 41: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/41.jpg)
Step 3: Remove and Merge Algorithm
Algorithm Pseudocode
while P.Q. size > 1:
remove two nodes with lowest frequency
combine into a single node
put that node back in the P.Q.
17
![Page 42: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/42.jpg)
Step 4: Print Encodings
Save the tree to a file to save the encodings for the characters we made.
0 1
0
0
0 1
1
1
‘d’ ‘a’ ‘b’
‘ ’ ‘c’
18
![Page 43: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/43.jpg)
Step 4: Print Encodings
Save the tree to a file to save the encodings for the characters we made.
0 1
0
0
0 1
1
1
‘d’ ‘a’ ‘b’
‘ ’ ‘c’
Output of write
18
![Page 44: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/44.jpg)
Step 4: Print Encodings
Save the tree to a file to save the encodings for the characters we made.
0 1
0
0
0 1
1
1
‘d’ ‘a’ ‘b’
‘ ’ ‘c’
Output of write
100
00
18
![Page 45: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/45.jpg)
Step 4: Print Encodings
Save the tree to a file to save the encodings for the characters we made.
0 1
0
0
0 1
1
1
‘d’ ‘a’ ‘b’
‘ ’ ‘c’
Output of write
100
00
97
01
18
![Page 46: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/46.jpg)
Step 4: Print Encodings
Save the tree to a file to save the encodings for the characters we made.
0 1
0
0
0 1
1
1
‘d’ ‘a’ ‘b’
‘ ’ ‘c’
Output of write
100
00
97
01
98
10
18
![Page 47: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/47.jpg)
Step 4: Print Encodings
Save the tree to a file to save the encodings for the characters we made.
0 1
0
0
0 1
1
1
‘d’ ‘a’ ‘b’
‘ ’ ‘c’
Output of write
100
00
97
01
98
10
32
110
18
![Page 48: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/48.jpg)
Step 4: Print Encodings
Save the tree to a file to save the encodings for the characters we made.
0 1
0
0
0 1
1
1
‘d’ ‘a’ ‘b’
‘ ’ ‘c’
Output of write
100
00
97
01
98
10
32
110
99
111
18
![Page 49: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/49.jpg)
Step 5: Compress the File
We do this step for you
Take the original file and the .code file produced in last step to translateinto the new binary encoding.
Input Filebad cab
Compressed Output
Huffman Encoding
100
00
97
01
98
10
32
110
99
111
19
![Page 50: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/50.jpg)
Step 5: Compress the File
We do this step for you
Take the original file and the .code file produced in last step to translateinto the new binary encoding.
Input Filebad cab
Compressed Output
Huffman Encoding
100 'd'
00
97 'a'
01
98 'b'
10
32 ' '
110
99 'c'
111
19
![Page 51: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/51.jpg)
Step 5: Compress the File
We do this step for you
Take the original file and the .code file produced in last step to translateinto the new binary encoding.
Input Filebad cab
Compressed Output10 01 100 110 111 01 10
Huffman Encoding
100 'd'
00
97 'a'
01
98 'b'
10
32 ' '
110
99 'c'
111
19
![Page 52: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/52.jpg)
Step 5: Compress the File
We do this step for you
Take the original file and the .code file produced in last step to translateinto the new binary encoding.
Input Filebad cab
Compressed Output10 01 100 110 111 01 10
Uncompressed Output01100010 01100001 0110010000100000 01100011 0110000101100010
Huffman Encoding
100 'd'
00
97 'a'
01
98 'b'
10
32 ' '
110
99 'c'
111
19
![Page 53: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/53.jpg)
Part B: Decompressing the File
Step 1: Reconstruct the Huffman tree from the code fileStep 2: Translate the compressed bits back to their character values.
20
![Page 54: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/54.jpg)
Step 1: Reconstruct the Huffman Tree
Now are just given the code file produced by our program and we need toreconstruct the tree.
Input code File9701011003210111211
Initially the tree is empty
0
0 1
0 1
1
‘a’
‘e’ ‘ ’
‘p’
21
![Page 55: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/55.jpg)
Step 1: Reconstruct the Huffman Tree
Now are just given the code file produced by our program and we need toreconstruct the tree.
Input code File9701011003210111211
Tree after processing first pair
0
0 1
0 1
1
‘a’
‘e’ ‘ ’
‘p’
21
![Page 56: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/56.jpg)
Step 1: Reconstruct the Huffman Tree
Now are just given the code file produced by our program and we need toreconstruct the tree.
Input code File9701011003210111211
Tree after processing second pair
0
0 1
0 1
1
‘a’
‘e’ ‘ ’
‘p’
21
![Page 57: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/57.jpg)
Step 1: Reconstruct the Huffman Tree
Now are just given the code file produced by our program and we need toreconstruct the tree.
Input code File9701011003210111211
Tree after processing third pair
0
0 1
0 1
1
‘a’
‘e’ ‘ ’
‘p’
21
![Page 58: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/58.jpg)
Step 1: Reconstruct the Huffman Tree
Now are just given the code file produced by our program and we need toreconstruct the tree.
Input code File9701011003210111211
Tree after processing last pair
0
0 1
0 1
1
‘a’
‘e’ ‘ ’
‘p’
21
![Page 59: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/59.jpg)
Step 2 Example
After building up tree, we will read the compressed file bit by bit.
Input0101110110101011100
Output0
0 1
0 1
1
‘a’
‘e’ ‘ ’
‘p’
22
![Page 60: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/60.jpg)
Step 2 Example
After building up tree, we will read the compressed file bit by bit.
Input0101110110101011100
Outputa papa ape
0
0 1
0 1
1
‘a’
‘e’ ‘ ’
‘p’
22
![Page 61: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/61.jpg)
Working with Bits? That Sounds a Little Bit Hard
Reading bits in Java is kind of tricky, we are providing a class to help!
public class BitInputStream
BitInputStream(String file) Creates a stream of bits from filereadBit() Reads and returns the next bit in the
stream
23
![Page 62: I Think You Have Some Priority Issues › courses › cse143 › ... · 1. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum](https://reader030.fdocuments.us/reader030/viewer/2022041109/5f0e43337e708231d43e644b/html5/thumbnails/62.jpg)
Review - Homework 8
Part A: Compression
public HuffmanTree(int[] counts)
• Slides 13-17
public void write(PrintStream out)
• Slide 18
Part B: Decompression
public HuffmanTree(Scanner input)
• Slide 21
public void decode(BitInputStream input ,
PrintStream output ,
int eof)
• Slide 22 24