2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.

Slides:



Advertisements
Similar presentations
Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:
Advertisements

Lecture 16: Tree Traversal.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Three Types of Depth-First Search Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Lecture 06: Tree Structures Topics: Trees in general Binary Search Trees Application: Huffman Coding Other types of Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
2014-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Trees – Part 2 CS 367 – Introduction to Data Structures.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
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.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
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.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
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
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Tree.
Section 8.1 Trees.
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Binary Search Trees (I)
Depth-first vs breadth-first traversals
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Principles of Computing – UFCFA3-30-1
Chapter 20: Binary Trees.
Binary Tree Implementation And Applications
Trees.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

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 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 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 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  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 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 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 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 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  What do we need to remember? In-order Traversal, iteratively? + x /

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!