Top Banner
Data Structures Week 7: Heap/Huffman Tree http://www.cs.hongik.ac.kr/~rhanha/rhanha_teaching.html
22

Data Structures

Jan 27, 2016

Download

Documents

fayre

Data Structures. Week 7 : Heap/Huffman Tree http://www.cs.hongik.ac.kr/~rhanha/rhanha_teaching.html. Array to Store the Heap. The same array is used both to compute the frequency for each char and to store the heap same array. A. B. C. D. E. F. G. H. I. 4. 2. 3. 9. 8. 5. 7. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Structures

Data Structures

Week 7: Heap/Huffman Tree

http://www.cs.hongik.ac.kr/~rhanha/rhanha_teaching.html

Page 2: Data Structures

Array to Store the Heap

The same array is used both to compute the frequency

for each char and to store the heap same array

4 32 9

A CB D

8 75 6

E GF H

1

I

9 78 6

D GE H

4 35 2

A CF B

1

I

Page 3: Data Structures

DescriptionBy replacing : the root with a leaf node and fixing the tree by swaping nodes starting from the root downwards (fix heap) a new heap obtained

If the two subtrees of the root are heaps we can obtain a new heap with two heaps and

a node of an arbitrary key value a heap from array by fixing the tree as in delete

Thus, we can create recursively making subtrees heaps making the the first element the root of two

subtrees(heaps) finally fixing the heap

Page 4: Data Structures

Constructing HeapStep1Insert all elements to be sorted into a

heap structure arbitrarily[4, 2, 3, 9, 8, 5, 7, 6, 1]

4

2 3

9 8

6

5

1

7

arbitrary

Page 5: Data Structures

Constructing Heap

4

2 3

9 8

6

5

1

7

construct heap construct heap

Page 6: Data Structures

Constructing Heap

9

7 6 8

2 5

1 3

construct heap construct heap

Step 2Recursively turn subtrees of root into

heaps.

Page 7: Data Structures

Constructing Heap

Step 3Use Fix Heap to insert label of root

4

9 7

6 8

2

5

1

3

not max heap fix heap

Page 8: Data Structures

Fix Heap

9

4 7

6 8

2

5

1

3

Page 9: Data Structures

Fix Heap

9

8 7

6 4

2

5

1

3

Page 10: Data Structures

Resulting Heap

9

8 7

6 4

2

5

1

3

Page 11: Data Structures

Building The Huffman Tree1. Build a minimum heap which contains the

nodes of all symbols with the frequency values as the keys in the message

2. Repeat until the heap is emptya) Delete two nodes from the heap

concatenate the two symbols add their frequencies insert the new node into the heap

b) Insert the new node into the Huffman tree the two nodes become the two children of the node

for the concatenate symbol

Page 12: Data Structures

Example of The Huffman Tree

4 32 9

A CB D

8 75 6

E GF H

1

I

1 32 4 5 76 8 9index

symbol

frequency

1 32 4

I CB A

8 75 6

E GF H

9

D

1 32 4 5 76 8 9index

symbol

frequency

0

0

Page 13: Data Structures

1 32 4

I CB A

8 75 6

E GF H

9

D

1 32 4 5 76 8 9index

symbol

frequency

0

I1 B2

IB3

Page 14: Data Structures

3 3 5

C IB F

4 98 7

A DE G

6

H

1 32 4 5 76 8 9index

symbol

frequency

0

I1 B2

IB3C3

CIB6

Page 15: Data Structures

4 6 5

A H F

7 98 6

G DE CIB

1 32 4 5 76 8 9index

symbol

frequency

0

I1 B2

IB3C3

CIB6

A4 F5

AF9

Page 16: Data Structures

6 7 6

H G CIB

9 98

D AFE

1 32 4 5 76 8 9index

symbol

frequency

0

I1 B2

IB3C3

CIB6

A4 F5

AF9

H6

`HCIB12

Page 17: Data Structures

7 8 9

G E AF

9 12

D HCIB

1 32 4 5 76 8 9index

symbol

frequency

0

I1 B2

IB3C3

CIB6

A4 F5

AF9

H6

`HCIB12

G7 E8

GE15

Page 18: Data Structures

9 12 9

D HCIB AF

15

GE

1 32 4 5 76 8 9index

symbol

frequency

0

I1 B2

IB3C3

CIB6

A4 F5

AF9

H6

`HCIB12

G7 E8

GE15 D9

DAF18

Page 19: Data Structures

12 15 18

HCIB GE DAF

1 32 4 5 76 8 9index

symbol

frequency

0

I1 B2

IB3C3

CIB6H6

`HCIB12

G7 E8

GE15

A4 F5

AF9D9

DAF18

`GEHCIB27

Page 20: Data Structures

2718

GEHCIBDAF

1 32 4 5 76 8 9index

symbol

frequency

0

I1 B2

IB3C3

CIB6H6

`HCIB12

G7 E8

GE15

A4 F5

AF9D9

DAF18 `GEHCIB27

`DAFGEHCIB45

Page 21: Data Structures

I1 B2

IB3C3

CIB6H6

`HCIB12

G7 E8

GE15

A4 F5

AF9D9

DAF18 `GEHCIB27

`DAFGEHCIB45

0

0

0 0

0

0

0

0

1

1 1 1

1

1

1

1

Page 22: Data Structures

Huffman Code Table

Symbol Frequency Huffman Code

D 9 00

E 8 101

G 7 100

H 6 110

F 5 011

A 4 010

C 3 1110

B 2 11111

I 1 11110