Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine
CSCI 1900 Lecture Lecture Introduction Reading –Rosen Section 11.3 Tree traversal –Preorder –Inorder –Postorder Encoding –Huffman encoding
CSCI 1900 Lecture 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
CSCI 1900 Lecture For Loop Positional Tree i = 1 i = 2 i = 3 j=1 j=2 j=3 j=4 j=5
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture Preorder Traversal The name for this systematic way of traversing a tree is preorder traversal
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture Example: Preorder Traversal A B C DFJLG IK E H Resulting string: A B C D E F G H I J K L
CSCI 1900 Lecture Example: Inorder Traversal A B C DFJLG IK E H Resulting string: D C B F E G A I J H K L
CSCI 1900 Lecture Example: Postorder Traversal A B C DFJLG IK E H Resulting string: D C F G E B J I L K H A
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture Huffman Tree Example ET A I NS
CSCI 1900 Lecture 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 – – –
CSCI 1900 Lecture 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
CSCI 1900 Lecture Key Concepts Summary Tree traversal –Preorder –Inorder –Postorder Encoding –Huffman encoding Reading for next time –Kolman Section 7.5