13
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas Kuehne VUW COMP 103 Marcus Frean Depth-first vs breadth-first traversals

2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

Embed Size (px)

Citation preview

Page 1: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria

University of Wellington

Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas Kuehne VUW

CO

MP 1

03

Marcus Frean

Depth-first vs breadth-first traversals

Page 2: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

2

RECAP-TODAY

RECAP Building and Traversing Binary Trees

TODAY Depth-First vs Breadth-First Traversal Chapter 16.2

ANNOUNCEMENTS no new assignment next week

Page 3: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

3

recap: depth-first traversalsVariations: 1. pre-order: node, then children 2. in-order: left child, node, right

child 3. post-order: children, then node

A

B E

FDC

A B C D E FC B D A F E C D B F E A

A Maze Analogykeep your left hand on the wall,

1. pre-order: first encounter 2. in-order: second encounter 3. post-order:

third encounter

and act on

Page 4: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

4

Prefix Notation (Polish notation)

+ x 5 2 / - 7 3 9plus( times(5, 2),

divide( minus(7, 3),

9 ) ) Infix Notation(5 x 2 ) + ( ( 7 – 3) / 9)

Postfix Notation (Reverse Polish)

5 2 x 7 3 – 9 / +

Arithmetic expression trees

+

x /

-25 9

7 3

No Parentheses

No Parentheses

Page 5: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

5

Depth-First Traversal in General Trees

Visit the subtree under each child before going to the next child

Pre-order: process the node, then process the subtrees

Post-order: process the subtrees, then process the node A

B

D E

C

HG

MI K

F

LJ

Page 6: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

6

Depth-first Traversal, recursively e.g., list all employee names Use recursion

(and iteration to step through the children)

public void listAll() {System.out.println(this.name);

for (OrgTreeNode child : this.children)child.listAll();

}

Which traversal order does this implement?

Page 7: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

7

Need to remember what nodes are still being worked on, and which of their children have been processed already

In a recursive depth-first traversal (eg. "ListAll" from earlier), activation stacks take care of the bookkeeping!

A1. print own name2. process children...3. return

Managing Depth-First Traversal

A

B

D E

C

HG

MI K

F

LJ

B1. print own name2. process children...3. return

D1. print own name2. process children...3. return

Page 8: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

8

Breadth-first Traversal

What about printing the nodes by level? root first then second level, then third level, ….

A B Julie Jenny D E F C Jenny Jude Jiles I….

A

B

D E

C

HG

MI K

F

LJ

Page 9: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

9

Tracking State during Breadth-First Traversal

Breadth-First Traversal What nodes do we have to remember? Which order do we work on them?

A

B

D E

C

HG

MI K

F

LJ

Page 10: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

10

public void BreadthFirstTraversal(TreeNode root) {Queue<TreeNode> todo = new

LinkedList<TreeNode>();todo.offer(root); // add

while (!todo.isEmpty()) {TreeNode node = todo.poll(); // removeSystem.out.println(node.name());

for (TreeNode child : node.getChildren())todo.offer(child);

}}

Breadth-first Traversal: Use a Queue!

Look Ma’, no

recursion!

Look Ma’, no

recursion!

A

B

D E

C

HG

MI K

F

LJ

Page 11: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

11

public void DepthFirstTraversal(TreeNode root) {Stack <TreeNode> todo = new Stack<TreeNode>();todo.push(root); // add

while (!todo.isEmpty()) {TreeNode node = todo.pop(); // removeSystem.out.println(node.name());

for (TreeNode child : node.getChildren())todo.push(child);

}}

Depth-First Traversal: Use a Stack!

A

B

D E

C

HG

MI K

F

LJ

Look Ma’, no

recursion!

Look Ma’, no

recursion!

Which traversal

order does this

implement?

Which traversal

order does this

implement?

Page 12: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

12

In-order Traversal, iteratively?

D

B H

FCA I

E G

Page 13: 2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas

13

Depth-First vs Breadth-First

Depth-First: Visit one child and all of its descendants

before its siblings May visit root before, after, or between its

children + Requires only one branch to manage

traversal! − May find “costly” results first, and

may not find results at all (infinite search trees)

Breadth-First (level-order): Visit nodes in order of increasing depth May choose nodes in a level in a specific

order − Requires accumulation of levels to

manage traversal + Finds minimal solutions and guarantees

success!