1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
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.
Transforming Infix to Postfix
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
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.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Data Structure Chapter# 5 Tree Course Instructor AMEER JAMAL SHAH.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
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.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
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.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
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.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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.
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.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
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. 
Discrete Mathematics Chapter 10 Trees.
CC 215 DATA STRUCTURES TREES TRAVERSALS Dr. Manal Helal - Fall 2014 Lecture 9 AASTMT Engineering and Technology College 1.
Chapter 12 Abstract Data Type.
Trees Chapter 15.
Paul Tymann and Andrew Watkins
Section 8.1 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.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Paul Tymann, Andrew Watkins,
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden

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

3 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

4 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 Queue and stack A queue is a sequence of elements such that each new element is added (enqueued) to one end, called the back of the queue, and an element is removed (dequeued) from the other end, called the front A stack is a sequence of elements such that each new element is added (or pushed) onto one end, called the top, and an element is removed (popped) from the same end

6 Breadth first tree traversal with a queue Enqueue root While queue is not empty –Dequeue a vertex and write it to the output list –Enqueue its children left-to-right StepOutputQueue                                     

7 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)

8 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,

9 Pre-order tree traversal with a stack Push root onto the stack While stack is not empty –Pop a vertex off stack, and write it to the output list –Push its children right-to-left onto stack StepOutputStack                                     

10 Preorder Traversal r T1T1 T2T2 TnTn Step 1: Visit r Step 2: Visit T 1 in preorder Step n +1: Visit T n in preorder Step 3: Visit T 2 in preorder

11 Example AREYPMHJQT A R EY P M HJ QT

12 Ordering of the preorder traversal is the same a the Universal Address System with lexicographic ordering. AREYPMHJQT A R EY P M HJ QT

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

14 Inorder Traversal Step 1: Visit T 1 in inorder Step 2: Visit r Step n +1: Visit T n in inorder Step 3: Visit T 2 in inorder r T1T1 T2T2 TnTn

15 Example AREYPMHJQT A R EY P M HJ QT

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

17 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

18 Postorder Traversal Step 1: Visit T 1 in postorder Step 2: Visit T 2 in postorder Step n +1: Visit r Step n : Visit T n in postorder r T1T1 T2T2 TnTn

19 Example AREYPMHJQT A R EY P M HJ QT

20 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

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

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

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

24 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

25 Example + / / – / / – / / / /

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

27 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 Evaluating Postfix Notation

28 Example / 3 2 – / / 3 2 – / – / / /