Data Structures - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Data Structures

Description:

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 ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 23
Provided by: csHon
Category:
Tags: data | heap | structures

less

Transcript and Presenter's Notes

Title: Data Structures


1
Data Structures
  • Week 7 Heap/Huffman Tree
  • http//www.cs.hongik.ac.kr/rhanha/rhanha_teaching
    .html

2
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
C
B
D
E
G
F
H
I
4
3
2
9
8
7
5
6
1
D
G
E
H
A
C
F
B
I
9
7
8
6
4
3
5
2
1
3
Description
  • By 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

4
Constructing Heap
  • Step1
  • Insert all elements to be sorted into a heap
    structure arbitrarily
  • 4, 2, 3, 9, 8, 5, 7, 6, 1

4
3
2
7
9
8
5
arbitrary
6
1
5
Constructing Heap
construct heap
construct heap
4
3
2
7
9
8
5
6
1
6
Constructing Heap
  • Step 2
  • Recursively turn subtrees of root into heaps.

9
7
6
8
3
5
2
1
construct heap
construct heap
7
Constructing Heap
  • Step 3
  • Use Fix Heap to insert label of root

4
not max heap
fix heap
7
9
3
6
8
5
2
1
8
Fix Heap
9
7
4
3
6
8
5
2
1
9
Fix Heap
9
7
8
3
6
4
5
2
1
10
Resulting Heap
9
7
8
3
6
4
5
2
1
11
Building The Huffman Tree
  • Build a minimum heap which contains the nodes of
    all symbols with the frequency values as the keys
    in the message
  • Repeat until the heap is empty
  • a) 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

12
Example of The Huffman Tree
index
1
3
2
4
5
7
6
8
9
0
symbol
A
C
B
D
E
G
F
H
I
4
3
2
9
8
7
5
6
1
frequency
index
1
3
2
4
5
7
6
8
9
0
symbol
I
C
B
A
E
G
F
H
D
1
3
2
4
8
7
5
6
9
frequency
13
index
1
3
2
4
5
7
6
8
9
0
symbol
I
C
B
A
E
G
F
H
D
1
3
2
4
8
7
5
6
9
frequency
IB3
I1
B2
14
index
3
2
4
5
7
6
8
9
1
0
symbol
C
IB
F
A
D
E
G
H
3
3
5
4
9
8
7
6
frequency
CIB6
C3
IB3
I1
B2
15
index
3
2
4
5
7
6
8
9
1
0
symbol
A
H
F
G
D
E
CIB
4
6
5
7
9
8
6
frequency
CIB6
C3
AF9
IB3
A4
F5
I1
B2
16
index
3
2
4
5
7
6
8
9
1
0
symbol
H
G
CIB
D
AF
E
6
7
6
9
9
8
frequency

HCIB12
H6
CIB6
C3
AF9
IB3
A4
F5
I1
B2
17
index
3
2
4
5
7
6
8
9
1
0
symbol
G
E
AF
D
HCIB
7
8
9
9
12
frequency

HCIB12
H6
CIB6
GE15
C3
AF9
IB3
G7
E8
A4
F5
I1
B2
18
index
3
2
4
5
7
6
8
9
1
0
symbol
D
HCIB
AF
GE
9
12
9
15
frequency

HCIB12
H6
CIB6
DAF18
C3
D9
IB3
AF9
GE15
I1
B2
A4
G7
F5
E8
19
index
3
2
4
5
7
6
8
9
1
0
symbol
HCIB
GE
DAF
12
15
18
frequency

GEHCIB27

HCIB12
GE15
H6
G7
CIB6
E8
DAF18
C3
D9
IB3
AF9
I1
B2
A4
F5
20
index
3
4
5
7
6
8
9
1
2
0
symbol
DAF
GEHCIB
27
18
frequency

DAFGEHCIB45

GEHCIB27
DAF18

D9
HCIB12
AF9
GE15
A4
G7
H6
F5
CIB6
E8
C3
IB3
I1
B2
21

DAFGEHCIB45
0
1

GEHCIB27
DAF18
0
1
0
1

HCIB12
AF9
D9
GE15
0
1
0
0
1
1
A4
G7
H6
F5
CIB6
E8
0
1
C3
IB3
1
0
I1
B2
22
Huffman Code Table
Write a Comment
User Comments (0)
About PowerShow.com