Upload
homer-rice
View
212
Download
0
Tags:
Embed Size (px)
Citation preview
Problem of the Day
The place where they signed the Declaration of Independence has the same name now as it did then. What is it?
Problem of the Day
The place where they signed the Declaration of Independence has the same name now as it did then. What is it?
The bottom of the page
LECTURE 33:TREE TRAVERSALS
CSC 212 – Data Structures
Trees
Represent hierarchical relationships Parent-child basis of this abstract data type
Real-world example: organization chart
Idiot
Kiss-up
Useless
Unknown Lead
?
Blob
Carbon RodLacke
y
Boot-Lick
ThiefDrone
MoronWorker
Hard Worker
Knowledgeable Person
You
Traversing Linear Collections Trees are another Collection of data
Positions 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 Positions
Traversing Linear Collections Trees are another Collection of data
Positions 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 Positions
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
Preorder Traversal
Algorithm preOrder(tree, v)/* Process v */foreach (w : v.children())
preOrder(tree, w);Table of Contents
Chapter 1
Appendix A
Chapter 2
Section 2.1
Section 2.2
Section 1.1
Section 1.2
Section 1.2.1
Section 1.2.2
Section 1.2.3
Preorder Traversal
Algorithm preOrder(tree, v)/* Process v */foreach (w : v.children())
preOrder(tree, w);Table of Contents
Chapter 1
Appendix A
Chapter 2
Section 2.1
Section 2.2
Section 1.1
Section 1.2
Section 1.2.1
Section 1.2.2
Section 1.2.3
Preorder Traversal
Table of Contents
Chapter 1
Appendix A
Chapter 2
Section 2.1
Section 2.2
Section 1.1
Section 1.2
Section 1.2.1
Section 1.2.2
Section 1.2.3
Algorithm preOrder(tree, v)/* Process v */foreach (w : v.children())
preOrder(tree, w);
Preorder Traversal
Table of Contents
Chapter 1
Appendix A
Chapter 2
Section 2.1
Section 2.2
Section 1.1
Section 1.2
Section 1.2.1
Section 1.2.2
Section 1.2.3
Algorithm preOrder(tree, v)/* Process v */foreach (w : v.children())
preOrder(tree, w);
Preorder Traversal
Table of Contents
Chapter 1
Appendix A
Chapter 2
Section 2.1
Section 2.2
Section 1.1
Section 1.2
Section 1.2.1
Section 1.2.2
Section 1.2.3
Algorithm preOrder(tree, v)/* Process v */foreach (w : v.children())
preOrder(tree, w);
Preorder Traversal
Table of Contents
Chapter 1
Appendix A
Chapter 2
Section 2.1
Section 2.2
Section 1.1
Section 1.2
Section 1.2.1
Section 1.2.2
Section 1.2.3
Algorithm preOrder(tree, v)/* Process v */foreach (w : v.children())
preOrder(tree, w);
Preorder Traversal
Table of Contents
Chapter 1
Appendix A
Chapter 2
Section 2.1
Section 2.2
Section 1.1
Section 1.2
Section 1.2.1
Section 1.2.2
Section 1.2.3
Algorithm preOrder(tree, v)/* Process v */foreach (w : v.children())
preOrder(tree, w);
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 Preorder honors thy elders and treats them
well Morals also important: postorder places
children first
Tree Traversals
Several different, predictable orderings Morals also important: postorder places
children first Do children THEN do parent using this
traversal
Tree Traversals
Several different, predictable orderings Morals also important: postorder places
children first Do children THEN do parent using this
traversal
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
Postorder Traversal
Csc212/
Activities/
Midterm1.tex
Projects/
Project01.tex
Project02.tex
Activity01.tex
Activity02/
Problems.tex
Solution.tex
Solution02.png
Algorithm postOrder(tree, v)foreach (w : v.children())
postOrder(tree, w);/* Process v */
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
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
Inorder Traversal
-
/+
*1
6 3
4 2
Algorithm inOrder(tree, v)if (tree.hasLeft(v))
inOrder(tree, tree.left(v))visit(tree, v)if (tree.hasRight(v)) inOrder(tree, tree.right(v));
Inorder Traversal
-
/+
*1
6 3
4 2
Algorithm inOrder(tree, v)if (tree.hasLeft(v))
inOrder(tree, tree.left(v))visit(tree, v)if (tree.hasRight(v)) inOrder(tree, tree.right(v));
Inorder Traversal
-
/+
*1
6 3
4 2
Algorithm inOrder(tree, v)if (tree.hasLeft(v))
inOrder(tree, tree.left(v))visit(tree, v)if (tree.hasRight(v)) inOrder(tree, tree.right(v));
Inorder Traversal
-
/+
*1
6 3
4 2
Algorithm inOrder(tree, v)if (tree.hasLeft(v))
inOrder(tree, tree.left(v))visit(tree, v)if (tree.hasRight(v)) inOrder(tree, tree.right(v));
Inorder Traversal
-
/+
*1
6 3
4 2
Algorithm inOrder(tree, v)if (tree.hasLeft(v))
inOrder(tree, tree.left(v))visit(tree, v)if (tree.hasRight(v)) inOrder(tree, tree.right(v));
Inorder Traversal
-
/+
*1
6 3
4 2
Algorithm inOrder(tree, v)if (tree.hasLeft(v))
inOrder(tree, tree.left(v))visit(tree, v)if (tree.hasRight(v)) inOrder(tree, tree.right(v));
Inorder Traversal
-
/+
*1
6 3
4 2
Algorithm inOrder(tree, v)if (tree.hasLeft(v))
inOrder(tree, tree.left(v))visit(tree, v)if (tree.hasRight(v)) inOrder(tree, tree.right(v));
Inorder Traversal
Traversal visits equation in proper order:
(1 + (6 * 3)) - (4 / 2) Binary search trees use this again &
again
-
/+
*1
6 3
4 2
Algorithm inOrder(tree, v)if (tree.hasLeft(v))
inOrder(tree, tree.left(v))visit(tree, v)if (tree.hasRight(v)) inOrder(tree, tree.right(v));
Postorder Traversal
Algorithm postOrder(tree, v)if (tree.hasLeft(v))
postOrder(tree, tree.left(v))if (tree.hasRight(v)) postOrder(tree, tree.right(v));visit(tree, v)
-
/+
*1
6 3
4 2
Postorder Traversal
Traversal follows order equation is computed:
1 6 3 * + 4 2 / - Java compiles equations using this
format
Algorithm postOrder(tree, v)if (tree.hasLeft(v))
postOrder(tree, tree.left(v))if (tree.hasRight(v)) postOrder(tree, tree.right(v));visit(tree, v)
-
/+
*1
6 3
4 2
Your Turn
Get into your groups and complete activity
For Next Lecture
Read parts of GT 7.2 & 7.3.6 for Friday Trees are Iterable, but how to implement
it? What is a template pattern & how do we
use it? I’m lazy; can’t we make this code easy to
write?
Week #12 assignment posted & due Tuesday
Programming Assignment #2 due Saturday