46
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?

Embed Size (px)

Citation preview

Page 1: 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?

Page 2: 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

Page 3: 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?

LECTURE 33:TREE TRAVERSALS

CSC 212 – Data Structures

Page 4: 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?

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

Page 5: 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?

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

Page 6: 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?

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?

Page 7: 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?

Tree Traversals

Several different, predictable orderings Preorder honors thy elders and treats them

well

Page 8: 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?

Tree Traversals

Several different, predictable orderings Preorder honors thy elders and treats them

well This traversal will start with parent THEN do

kids

Page 9: 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?

Tree Traversals

Several different, predictable orderings Preorder honors thy elders and treats them

well This traversal will start with parent

THEN do kids

Page 10: 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?

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

Page 11: 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?

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

Page 12: 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?

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);

Page 13: 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?

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);

Page 14: 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?

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);

Page 15: 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?

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);

Page 16: 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?

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);

Page 17: 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?

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

Page 18: 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?

Tree Traversals

Several different, predictable orderings Preorder honors thy elders and treats them

well Morals also important: postorder places

children first

Page 19: 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?

Tree Traversals

Several different, predictable orderings Morals also important: postorder places

children first Do children THEN do parent using this

traversal

Page 20: 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?

Tree Traversals

Several different, predictable orderings Morals also important: postorder places

children first Do children THEN do parent using this

traversal

Page 21: 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?

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 */

Page 22: 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?

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 */

Page 23: 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?

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 */

Page 24: 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?

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 */

Page 25: 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?

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 */

Page 26: 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?

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 */

Page 27: 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?

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 */

Page 28: 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?

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 */

Page 29: 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?

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 */

Page 30: 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?

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 */

Page 31: 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?

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 */

Page 32: 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?

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 */

Page 33: 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?

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

Page 34: 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?

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

Page 35: 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?

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));

Page 36: 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?

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));

Page 37: 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?

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));

Page 38: 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?

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));

Page 39: 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?

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));

Page 40: 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?

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));

Page 41: 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?

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));

Page 42: 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?

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));

Page 43: 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?

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

Page 44: 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?

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

Page 45: 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?

Your Turn

Get into your groups and complete activity

Page 46: 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?

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