Download presentation
Presentation is loading. Please wait.
Published byAubrey Powers Modified over 9 years ago
1
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas Kuehne VUW COMP 103 Thomas Kuehne Traversing Trees
2
2 RECAP-TODAY RECAP Building and Traversing Binary Trees TODAY Depth-First vs Breadth-First Traversal Chapter 16.2 ANNOUNCEMENTS no new assignment this week prepare for TEST 2, on Monday, 7/10, 10.00
3
3 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 MI K F L J
4
4 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?
5
5 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 1.print own name 2.process children... 3.return Managing Depth-First Traversal A B D E C H G MI K F L J B 1.print own name 2.process children... 3.return D 1.print own name 2.process children... 3.return …
6
6 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 MI K F L J
7
7 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 H G MI K F L J
8
8 public void BreadthFirstTraversal(TreeNode root) { Queue todo = new LinkedList (); 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); } } Breadth-first Traversal: Use a Queue! Look Ma’, no recursion! A B D E C H G MI K F L J
9
9 public void DepthFirstTraversal(TreeNode root) { Stack todo = new Stack (); 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); } Depth-First Traversal: Use a Stack! A B D E C H G MI K F L J Look Ma’, no recursion! Which traversal order does this implement?
10
10 What do we need to remember? In-order Traversal, iteratively? + x / -2514 73
11
11 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!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.