Comparable + Huffman

Comparable
 + Huffman
Hitesh Boinpally
Summer 2023
Agenda
Comparable
P3 - Huffman
Lesson 12 - Summer 2023
2
Agenda
Comparable
P3 - Huffman
Lesson 12 - Summer 2023
3
Interfaces Review
Define set of behavior a class can implement
“Contract”, “Certification”
List
 and 
Set
 are some interfaces we’ve used
To utilize:
public class Tesla 
implements Car
Says that 
Tesla
 “implements” the 
Car
interface
Lesson 12 - Summer 2023
4
 The 
Comparable
 Interface
Say you had a 
FootballTeam
 class and
wanted to sort them
How could you tell Java how to sort them?
Lesson 12 - Summer 2023
5
 The 
Comparable
 Interface
Say you had a 
FootballTeam
 class and
wanted to sort them
How could you tell Java how to sort them?
Utilize the 
Comparable
 interface!
Fixed definition of how to compare objects
This is needed to allow objects to be added
to 
TreeSets
 and 
TreeMaps
Lesson 12 - Summer 2023
6
Agenda
Comparable
P3 - Huffman
Lesson 12 - Summer 2023
7
undefined
Priority
 
Queues
 
and
 
Huffman
 
Encoding
Introduction
 
to
 
the
 
Final
 
Project
Hunter
 
Schafer
CSE 
143,
 
Autumn
 
2021
Priority
 
Queue
Priority
 
Queue
A
 
collection of
 
ordered elements that
 
provides fast access
 
to
the
 
minimum
 
(or
 
maximum)
 
element.
public
 
class
 
PriorityQueue<E>
 
implements
 
Queue<E>
Queue<String>
 
tas
 
=
 
new
 
PriorityQueue<String>();
tas.add(
"Watson"
);
tas.add(
"Sherlock"
);
tas.remove();
1
Priority
 
Queue
Priority
 
Queue
A
 
collection of
 
ordered elements that
 
provides fast access
 
to
the
 
minimum
 
(or
 
maximum)
 
element.
public
 
class
 
PriorityQueue<E>
 
implements
 
Queue<E>
Queue<String>
 
tas
 
=
 
new
 
PriorityQueue<String>();
tas.add(
"Watson"
);
tas.add(
"Sherlock"
);
tas.remove();
 
//
 
"Sherlock"
1
Final
 
Project:
 
Huffman
 
Coding
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
2
ASCII
ASCII
 
(American
 
Standard
 
Code
 
for
 
Information
 
Interchange)
Standardized
 
code
 for 
mapping
 
characters
 
to
 integers
Many
 
text
 files
 
on
 
your
 
computer are
 
in
 ASCII.
But,
 
computers
 
need
 
numbers
 represented 
in
 binary!
3
ASCII
ASCII
 
(American
 
Standard
 
Code
 
for
 
Information
 
Interchange)
Standardized
 
code
 for 
mapping
 
characters
 
to
 integers
Many
 
text
 files
 
on
 
your
 
computer are
 
in
 ASCII.
But,
 
computers
 
need
 
numbers
 represented 
in
 binary!
Every
 
character
 
is
 
represented
 
by
 
a
 
byte
 
(8
 
bits).
3
ASCII
 
Example
4
What
 
is
 
the
 
binary
 
representation
 
of
 
the
 
following
 
String?
cab
 z
ASCII
 
Example
4
What
 
is
 
the
 
binary
 
representation
 
of
 
the
 
following
 
String?
c
ab
 
z
Answer
01100011
ASCII
 
Example
4
What
 
is
 
the
 
binary
 
representation
 
of
 
the
 
following
 
String?
c
a
b
 
z
Answer
01100011
 
01100001
ASCII
 
Example
4
What
 
is
 
the
 
binary
 
representation
 
of
 
the
 
following
 
String?
ca
b
 z
Answer
01100011
 
01100001
 
01100010
ASCII
 
Example
What
 
is
 
the
 
binary
 
representation
 
of
 
the
 
following
 
String?
cab
 z
4
Answer
01100011
 
01100001
 
01100010
 
00100000
ASCII
 
Example
4
What
 
is
 
the
 
binary
 
representation
 
of
 
the
 
following
 
String?
cab
 
z
Answer
01100011
 
01100001
 
01100010
 
00100000
 
01111010
ASCII
 
Example
4
What
 
is
 
the
 
binary
 
representation
 
of
 
the
 
following
 
String?
cab
 z
Answer
0110001101100001011000100010000001111010
Another
 
ASCII
 Example
5
How
 
do
 
we
 
read
 
the
 
following
 
binary
 
as
 
ASCII?
011000010110001101100101
Another
 
ASCII
 Example
How
 
do
 
we
 
read
 
the
 
following
 
binary
 
as
 
ASCII?
01100001
 
01100011
 
01100101
Answer
5
Another
 
ASCII
 Example
How
 
do
 
we
 
read
 
the
 
following
 
binary
 
as
 
ASCII?
01100001
 
01100011
 
01100101
Answer
a
5
Another
 
ASCII
 Example
5
How
 
do
 
we
 
read
 
the
 
following
 
binary
 
as
 
ASCII?
01100001
 
01100011
 
01100101
Answer
ac
Another
 
ASCII
 Example
5
How
 
do
 
we
 
read
 
the
 
following
 
binary
 
as
 
ASCII?
01100001
 
01100011
 
01100101
Answer
ace
Huffman
 
Idea
Huffman’s
 
Insight
Use
 
variable 
length
 
encodings
 for 
different
 
characters
 
to
 
take
advantage
 
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.
6
Huffman
 
Encoding
Create
 
a
 
“Huffman
 
Tree”
 
that
 
gives
 
a
 
good
 
binary
 
representation
for
 
each
 
character.
The
 
path
 
from
 
the
 
root
 
to
 
the
 
character
 
leaf
 
is
 
the
 
encoding
 
for
that 
character;
 
left
 
means
 
0,
 
right 
means
 
1.
ASCII
 
Table
Huffman 
Tree
0
0
1
0
1
1
b
c
 
a
7
undefined
Final
 
Project:
 
Huffman
 
Coding
8
The 
final
 
project
 
asks
 
you
 
to
 
write
 
a
 
class
 
that
 
manages
 
creating
 
and
using this
 
Huffman
 
code.
(A)
Create
 
a
 
Huffman
 
Code
 
from
 
a
 
file
 
and
 
compress
 it.
(B)
Decompress
 
the
 file 
to
 
get
 
original
 
contents.
Part
 
A:
 
Making
 
a
 HuffmanCode Overview
9
Input
 
File
 
Contents
bad
 cab
Part
 
A:
 
Making
 
a
 HuffmanCode Overview
9
Input
 
File
 
Contents
bad
 cab
Step
 
1:
 
Count
 
the
 
occurrences
 
of
 
each
 
character
 
in
 
file
{'
 
'=1,
 
'a'=2,
 
'b'=2,
 
'c'=1,
 
'd'=1}
Part
 
A:
 
Making
 
a
 HuffmanCode Overview
Input
 
File
 
Contents
bad
 cab
9
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.
 
Place
 
in
 
a
 
PriorityQueue
pq
 
 
Part
 
A:
 
Making
 
a
 HuffmanCode Overview
Input
 
File
 
Contents
bad
 cab
9
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.
 
Place
 
in
 
a
 
PriorityQueue
pq
 
 
Step
 
3:
 
Use
 
Huffman
 
Tree
 
building
 
algorithm
 
(described
 
soon)
Part
 
A:
 
Making
 
a
 HuffmanCode Overview
Input
 
File
 
Contents
bad
 cab
9
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.
 
Place
 
in
 
a
 
PriorityQueue
pq
 
 
Step
 
3:
 
Use
 
Huffman
 
Tree
 
building
 
algorithm
 
(described
 
soon)
Step
 
4:
 
Save
 
encoding
 
to
 
.code
 
file
 
to
 
encode/decode
 
later.
{'d'=00,
 
'a'=01,
 
'b'=10,
 
'
 
'=110,
 
'c'=111}
Part
 
A:
 
Making
 
a
 HuffmanCode Overview
Input
 
File
 
Contents
bad
 cab
9
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.
 
Place
 
in
 
a
 
PriorityQueue
pq
 
 
Step
 
3:
 
Use
 
Huffman
 
Tree
 
building
 
algorithm
 
(described
 
soon)
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
 
encodings
Compressed
 
Output: 1001001101110110
Step
 
1:
 
Count
 
Character
 
Occurrences
We 
do
 
this
 
step
 
for
 
you
Input
 
File
bad
 cab
Generate
 
Counts
 
Array:
index
 
0
 
1
...
32
97
 
98
 
99
 
100
  
101
10
...
Step
 
2:
 
Create
 
PriorityQueue
Store
 
each
 
character
 
and
 
its
 
frequency
 
in
 
a
 
HuffmanNode
object.
Place
 
all
 
the
 
HuffmanNode
s
 
in
 
a
 
PriorityQueue
 
so
 
that
 
they
are
 
in
 
ascending
 
order
 
with
 
respect
 
to
 
frequency
11
Step
 
3:
 
Remove
 
and
 
Merge
pq
 
12
Step
 
3:
 
Remove
 
and
 
Merge
freq:
 
2
 
 
 
freq:
 
1
  
c
 
freq:
 
1
pq
 
12
Step
 
3:
 
Remove
 
and
 
Merge
pq
 
12
Step
 
3:
 
Remove
 
and
 
Merge
freq:
 
3
  
d
 
freq:
 
1
  
a
 
freq:
 
2
pq
 
  
b
 
freq:
 
2
freq:
 
2
 
 
 
freq:
 
1
  
c
 
freq:
 
1
12
Step
 
3:
 
Remove
 
and
 
Merge
pq
 
12
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
12
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
12
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
 
12
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
12
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
12
What
 
is
 
the
 
relationship
 
between 
frequency
 
in
 
file
 
and
 
binary
representation
 
length?
Step
 
3:
 
Remove
 
and
 
Merge
 
Algorithm
13
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.
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
14
Step
 
4:
 
Print
 
Encodings
0
1
0
0
0
1
1
1
d
a
b
 
c
Save
 
the
 tree
 
to
 
a
 file
 
to
 
save
 
the
 
encodings
 
for
 
the
 
characters
 
we
made.
Output
 
of
 
save
14
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
14
Output
 
of
 
save
100
00
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
14
Output
 
of
 
save
100
00
97
01
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
14
Output
 
of
 
save
100
00
97
01
98
10
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
14
Output
 
of
 
save
100
00
97
01
98
10
32
110
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
14
Output
 
of
 
save
100
00
97
01
98
10
32
110
99
111
Step
 
5:
 
Compress
 
the
 
File
We 
do
 
this
 
step
 
for
 
you
Take
 
the
 
original
 
file
 
and
 
the
 
.code
 
file
 
produced
 
in
 
last
 
step
 
to
translate
 
into
 
the
 
new
 
binary
 
encoding.
Input
 
File
bad 
cab
Compressed
 
Output
Huffman Encoding
15
100
00
97
01
98
10
32
110
99
111
Step
 
5:
 
Compress
 
the
 
File
We 
do
 
this
 
step
 
for
 
you
Take
 
the
 
original
 
file
 
and
 
the
 
.code
 
file
 
produced
 
in
 
last
 
step
 
to
translate
 
into
 
the
 
new
 
binary
 
encoding.
Input
 
File
bad 
cab
Compressed
 
Output
15
Step
 
5:
 
Compress
 
the
 
File
15
We 
do
 
this
 
step
 
for
 
you
Take
 
the
 
original
 
file
 
and
 
the
 
.code
 
file
 
produced
 
in
 
last
 
step
 
to
translate
 
into
 
the
 
new
 
binary
 
encoding.
Input
 
File
bad 
cab
Compressed
 
Output
10
 
01
 
00
 
110
 
111
 
01
 
10
Step
 
5:
 
Compress
 
the
 
File
15
We 
do
 
this
 
step
 
for
 
you
Take
 
the
 
original
 
file
 
and
 
the
 
.code
 
file
 
produced
 
in
 
last
 
step
 
to
translate
 
into
 
the
 
new
 
binary
 
encoding.
Input
 
File
bad 
cab
Compressed
 
Output
10
 
01
 
00
 
110
 
111
 
01
 
10
Uncompressed
 
Output
01100010
 
01100001
 
01100100
00100000
 
01100011
 
01100001
01100010
undefined
Part
 
B:
 
Decompressing
 
the
 
File
16
Step
 
1:
 
Reconstruct
 
the
 
Huffman
 
tree
 
from
 
the
 
code
 
file
Step
 
2:
 
Translate
 
the
 
compressed
 
bits
 
back
 
to
 
their
 
character
 
values.
Step
 
1:
 
Reconstruct
 
the
 
Huffman
 
Tree
17
Input
 
code 
File
97
0
101
100
32
101
112
11
Now
 
are
 
just
 
given
 
the
 
code
 
file 
produced
 
by
 
our
 
program
 
and
 
we
need
 
to
 
reconstruct
 
the
 
tree.
Initially 
the
 
tree
 
is
 empty
0
0
1
0
1
1
a
e
 
p
Step
 
1:
 
Reconstruct
 
the
 
Huffman
 
Tree
Input
 
code 
File
97
0
101
100
32
101
112
11
Now
 
are
 
just
 
given
 
the
 
code
 
file 
produced
 
by
 
our
 
program
 
and
 
we
need
 
to
 
reconstruct
 
the
 
tree.
Tree
 
after
 
processing
 
first
 
pair
0
0
1
0
1
1
a
17
e
 
p
Step
 
1:
 
Reconstruct
 
the
 
Huffman
 
Tree
Now
 
are
 
just
 
given
 
the
 
code
 
file 
produced
 
by
 
our
 
program
 
and
 
we
need
 
to
 
reconstruct
 
the
 
tree.
Input
 
code 
File
97
0
101
100
32
101
112
11
Tree
 
after
 
processing
 
second
pair
0
0
1
0
1
1
a
e
17
 
p
Step
 
1:
 
Reconstruct
 
the
 
Huffman
 
Tree
Input
 
code 
File
97
0
101
100
32
101
112
11
Now
 
are
 
just
 
given
 
the
 
code
 
file 
produced
 
by
 
our
 
program
 
and
 
we
need
 
to
 
reconstruct
 
the
 
tree.
Tree
 
after
 
processing
 
third
 
pair
0
0
1
0
1
1
a
e
 
17
p
Step
 
1:
 
Reconstruct
 
the
 
Huffman
 
Tree
Input
 
code 
File
97
0
101
100
32
101
112
11
Now
 
are
 
just
 
given
 
the
 
code
 
file 
produced
 
by
 
our
 
program
 
and
 
we
need
 
to
 
reconstruct
 
the
 
tree.
Tree
 
after
 
processing
 
last
 
pair
0
0
1
0
1
1
a
e
 
p
17
Step
 
2
 
Example
After
 
building
 
up
 
tree,
 
we
 
will
 
read
 
the
 
compressed
 
file
 
bit
 
by
 
bit.
Input
0101110110101011100
Output
0
0
1
0
1
1
a
e
 
p
18
Step
 
2
 
Example
After
 
building
 
up
 
tree,
 
we
 
will
 
read
 
the
 
compressed
 
file
 
bit
 
by
 
bit.
Input
0101110110101011100
Output
a
 
papa
 
ape
0
0
1
0
1
1
a
e
 
p
18
Working
 
with
 Bits?
 
That
 Sounds a
 
Little
 
Bit 
Hard
19
Reading
 
bits
 
in
 
Java
 
is
 
kind
 
of
 
tricky,
 
we
 
are
 
providing
 
a
 
class
 
to
 
help!
public
 
class
 
BitInputStream
Review
 
-
 
Final
 
Project
Part
 
A:
 
Compression
public
 
HuffmanCode(
int
[]
 
counts)
Slides
 
11-
13
public
 
void
 
save(PrintStream
 
out)
Slide
 
14
Part
 
B:
 
Decompression
public
 
HuffmanCode(Scanner
 
input)
Slide
 
17
public
 
void
 
translate(BitInputStream
 
in,
PrintStream
 
out)
Slide
 
18
20
Slide Note
Embed
Share

In this content, we delve into Java interfaces and explore the Comparable interface. We discuss how to define a set of behaviors a class can implement using interfaces and the importance of utilizing the Comparable interface for sorting objects. The content also touches on priority queues and Huffman encoding, providing an introduction to these concepts. With images and examples, the material aims to enhance understanding and application of these Java concepts in a Summer 2023 lesson.

  • Java
  • Interfaces
  • Comparable
  • Sorting
  • Priority Queues

Uploaded on Feb 20, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Comparable + Huffman Hitesh Boinpally Summer 2023

  2. Agenda Comparable P3 - Huffman Lesson 12 - Summer 2023 2

  3. Agenda Comparable P3 - Huffman Lesson 12 - Summer 2023 3

  4. Interfaces Review Define set of behavior a class can implement Contract , Certification List and Set are some interfaces we ve used To utilize: public class Tesla implements Car Says that Tesla implements the Car interface Lesson 12 - Summer 2023 4

  5. The Comparable Interface Say you had a FootballTeam class and wanted to sort them How could you tell Java how to sort them? Lesson 12 - Summer 2023 5

  6. The Comparable Interface Say you had a FootballTeam class and wanted to sort them How could you tell Java how to sort them? Utilize the Comparable interface! Fixed definition of how to compare objects This is needed to allow objects to be added to TreeSetsand TreeMaps Lesson 12 - Summer 2023 6

  7. Agenda Comparable P3 - Huffman Lesson 12 - Summer 2023 7

  8. Priority Queues and Huffman Encoding Introduction to the Final Project Hunter Schafer CSE 143, Autumn 2021

  9. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum (or maximum) element. public class PriorityQueue<E> implements Queue<E> constructs an empty queue PriorityQueue<E>() add(E value) adds value in sorted order to the queue returns minimum element in queue peek() removes/returns minimum element in queue remove() returns the number of elements in queue size() Queue<String> tas = new PriorityQueue<String>(); tas.add("Watson"); tas.add("Sherlock"); tas.remove(); 1

  10. Priority Queue Priority Queue A collection of ordered elements that provides fast access to the minimum (or maximum) element. public class PriorityQueue<E> implements Queue<E> constructs an empty queue PriorityQueue<E>() add(E value) adds value in sorted order to the queue returns minimum element in queue peek() removes/returns minimum element in queue remove() returns the number of elements in queue size() Queue<String> tas = new PriorityQueue<String>(); tas.add("Watson"); tas.add("Sherlock"); tas.remove(); // "Sherlock" 1

  11. Final Project: Huffman Coding

  12. 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 2

  13. ASCII ASCII (American Standard Code for Information Interchange) Standardized code for mapping characters to integers Many text files on your computer are in ASCII. But, computers need numbers represented in binary! Character a b c e z ASCII value 32 97 98 99 101 122 3

  14. ASCII ASCII (American Standard Code for Information Interchange) Standardized code for mapping characters to integers Many text files on your computer are in ASCII. But, computers need numbers represented in binary! Every character is represented by a byte (8 bits). Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 3

  15. ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 What is the binary representation of the following String? cab z 4

  16. ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 What is the binary representation of the following String? cab z Answer 01100011 4

  17. ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 What is the binary representation of the following String? cab z Answer 01100011 01100001 4

  18. ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 What is the binary representation of the following String? cab z Answer 01100011 01100001 01100010 4

  19. ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 What is the binary representation of the following String? cab z Answer 01100011 01100001 01100010 00100000 4

  20. ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 What is the binary representation of the following String? cab z Answer 01100011 01100001 01100010 00100000 01111010 4

  21. ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 What is the binary representation of the following String? cab z Answer 0110001101100001011000100010000001111010 4

  22. Another ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 How do we read the following binary as ASCII? 011000010110001101100101 5

  23. Another ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 How do we read the following binary as ASCII? 01100001 01100011 01100101 Answer 5

  24. Another ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 How do we read the following binary as ASCII? 01100001 01100011 01100101 Answer a 5

  25. Another ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 How do we read the following binary as ASCII? 01100001 01100011 01100101 Answer ac 5

  26. Another ASCII Example Character a b c e z ASCII value 32 97 98 99 101 122 Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 How do we read the following binary as ASCII? 01100001 01100011 01100101 Answer ace 5

  27. Huffman Idea Huffman s Insight Use variable length encodings for different characters to take advantage 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. 6

  28. Huffman Encoding Create a HuffmanTree that gives a good binary representation for each character. The path from the root to the character leaf is the encoding for that character; left means 0, right means 1. ASCII Table Binary Representation 00100000 01100001 01100010 01100011 01100101 01111010 Huffman Tree Character a b c e z 0 1 b 0 1 a 0 1 c 7

  29. Final Project: Huffman Coding The final project asks you to write a class that manages creating and using this Huffman code. (A) Create a Huffman Code from a file and compress it. (B) Decompress the file to get original contents. 8

  30. Part A: Making a HuffmanCode Overview Input File Contents bad cab 9

  31. Part A: Making a HuffmanCode Overview Input File Contents bad cab Step 1: Count the occurrences of each character in file {' '=1, 'a'=2, 'b'=2, 'c'=1, 'd'=1} 9

  32. Part A: Making a HuffmanCode Overview Input File Contents bad 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. Place in a PriorityQueue c d a b pq freq: 1 freq: 1 freq: 1 freq: 2 freq: 2 9

  33. Part A: Making a HuffmanCode Overview Input File Contents bad 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. Place in a PriorityQueue c d a b pq freq: 1 freq: 1 freq: 1 freq: 2 freq: 2 Step 3: Use Huffman Tree building algorithm (described soon) 9

  34. Part A: Making a HuffmanCode Overview Input File Contents bad 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. Place in a PriorityQueue c d a b pq freq: 1 freq: 1 freq: 1 freq: 2 freq: 2 Step 3: Use Huffman Tree building algorithm (described soon) Step 4: Save encoding to .code file to encode/decode later. {'d'=00, 'a'=01, 'b'=10, ' '=110, 'c'=111} 9

  35. Part A: Making a HuffmanCode Overview Input File Contents bad 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. Place in a PriorityQueue c d a b pq freq: 1 freq: 1 freq: 1 freq: 2 freq: 2 Step 3: Use Huffman Tree building algorithm (described soon) 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 encodings Compressed Output: 1001001101110110 9

  36. Step 1: Count Character Occurrences We do this step for you Input File bad cab Generate Counts Array: index 0 1 32 97 98 99 100 101 ... ... value 0 0 2 2 1 1 0 10

  37. 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 c a d b pq freq: 1 freq: 2 freq: 1 freq: 1 freq: 2 11

  38. Step 3: Remove and Merge c a d b pq freq: 1 freq: 2 freq: 1 freq: 1 freq: 2 12

  39. Step 3: Remove and Merge freq: 2 c freq: 1 freq: 1 a d b pq freq: 2 freq: 1 freq: 2 12

  40. Step 3: Remove and Merge freq: 2 d b a pq freq: 1 freq: 2 freq: 2 c freq: 1 freq: 1 12

  41. Step 3: Remove and Merge freq: 3 a d freq: 1 freq: 2 freq: 2 b pq freq: 2 c freq: 1 freq: 1 12

  42. Step 3: Remove and Merge freq: 2 freq: 3 b pq freq: 2 c d a freq: 1 freq: 1 freq: 1 freq: 2 12

  43. Step 3: Remove and Merge freq: 4 b freq: 2 freq: 2 c freq: 1 freq: 1 freq: 3 pq a d freq: 2 freq: 1 12

  44. Step 3: Remove and Merge freq: 4 freq: 3 b pq freq: 2 freq: 2 a d freq: 2 freq: 1 c freq: 1 freq: 1 12

  45. Step 3: Remove and Merge freq: 7 freq: 3 freq: 4 a d b freq: 2 freq: 2 freq: 1 freq: 2 c freq: 1 freq: 1 pq 12

  46. Step 3: Remove and Merge freq: 7 freq: 3 freq: 4 pq a d b freq: 2 freq: 2 freq: 1 freq: 2 c freq: 1 freq: 1 12

  47. Step 3: Remove and Merge freq: 7 freq: 3 freq: 4 pq a d b freq: 2 freq: 1 freq: 2 freq: 2 c freq: 1 freq: 1 What is the relationship between frequency in file and binary representation length? 12

  48. 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. 13

  49. Step 4: Print Encodings Save the tree to a file to save the encodings for the characters we made. 0 1 0 0 1 1 a d b 0 1 c 14

  50. Step 4: Print Encodings Save the tree to a file to save the encodings for the characters we made. Output of save 0 1 0 0 1 1 a d b 0 1 c 14

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#