Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

24
Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

Transcript of Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

Page 1: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

Lecture 18Tree Traversal

CSCI – 1900 Mathematics for Computer Science

Fall 2014

Bill Pine

Page 2: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 2

Lecture Introduction

• Reading– Rosen Section 11.3

• Tree traversal– Preorder– Inorder– Postorder

• Encoding– Huffman encoding

Page 3: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 3

Tree Traversal

• Trees can represent the organization of items• Trees can represent a decision hierarchy• Trees can also represent a process

– With each vertex specifying a task– for-loop example

for i = 1 thru 3 by 1 for j = 1 thru 5 by 1 array[i,j] = 10*i + j next jnext i

Page 4: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 4

For Loop Positional Tree

11 12 13 14 15 21 22 23 24 25 31 32 33 34 35

i = 1i = 2

i = 3

j=1j=2j=3j=4j=5

Page 5: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 5

Terminology

• Traverse a tree – Visit each vertex in a specific order

• Visit a vertex – Performing a task at a vertex

• Do a computation• Take a decision

• Kolman uses the term “search” to mean traverse– Search implies looking for a specific vertex– This is not necessarily the case

Page 6: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 6

Tree Traversal

• Applications using trees, traverse the tree in a methodic way– To ensure visiting every vertex, exactly once

• We will explore three methodic ways• Example: Assume we want to compute

– The average age, maximum age, and minimum age of all of the children from five families• Tree on next slide

Page 7: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 7

Traversal Example

Neighborhoodfamilies

A. Jones Halls Smith Taylor B. Jones

Katyage 3

Tommyage 5

Philage 8

Taylorage 1

Loriage 4

Lexiage 2

Karenage 14

Bartage 12

Mikeage 6

Benage 2

Page 8: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 8

Traversal Example (cont)

• To ensure that we don’t miss a child– Need a systematic way to visit each vertex – To calculate the average, max, and min ages for

all of the children

• By defining a systematic process– Not only can a human be sure not to miss a vertex– But also can serve as the basis for developing a

computer algorithm

Page 9: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 9

One Such Systematic Process

1. Starting at the root, repeatedly take the leftmost “untraveled” edge until you arrive at a leaf

2. Include each vertex in the average, max, and min calculations

3. One at a time, go back up the edges until you reach a vertex that hasn’t had all of its outgoing edges traveled

4. Traverse the leftmost “untraveled” edge5. If you get back to the root and there are no

untraveled edges, you are done

Page 10: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 10

Vertices Numbered in Order of Visits

Neighborhoodfamilies

A. Jones Halls Smith Taylor B. Jones

Katyage 3

Tommyage 5

Philage 8

Taylorage 1

Loriage 4

Lexiage 2

Karenage 14

Bartage 12

Mikeage 6

Benage 2

1

2

3

5

6

7

8

9

10

11

12

13

14

15

164

Page 11: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 11

Preorder Traversal

• The name for this systematic way of traversing a tree is preorder traversal

Page 12: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 12

Preorder Traversal Algorithm

• A preorder traversal of a binary tree consists of the following three steps:

1. Visit the root

2. Traverse the left subtree if it exists

3. Traverse the right subtree if it exists

• The term “traverse” in steps 2 and 3 implies that we apply all three steps to the subtree beginning with step 1

Page 13: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 13

Inorder Traversal Algorithm

• An inorder traversal of a binary tree has the following three steps:

1. Traverse the left subtree if it exists

2. Visit the root

3. Traverse the right subtree if it exists

Page 14: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 14

Postorder Traversal Algorithm

• A postorder traversal of a binary tree has the following three steps:

1. Traverse the left subtree if it exists

2. Traverse the right subtree if it exists

3. Visit the root

Page 15: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 15

Example: Preorder Traversal

A

B

C

D F J LG

I KE

H

Resulting string: A B C D E F G H I J K L

Page 16: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 16

Example: Inorder Traversal

A

B

C

D F J LG

I KE

H

Resulting string: D C B F E G A I J H K L

Page 17: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 17

Example: Postorder Traversal

A

B

C

D F J LG

I KE

H

Resulting string: D C F G E B J I L K H A

Page 18: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 18

Encoding

• Encode – translate a string into a series of 1’s and 0’s

• ASCII – fixed length code– Code length = 7 bits

• Each character is represented by 7 bits• Why 7 and not 8 bits?

– Encode 128 distinct characters• 95 printable characters• 33 nonprintable characters

Page 19: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 19

Encoding (cont)

• Suppose instead of a fixed length code, we use a variable length– Why? To reduce the size of the encoding

• Message compression

– Assign the short codes to the more frequently used characters in message

• The amount of compression achieved is a function of – The method used to generate the codes– Relative frequency count of the characters in the message

Page 20: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 20

Huffman Encoding

• One way of generating the codes – Huffman encoding

• Huffman encoding can be represented as a tree

• Details of generating the code are beyond the scope of this course

• We will examine the use of a generated code

Page 21: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 21

Huffman Tree Example

• •

E T • •

A IN S

0

0 0

0 0

1

1

1

1 1

Page 22: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 22

Using Huffman Trees

• To convert a code to a string– Begin at the root of the tree– Take the branch indicated by the bit– Look at next bit and take the indicated branch– Until you reach a leaf; this gives the first character– Return to the root and repeat until done

• What string is represented by – 1000100– 11101100101110– 1110010001

Page 23: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 23

Using Huffman Trees (cont)

• Generate the code for a string– Begin by traversing the tree once, creating a set

of <character, character code> pairs– Use the set of pairs to look up the code for each

character

• What is the encoding for– NEST– STAT

Page 24: Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

CSCI 1900 Lecture 18 - 24

Key Concepts Summary

• Tree traversal– Preorder– Inorder– Postorder

• Encoding– Huffman encoding

• Reading for next time– Kolman Section 7.5