Download presentation
Presentation is loading. Please wait.
1
Depth-first vs breadth-first traversals
COMP 103 Depth-first vs breadth-first traversals Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas Kuehne VUW Marcus Frean School of Engineering and Computer Science, Victoria University of Wellington 2014-T2 Lecture 23
2
RECAP-TODAY RECAP Building and Traversing Binary Trees TODAY
Depth-First vs Breadth-First Traversal Chapter 16.2 ANNOUNCEMENTS – TOMORROW: assignment #7 is due (9:30) no 10am lecture (but will hand out assignment 8) after that, copies in corridor of Cotton 2nd floor
3
recap: depth-first traversals
Variations: 1. pre-order: node, then children 2. in-order: left child, node, right child 3. post-order: children, then node A B C D E F C B D A F E C D B F E A A Maze Analogy keep your left hand on the wall, 1. pre-order: first encounter 2. in-order: second encounter 3. post-order: third encounter A and act on B E C D F
4
Arithmetic expression trees
Prefix Notation (Polish notation) + x 5 2 / plus( times(5, 2), divide( minus(7, 3), ) ) Infix Notation (5 x 2 ) + ( ( 7 – 3) / 9) Postfix Notation (Reverse Polish) 5 2 x 7 3 – 9 / + + x / - 2 5 9 7 3 No Parentheses
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 H G M I K F L J
6
(recap slide) Traversing a General Tree
Recursion is the most natural and easiest approach Iterate through the children at each node printAll() G T K M Z A printAll() printAll() public void printAll() { System.out.println(item); for (GenTreeNode<E> child : children) child.printAll(); } printAll() printAll() printAll() Hint: Finding is like Printing with an early exit potential
7
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?
8
Managing Depth-First Traversal
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! A B D E C H G M I K F L J A print own name process children... return B print own name process children... return D print own name process children... return …
9
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 H G M I K F L J
10
Tracking State during Breadth-First Traversal
What nodes do we have to remember? Which order do we work on them? A B D E C H G M I K F L J
11
Breadth-first Traversal: Use a Queue!
public void BreadthFirstTraversal(TreeNode root) { Queue<TreeNode> todo = new LinkedList<TreeNode>(); todo.offer(root); // add while (!todo.isEmpty()) { TreeNode node = todo.poll(); // remove System.out.println(node.name()); for (TreeNode child : node.getChildren()) todo.offer(child); } Look Ma’, no recursion! A B D E C H G M I K F L J
12
Depth-First Traversal: Use a Stack!
public void DepthFirstTraversal(TreeNode root) { Stack <TreeNode> todo = new Stack<TreeNode>(); todo.push(root); // add while (!todo.isEmpty()) { TreeNode node = todo.pop(); // remove System.out.println(node.name()); for (TreeNode child : node.getChildren()) todo.push(child); } Which traversal order does this implement? Look Ma’, no recursion! A B D E C H G M I K F L J
13
In-order Traversal, iteratively?
H B A C F I E G
14
Depth-First vs Breadth-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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.