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

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

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:
2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
CS 171: Introduction to Computer Science II
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.
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 Traversal. Traversal Algorithms preorder inorder postorder.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Binary Trees. Node structure Data A data field and two pointers, left and right.
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.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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.
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.
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.
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.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
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.
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
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.
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 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 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.
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
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,
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
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.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
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.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
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. 
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
Recursive Objects (Part 4)
Paul Tymann and Andrew Watkins
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Tree.
Section 8.1 Trees.
Binary Search Trees (I)
Depth-first vs breadth-first traversals
8.2 Tree Traversals Chapter 8 - Trees.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Section 9.3 by Andrew Watkins
Binary Tree Chapter 8 (cont’) Part2.
Non-Linear data structures
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas Kuehne VUW COMP 103 Marcus Frean Depth-first vs breadth-first traversals

2 RECAP-TODAY RECAP  Building and Traversing Binary Trees TODAY  Depth-First vs Breadth-First Traversal  Chapter 16.2 ANNOUNCEMENTS  no new assignment next week

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 E FD C 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 and act on

4  Prefix Notation (Polish notation) + x 5 2 / 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 / + Arithmetic expression trees + x / 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 MI K F L J

6 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?

7  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 …

8 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

9 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

10 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

11 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?

12 In-order Traversal, iteratively? D B H FCAI EG

13 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!