Paul Tymann and Andrew Watkins

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
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.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
Data Structure Chapter# 5 Tree Course Instructor AMEER JAMAL SHAH.
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.
Discrete Structures Trees (Ch. 11)
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.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
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.
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.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Traversing Trees. Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node.
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.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
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. 
Discrete Mathematics Chapter 10 Trees.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Recursive Objects (Part 4)
Trees Chapter 11.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
CS212: Data Structures and Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Abstract Data Structures
Paul Tymann, Andrew Watkins,
Binary Tree Traversal Methods
Binary Tree Traversals
Section 9.3 by Andrew Watkins
Binary Tree Chapter 8 (cont’) Part2.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Paul Tymann, Andrew Watkins,
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Binary Trees.
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
A Binary Tree is a tree in which each node has at most 2 children
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

Paul Tymann and Andrew Watkins Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann and Andrew Watkins

Tree Anatomy The children of a node are, themselves, trees, called subtrees. Root Level 0 R Level 1 S T Internal Node Level 2 X U V W Leaf Level 3 Y Z Child of X Subtree Parent of Z and Y

Tree Traversals One of the most common operations performed on trees, are a tree traversals A traversal starts at the root of the tree and visits every node in the tree exactly once visit means to process the data in the node Traversals are either depth-first or breadth-first

Breadth First Traversals All the nodes in one level are visited Followed by the nodes the at next level Beginning at the root For the sample tree 7, 6, 10, 4, 8, 13, 3, 5 7 6 10 4 8 13 3 5

Depth-First Traversals There are 8 different depth-first traversals VLR (pre-order traversal) VRL LVR (in-order traversal) RVL RLV LRV (post-order traversal)

Pre-order Traversal: VLR Visit the node Do a pre-order traversal of the left subtree Finish with a pre-order traversal of the right subtree For the sample tree 7, 6, 4, 3, 5, 10, 8, 13 7 6 10 4 8 13 3 5

Preorder Traversal Step 1: Visit r Step 2: Visit T1 in preorder Tn Step 2: Visit T1 in preorder Step 3: Visit T2 in preorder Step n+1: Visit Tn in preorder

Example A R E Y P M H J Q T M A J Y R H P Q T E

Ordering of the preorder traversal is the same a the Universal Address System with lexicographic ordering. A R E Y P M H J Q T 1 2 3 2.2 2.1 1.1 2.2.1 2.2.2 2.2.3 M A J Y R H P Q T E

In-order Traversal: LVR Do an in-order traversal of the left subtree Visit the node Finish with an in-order traversal of the right subtree For the sample tree 3, 4, 5, 6, 7, 8, 10, 13 7 6 10 4 8 13 3 5

Inorder Traversal Step 1: Visit T1 in inorder Step 2: Visit r Tn Step 2: Visit r Step 3: Visit T2 in inorder Step n+1: Visit Tn in inorder

Example A R E Y P M H J Q T J A M R Y P H Q T E

inorder (t) if t != NIL: { inorder (left[t]); write (label[t]); inorder (right[t]); } Inorder Traversal on a binary search tree.

Post-order Traversal: LRV Do a post-order traversal of the left subtree Followed by a post-order traversal of the right subtree Visit the node For the sample tree 3, 5, 4, 6, 8, 13, 10, 7 7 6 10 4 8 13 3 5

Postorder Traversal Step 1: Visit T1 in postorder Tn Step 2: Visit T2 in postorder Step n: Visit Tn in postorder Step n+1: Visit r

Example A R E Y P M H J Q T J A R P Q T H Y E M

Representing Arithmetic Expressions Complicated arithmetic expressions can be represented by an ordered rooted tree Internal vertices represent operators Leaves represent operands Build the tree bottom-up Construct smaller subtrees Incorporate the smaller subtrees as part of larger subtrees

Example (x+y)2 + (x-3)/(y+2) + 2  / + x y – x 3 + y 2

Infix Notation Traverse in inorder (LVR) adding parentheses for each operation +  – / 2 x y 3 ( ) ( ) ( ) x + y  2 + ( ) ( ) x – 3 / ( ) y + 2

Prefix Notation (Polish Notation) Traverse in preorder (VLR) +  – / 2 x y 3 +  + x y 2 / – x 3 + y 2

Evaluating Prefix Notation In an prefix expression, a binary operator precedes its two operands The expression is evaluated right-left Look for the first operator from the right Evaluate the operator with the two operands immediately to its right

Example + / + 2 2 2 / – 3 2 + 1 0 + / + 2 2 2 / – 3 2 1 + / + 2 2 2 / 1 1 + / + 2 2 2 1 + / 4 2 1 + 2 1 3

Postfix Notation (Reverse Polish) Traverse in postorder (LRV) +  – / 2 x y 3 x y + 2  x 3 – y 2 + / +

Evaluating Postfix Notation In an postfix expression, a binary operator follows its two operands The expression is evaluated left-right Look for the first operator from the left Evaluate the operator with the two operands immediately to its left

Example 2 2 + 2 / 3 2 – 1 0 + / + 4 2 / 3 2 – 1 0 + / + 2 3 2 – 1 0 + / + 2 1 1 0 + / + 2 1 1 / + 2 1 + 3