20
Trees (II) Hwansoo Han

Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Embed Size (px)

Citation preview

Page 1: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Trees (II)

Hwansoo Han

Page 2: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Abstract Data Type - Tree

2

Operations on Tree T

PARENT (n, T) – parent of n

LEFTMOST_CHILD (n, T) – leftmost child of n

RIGHT_SIBLING (n, T) – immediately to the right of n

LABEL (n, T) – label of n

CREATEi (v, T1, T2, …, Ti) – make a node r with label v and

gives it i children, which are the

roots of T1, T2, …, Ti

ROOT (T) – root of T

MAKENULL (T) – make T be the null tree

Page 3: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Example – Preorder Traversal

3

begin

Page 4: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Implementation of Trees

4

Array representation of trees

Page 5: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Array Representation of Trees - Example

5

Array elements

Specify parent node

Page 6: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Array Representation of Trees - Example

6

RIGHT_SIBLING (n, T)

Page 7: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Implementation of Trees

7

Representation of Trees by Lists of Children

For each node, form a list of its children

Page 8: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Trees by Lists of Children - Example

8

Page 9: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Trees by Lists of Children - Example

9

Page 10: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Trees by Lists of Children – Another Impl.

10

Page 11: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Binary Trees

11

Definition

Every node has either no children, a left child, a right

child, or both a left and a right child

Empty tree is also a binary tree

Page 12: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Binary Trees - Example

12

Two Binary Trees A Ordinary Tree

Page 13: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Representation of Binary Trees

13

Use an array for nodes, 1, 2, …, n

Page 14: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Representation of Binary Trees

14

Two Binary Trees

left child right child

1

2

3

4

5

left child right child

1

2

3

4

5

Page 15: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Binary Trees Example – Huffman Codes

15

A message made from five characters

a, b, c, d, e

which appear with probabilities, .12, .4, .15, .08, .25

Encode each character into a sequence of 0’s and

1’s

No code for a character is not the prefix of the code

for any other character – prefix property

Page 16: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Codes with Prefix Property - Example

16

Two possible codes both of which satisfy prefix

property

bcd – Code1: 001010011

– Code2: 1101001

Page 17: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Codes with Prefix Property - Example

17

Binary trees representing codes with prefix

property

Code 1 Code 2

Page 18: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Huffman Codes

18

A set of characters and their probabilities are given,

Find a code with the prefix property such that the

average length of a code for a character is a minimum

Average length of a character for previous codes

Code1 : 3 as all characters are coded with 3 bits

Code2 : 3 for a, d (total 20%), 2 for b, c, e (total 80%)

average is 2.2 = 3 * 20% + 2 * 80%

Page 19: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Construction of a Huffman Tree

19

(a) Initial (b) Merge a and d

(c) Merge a, d with c (d) Merge a, c, d with c (e) Final

Page 20: Data Structures & Algorithms - SKKUarcs.skku.edu/pmwiki/uploads/Courses/DataStructures… ·  · 2012-10-18Abstract Data Type - Tree 2 ... c, d, e which appear with ... Data Structures

Huffman Codes - Example

20

Find an optimal Huffman code for

Alphabet = { a, b, c, d, e, f }

Probabilities = .07, .09, .12, .22, .23, .27