1 / 24

Lecture 18 Tree Traversal

Lecture 18 Tree Traversal. CSCI – 1900 Mathematics for Computer Science Spring 2014 Bill Pine. Lecture Introduction. Reading Kolman Section 7.2, 7.3 Tree traversal Preorder Inorder Postorder Encoding Huffman encoding. Tree Traversal. Trees can represent the organization of items

craig
Télécharger la présentation

Lecture 18 Tree Traversal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Lecture Introduction • Reading • Kolman Section 7.2, 7.3 • Tree traversal • Preorder • Inorder • Postorder • Encoding • Huffman encoding CSCI 1900

  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 CSCI 1900

  4. i = 1 i = 3 i = 2 j=1j=2j=3j=4j=5 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 For Loop Positional Tree CSCI 1900

  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 CSCI 1900

  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 CSCI 1900

  7. Neighborhoodfamilies A.Jones Halls Smith Taylor B. Jones Katyage 3 Tommyage 5 Taylorage 1 Loriage 4 Karenage 14 Mikeage 6 Philage 8 Lexiage 2 Bartage 12 Benage 2 Traversal Example CSCI 1900

  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 CSCI 1900

  9. One Such Systematic Process • Starting at the root, repeatedly take the leftmost “untraveled” edge until you arrive at a leaf • Include each vertex in the average, max, and min calculations • One at a time, go back up the edges until you reach a vertex that hasn’t had all of its outgoing edges traveled • Traverse the leftmost “untraveled” edge • If you get back to the root and there are no untraveled edges, you are done CSCI 1900

  10. Neighborhoodfamilies 1 2 14 7 5 A. Jones Halls Smith Taylor B. Jones 11 3 8 4 10 13 16 Katyage 3 Tommyage 5 Taylorage 1 Loriage 4 Karenage 14 Mikeage 6 6 9 12 15 Philage 8 Lexiage 2 Bartage 12 Benage 2 Vertices Numbered in Order of Visits CSCI 1900

  11. Preorder Traversal • The name for this systematic way of traversing a tree is preorder traversal CSCI 1900

  12. Preorder Traversal Algorithm • A preorder traversal of a binary tree consists of the following three steps: • Visit the root • Traverse the left subtree if it exists • 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

  13. Inorder Traversal Algorithm • An inorder traversal of a binary tree has the following three steps: • Traverse the left subtree if it exists • Visit the root • Traverse the right subtree if it exists CSCI 1900

  14. Postorder Traversal Algorithm • A postorder traversal of a binary tree has the following three steps: • Traverse the left subtree if it exists • Traverse the right subtree if it exists • Visit the root CSCI 1900

  15. A H B C E I K D F G J L Example: Preorder Traversal Resulting string: A B C D E F G H I J K L CSCI 1900

  16. A H B C E I K D F G J L Example: Inorder Traversal Resulting string: D C B F E G A I J H K L CSCI 1900

  17. A H B C E I K D F G J L Example: Postorder Traversal Resulting string: D C F G E B J I L K H A CSCI 1900

  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 CSCI 1900

  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 CSCI 1900

  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 CSCI 1900

  21. Huffman Tree Example • • • ET • • A I NS 1 0 1 0 1 0 1 0 1 0 CSCI 1900

  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 CSCI 1900

  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 CSCI 1900

  24. Key Concepts Summary • Tree traversal • Preorder • Inorder • Postorder • Encoding • Huffman encoding • Reading for next time • Kolman Section 7.5 CSCI 1900

More Related