540 likes | 650 Views
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?
E N D
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? • Take the 2nd glass & pour it into the 5th glass!
CSC 212 – Data Structures Lecture 35:Tree Traversals
Trees • Represent hierarchical relationships • Parent-child basis of this abstract data type • Real-world example: organization chart
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
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?
Tree Traversals • Several different, predictable orderings • Preorder honors thy elders and treats them well
Tree Traversals • Several different, predictable orderings • Preorder honors thy elders and treats them well • This traversal will start with parent THEN do kids
Tree Traversals • Several different, predictable orderings • Preorder honors thy elders and treats them well • This traversal will start with parent THEN do kids
Tree Traversals • Several different, predictable orderings • Preorder honors thy elders and treats them well • This traversal will start with parent THEN do kids
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Tree Traversals • Several different, predictable orderings • Postorder places children first: morals also important
Postorder Traversal • Several different, predictable orderings • Postorder places children first: morals also important • For postorder traversal do children THEN do parent
Postorder Traversal • Several different, predictable orderings • Postorder places children first:morals also important • For postorder traversal do children THEN do parent
Postorder Traversal • Several different, predictable orderings • Postorder places children first:morals also important • For postorder traversal do children THEN do parent
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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