COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Lecture 16: Tree Traversal.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
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 Traversal. Traversal Algorithms preorder inorder postorder.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CS261 Data Structures Tree Traversals. Goals Euler Tours Recursive Implementation Tree Sort Algorithm.
Binary Trees. Node structure Data A data field and two pointers, left and right.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
COMP 103 Introduction to 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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
Tree Traversal.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
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.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Binary Search Trees Data Structures Ananda Gunawardena
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
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.
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.
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.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
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.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Saurav Karmakar
Building and Traversing Trees
Recursive Objects (Part 4)
Paul Tymann and Andrew Watkins
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Tree.
Section 8.1 Trees.
COMP 103 Binary Search Trees.
Depth-first vs breadth-first traversals
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Find in a linked list? first last 7  4  3  8 NULL
Binary Tree Traversal Methods
Abstract Data Structures
Trees.
Paul Tymann, Andrew Watkins,
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Binary Tree Traversal Methods
Binary Tree Traversals
Binary Tree Chapter 8 (cont’) Part2.
Paul Tymann, Andrew Watkins,
Trees.
Chapter 20: Binary Trees.
Binary Tree Implementation And Applications
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

COMP 103 Traversing Trees

2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2

3 Depth first traversal – a "maze walk"  To get out of a maze: keep your left hand on the wall Variations:  pre-order: ABCDEFRoot, then children  post-order: CDBFEAChildren, then root  in-order (binary): CBDAFE Left child, root, right child A B E FD C

4 Depth First Traversal  Visit the subtree under each child before going to next child  Pre-order: process the node, then process the subtrees  Post-order: process the subtrees, then process the node. Easy to do recursively: the activation stack records where you are up to. Justine JeremyJulie John Julia Jesse Jordan Jenna JackJacobJules James JadaJenny Jackie Jenny Jake Jed Jude Jiles JoanJimJade

5 Depth-first traversal, recursively  eg: List all employee names  Use recursion, but use a loop to step through the children private void listAll(OrgTreeNode node) { System.out.println(node.employee); for (OrgTreeNode ch : node.children) listAll(ch); } OR private void listAll(OrgTreeNode node) { for (OrgTreeNode ch : node.children) listAll(ch); System.out.println(node.employee); } PRE-ORDER POST-ORDER

6 Count nodes  This involves doing a traversal and returning a value: private int count(TreeNode node) { int ans = 1; for (TreeNode ch : node.children) ans += count(ch); return ans; }

7 Finding a node  Same as a traversal, but “jump out” once you find it... private TreeNode find(TreeNode node, String name) { if (node.employee.hasName(name) ) return node; for (TreeNode ch : node.children) { TreeNode ans = find(ch, name); if (ans != null) return ans; } return null; } Search is a traversal with an early exit Search is a traversal with an early exit.

8 Arithmetic expression trees (5 x 2 ) + ( ( 7 – 3) / 14) + x 5 2 / "plus( times(5, 2), divide( minus(7, 3), 14) )" 5 2 x 7 3 – 14 / + + x / in-order traversal of the tree! "Polish notation": pre-order traversal “Reverse Polish notation” post-order traversal

9  We need to remember what nodes we are still working on, and which of their children we have processed already  In a recursive depth-first traversal (eg. "ListAll" from earlier), the activation stack remembers it all, automatically Justine 1.print own name 2.process children... 3.return Tracking State during Depth-first traversal Justine Jeremy John Julia Jordan Jenna Jack JacobJules James Jada Jackie Jake Jeremy 1.print own name 2.process children... 3.return John 1.print own name 2.process children... 3.return Refer to the “onion” example we looked at in the lecture on recursion

10 Breadth-first Traversal  What about printing the nodes by layer?  root first  then second layer,  then third layer, …. Justine Jeremy Julie Jenny John Julia Jada Jordan Jenny Jude Jiles Jules…. Justine JeremyJulie John Julia Jesse Jordan Jenna JackJacobJules James JadaJenny Jackie Jenny Jake Jed Jude Jiles JoanJimJade

11 Tracking State during Breadth-first traversal  Breadth first traversal:  What nodes do we have to remember?  Which order do we work on them? Justine Jeremy John Julia Jordan Jenna Jack JacobJules James Jada Jackie Jake

12 private void BreadthFirstTraversal(TreeNode root) { Queue todo = new LinkedList (); todo.offer(root); // enqueue while (!todo.isEmpty()) { TreeNode node = todo.poll(); // dequeue System.out.println(node.employee()); for (TreeNode child : node.getChildren()) todo.offer(child); } } Breadth-first Traversal: use a Queue! Justine Jeremy John Julia Jordan Jenna Jack JacobJules James Jada Jackie Jake Iterative

13 Depth First, iteratively: same code, but use Stack! private void DepthFirstTraversal(TreeNode root) { Stack todo = new Stack (); todo.push(root); while (!todo.isEmpty()) { TreeNode node = todo.pop(); System.out.println(node.employee()); for (TreeNode child : node.getChildren()) todo.push(child); } Justine Jeremy John Julia Jordan Jenna Jack JacobJules James Jada Jackie Jake pre- ? post- ? in- ?

14 In-order Traversal, iteratively?  What do we need to remember? + x /

15 Summary: Traversals  Depth First  pre-order : in-order : post-order  Recursive (easy)  Iterative pre-order, with a Stack  Iterative in-order ??  Breadth First  can’t do it recursively  Iterative with a Queue.  Traversal vs Search  Basically the same thing, but search can exit in the middle.

16 Summary: Kinds of trees  Binary trees... Ternary trees … General trees  Ordered children (List)... Unordered children (Set)  Nodes with just children... children & parent... just parents  Tree structures to store structured data to keep track of an algorithm to store unstructured data efficiently (coming next…)  Ex: Compare two trees to see if they’re the same, or whether one is a subtree of the other.