Depth-first vs breadth-first traversals

Slides:



Advertisements
Similar presentations
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms Post-order Traversal: Left Child - Right Child - Root Depth-First Search.
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Information and Computer Sciences University of Hawaii, Manoa
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
2014-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Lecture 8 Tree.
Tree Data Structures.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
Trees A non-linear implementation for collection classes.
CSC 213 – Large Scale Programming. Trees  Represent hierarchical relationships  Parent-child basis of this abstract data type  Real-world example:
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
CS 201 Data Structures and Algorithms
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19
Paul Tymann and Andrew Watkins
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Tree.
Tree traversal from Introduction to Trees Chapter 6 Objectives
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Binary Search Trees (I)
i206: Lecture 13: Recursion, continued Trees
TREES General trees Binary trees Binary search trees AVL trees
8.2 Tree Traversals Chapter 8 - Trees.
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Paul Tymann, Andrew Watkins,
ASTs, Grammars, Parsing, Tree traversals
Trees CMSC 202, Version 5/02.
Binary Tree Traversal Methods
Binary Tree Traversals
Section 9.3 by Andrew Watkins
CE 221 Data Structures and Algorithms
Binary Tree Chapter 8 (cont’) Part2.
Paul Tymann, Andrew Watkins,
Non-Linear data structures
Binary Tree Implementation And Applications
8.2 Tree Traversals Chapter 8 - Trees.
Trees.
Presentation transcript:

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

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

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

Arithmetic expression trees Prefix Notation (Polish notation) + x 5 2 / - 7 3 9 plus( 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 / + + x / - 2 5 9 7 3 No Parentheses

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

(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

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?

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 …

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

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

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

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

In-order Traversal, iteratively? H B A C F I E G

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!