Download presentation
Presentation is loading. Please wait.
Published byPauline Cole Modified over 8 years ago
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 2 nd glass & pour it into the 5 th glass!
3
CSC 212 – Data Structures
4
Trees Represent hierarchical relationships Parent-child basis of this abstract data type Real-world example: organization chart IdiotKiss-upUseless Unknown Lead ?Blob Carbon Rod Lackey Boot-LickThief DroneMoron Worker Hard Worker Knowledgeable Person You
5
Traversing Linear Collections Trees are another Collection of data Position s required in the Tree ADT methods 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 IndexList ’s indices from lowest to highest Iterator for NodeList follows Position s
6
Traversing Linear Collections Trees are another Collection of data Position s required in the Tree ADT methods 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 IndexList ’s indices from lowest to highest Iterator for NodeList follows Position s Tree s? But how do we do this for Tree s?
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 start with parent THEN do kids This traversal will start with parent THEN do kids
10
Tree Traversals Several different, predictable orderings Preorder honors thy elders and treats them well start with parent THEN do kids This traversal will start with parent THEN do kids
11
Preorder Traversal Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w); Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3
12
Preorder Traversal Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w); Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3
13
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
14
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
15
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
16
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
17
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
18
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
19
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
20
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
21
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
22
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
23
Preorder Traversal Table of Contents Chapter 1Appendix A Chapter 2 Section 2.1Section 2.2 Section 1.1 Section 1.2 Section 1.2.1Section 1.2.2Section 1.2.3 Algorithm preOrder(tree, v) /* Process v */ foreach (w : v.children()) preOrder(tree, w);
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.2 Appendix A
25
Tree Traversals Several different, predictable orderings Preorder honors thy elders and treats them well Morals also important: postorder places children first
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 do children THEN do parent For postorder traversal do children THEN do parent
28
Postorder Traversal Several different, predictable orderings Postorder places children first: morals also important do children THEN do parent For postorder traversal do children THEN do parent
29
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
30
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
31
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
32
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
33
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
34
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
35
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
36
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
37
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
38
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
39
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
40
Postorder Traversal Csc212/ Activities/Midterm1.tex Projects/ Project01.texProject02.tex Activity01.tex Activity02/ Problems.texSolution.texSolution02.png Algorithm postOrder(tree, v) foreach (w : v.children()) postOrder(tree, w); /* Process v */
41
Postorder Traversal Node visits children before it is processed Needed to compute directory’s disk space First look at size of contents then 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 reminisce over HP calculators Kind of stupid, but is actually used in programs
43
Inorder Traversal 1 63 42 Algorithm inOrder(tree, v) if (tree.hasLeft(v)) inOrder(tree, tree.left(v)) /* Process v */ if (tree.hasRight(v)) inOrder(tree, tree.right(v));
44
Inorder Traversal 1 63 42 Algorithm inOrder(tree, v) if (tree.hasLeft(v)) inOrder(tree, tree.left(v)) /* Process v */ if (tree.hasRight(v)) inOrder(tree, tree.right(v));
45
Inorder Traversal 1 63 42 Algorithm inOrder(tree, v) if (tree.hasLeft(v)) inOrder(tree, tree.left(v)) /* Process v */ if (tree.hasRight(v)) inOrder(tree, tree.right(v));
46
Inorder Traversal 1 63 42 Algorithm inOrder(tree, v) if (tree.hasLeft(v)) inOrder(tree, tree.left(v)) /* Process v */ if (tree.hasRight(v)) inOrder(tree, tree.right(v));
47
Inorder Traversal 1 63 42 Algorithm inOrder(tree, v) if (tree.hasLeft(v)) inOrder(tree, tree.left(v)) /* Process v */ if (tree.hasRight(v)) inOrder(tree, tree.right(v));
48
Inorder Traversal 1 63 42 Algorithm inOrder(tree, v) if (tree.hasLeft(v)) inOrder(tree, tree.left(v)) /* Process v */ if (tree.hasRight(v)) inOrder(tree, tree.right(v));
49
Inorder Traversal 1 63 42 Algorithm inOrder(tree, v) if (tree.hasLeft(v)) inOrder(tree, tree.left(v)) /* Process v */ if (tree.hasRight(v)) inOrder(tree, tree.right(v));
50
Inorder Traversal Traversal visits equation in proper order: (1 + (6 * 3)) - (4 / 2) Binary search trees use this again & again 1 63 42 Algorithm inOrder(tree, v) if (tree.hasLeft(v)) inOrder(tree, tree.left(v)) /* Process v */ if (tree.hasRight(v)) inOrder(tree, tree.right(v));
51
Postorder Traversal Algorithm postOrder(tree, v) if (tree.hasLeft(v)) postOrder(tree, tree.left(v)) if (tree.hasRight(v)) postOrder(tree, tree.right(v)); /* Process v */ 1 63 42
52
Postorder Traversal Traversal follows order equation is computed: 1 6 3 * + 4 2 / - Java compiles equations using this format 1 63 42 Algorithm postOrder(tree, v) if (tree.hasLeft(v)) postOrder(tree, tree.left(v)) if (tree.hasRight(v)) postOrder(tree, tree.right(v)); /* Process v */
53
Your Turn Get into your groups and complete activity
54
For Next Lecture Week #12 assignment due tomorrow Programming Assignment #3 Programming Assignment #3 available Have a great thanksgiving!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.