Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine."— Presentation transcript:

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

2 CSCI 1900 Lecture 18 - 2 Lecture Introduction Reading –Rosen Section 11.3 Tree traversal –Preorder –Inorder –Postorder Encoding –Huffman encoding

3 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 j next i

4 CSCI 1900 Lecture 18 - 4 For Loop Positional Tree 111213141521222324253132333435 i = 1 i = 2 i = 3 j=1 j=2 j=3 j=4 j=5

5 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

6 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

7 CSCI 1900 Lecture 18 - 7 Traversal Example Neighborhood families A. Jones HallsSmithTaylorB. Jones Katy age 3 Tommy age 5 Phil age 8 Taylor age 1 Lori age 4 Lexi age 2 Karen age 14 Bart age 12 Mike age 6 Ben age 2

8 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

9 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” edge 5.If you get back to the root and there are no untraveled edges, you are done

10 CSCI 1900 Lecture 18 - 10 Vertices Numbered in Order of Visits Neighborhood families A. JonesHallsSmithTaylorB. Jones Katy age 3 Tommy age 5 Phil age 8 Taylor age 1 Lori age 4 Lexi age 2 Karen age 14 Bart age 12 Mike age 6 Ben age 2 1 2 3 5 6 7 8 9 10 11 12 13 14 15 164

11 CSCI 1900 Lecture 18 - 11 Preorder Traversal The name for this systematic way of traversing a tree is preorder traversal

12 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

13 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

14 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

15 CSCI 1900 Lecture 18 - 15 Example: Preorder Traversal A B C DFJLG IK E H Resulting string: A B C D E F G H I J K L

16 CSCI 1900 Lecture 18 - 16 Example: Inorder Traversal A B C DFJLG IK E H Resulting string: D C B F E G A I J H K L

17 CSCI 1900 Lecture 18 - 17 Example: Postorder Traversal A B C DFJLG IK E H Resulting string: D C F G E B J I L K H A

18 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

19 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

20 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

21 CSCI 1900 Lecture 18 - 21 Huffman Tree Example ET A I NS 0 0 0 0 0 1 1 1 1 1

22 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

23 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 pairs –Use the set of pairs to look up the code for each character What is the encoding for –NEST –STAT

24 CSCI 1900 Lecture 18 - 24 Key Concepts Summary Tree traversal –Preorder –Inorder –Postorder Encoding –Huffman encoding Reading for next time –Kolman Section 7.5


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

Similar presentations


Ads by Google