1 / 54

Problem of the Day

Problem of the Day. Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?. Problem of the Day. Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?

sasha
Download Presentation

Problem of the Day

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. Problem of the Day • Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?

  2. Problem of the Day • Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another? • Take the 2nd glass & pour it into the 5th glass!

  3. CSC 212 – Data Structures Lecture 35:Tree Traversals

  4. Trees • Represent hierarchical relationships • Parent-child basis of this abstract data type • Real-world example: organization chart

  5. Traversing Linear Collections • Trees are another Collection of data • Nodes usually used to hold elements in BinaryTree • ADT uses generic type to specify type of element • Want to iterate through tree’s elements • Not required, but order obvious for linear structures • Follow ArrayList’s indices from lowest to highest • Iterator for LinkedList follows nodes

  6. Traversing Linear Collections • Trees are another Collection of data • Nodes usually used to hold elements in BinaryTree • ADT uses generic type to specify type of element • Want to iterate through tree’s elements • Not required, but order obvious for linear structures • Follow ArrayList’s indices from lowest to highest • Iterator for LinkedList follows nodes But how do we do this for Trees?

  7. Tree Traversals • Several different, predictable orderings • Preorder honors thy elders and treats them well

  8. Tree Traversals • Several different, predictable orderings • Preorder honors thy elders and treats them well • This traversal will start with parent THEN do kids

  9. Tree Traversals • Several different, predictable orderings • Preorder honors thy elders and treats them well • This traversal will start with parent THEN do kids

  10. Tree Traversals • Several different, predictable orderings • Preorder honors thy elders and treats them well • This traversal will start with parent THEN do kids

  11. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  12. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  13. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  14. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  15. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  16. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  17. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  18. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  19. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  20. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  21. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  22. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  23. Preorder Traversal AlgorithmpreOrder(tree, v) /* Process v*/ foreach(w: v.children()) preOrder(tree, w); Table of Contents Chapter 1 Appendix A Chapter 2 Section 1.1 Section 1.2 Section 2.1 Section 2.2 Section 1.2.1 Section 1.2.2 Section 1.2.3

  24. Preorder Traversal • Visit node first, then visit its children • Normal ordering found in every chapter book • Start each section before going into contents: Table of Contents §1, §1.1,§1.2, §1.2.1, §1.2.2, §1.2.3§2, §2.1,§2.2Appendix A

  25. Tree Traversals • Several different, predictable orderings • Postorder places children first: morals also important

  26. Postorder Traversal • Several different, predictable orderings • Postorder places children first: morals also important • For postorder traversal do children THEN do parent

  27. Postorder Traversal • Several different, predictable orderings • Postorder places children first:morals also important • For postorder traversal do children THEN do parent

  28. Postorder Traversal • Several different, predictable orderings • Postorder places children first:morals also important • For postorder traversal do children THEN do parent

  29. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  30. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  31. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  32. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  33. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  34. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  35. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  36. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  37. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  38. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  39. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  40. Postorder Traversal AlgorithmpostOrder(tree, v) foreach(w : v.children()) postOrder(tree, w); /* Process v*/ Csc212/ Activities/ Midterm1.tex Projects/ Activity01.tex Activity02/ Project01.tex Project02.tex Problems.tex Solution.tex Solution02.png

  41. Postorder Traversal • Node visits children before it is processed • Needed to compute directory’s disk space • First look at size of contentsthen the main directory • Important whenever leaves contain actual data • Visits external nodes, then works way up the tree • In this traversal, after its descendants node processed

  42. Binary Trees • Pre- & post-order identical for binary trees • Also offer additional type of traversal • Traversals used again, so do not let this slip • Equation trees are canonical examples • Using them, the different approaches can be seen • Lets (old) Professors reminisceover HP calculators • Kind of stupid, but is actually used in programs

  43. Inorder Traversal - • AlgorithminOrder(tree,v)if(tree.hasLeft(v))inOrder(tree,tree.left(v)) • /* Process v*/ • if (tree.hasRight(v))inOrder(tree,tree.right(v)); + / 1 * 4 2 6 3

  44. Inorder Traversal - • AlgorithminOrder(tree,v)if(tree.hasLeft(v))inOrder(tree,tree.left(v)) • /* Process v*/ • if (tree.hasRight(v))inOrder(tree,tree.right(v)); + / 1 * 4 2 6 3

  45. Inorder Traversal - • AlgorithminOrder(tree,v)if(tree.hasLeft(v))inOrder(tree,tree.left(v)) • /* Process v*/ • if (tree.hasRight(v))inOrder(tree,tree.right(v)); + / 1 * 4 2 6 3

  46. Inorder Traversal - • AlgorithminOrder(tree,v)if(tree.hasLeft(v))inOrder(tree,tree.left(v)) • /* Process v*/ • if (tree.hasRight(v))inOrder(tree,tree.right(v)); + / 1 * 4 2 6 3

  47. Inorder Traversal - • AlgorithminOrder(tree,v)if(tree.hasLeft(v))inOrder(tree,tree.left(v)) • /* Process v*/ • if (tree.hasRight(v))inOrder(tree,tree.right(v)); + / 1 * 4 2 6 3

  48. Inorder Traversal - • AlgorithminOrder(tree,v)if(tree.hasLeft(v))inOrder(tree,tree.left(v)) • /* Process v*/ • if (tree.hasRight(v))inOrder(tree,tree.right(v)); + / 1 * 4 2 6 3

  49. Inorder Traversal - • AlgorithminOrder(tree,v)if(tree.hasLeft(v))inOrder(tree,tree.left(v)) • /* Process v*/ • if (tree.hasRight(v))inOrder(tree,tree.right(v)); + / 1 * 4 2 6 3

  50. Inorder Traversal - • Traversal visits equation in proper order: (1 + (6 * 3)) - (4 / 2) • Binary search trees use this again & again • AlgorithminOrder(tree,v)if(tree.hasLeft(v))inOrder(tree,tree.left(v)) • /* Process v*/ • if (tree.hasRight(v))inOrder(tree,tree.right(v)); + / 1 * 4 2 6 3

More Related