Principles of Computing – UFCFA3-30-1

Slides:



Advertisements
Similar presentations
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms Post-order Traversal: Left Child - Right Child - Root Depth-First Search.
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Three Types of Depth-First Search Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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 CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Discrete Structures Trees (Ch. 11)
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.
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.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Binary Search Trees (BST)
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Discrete Mathematics Chapter 10 Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
CSCE 210 Data Structures and Algorithms
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
CS223 Advanced Data Structures and Algorithms
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Trees.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Abstract Data Structures
Trees.
Binary Trees.
Binary Tree Traversal Methods
CS-240 Dick Steflik D.J. Foreman
Trees.
if the tree is empty, do nothing,
Trees.
Chapter 20: Binary Trees.
Trees Chapter 11.
Data Structures Using C++ 2E
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

Principles of Computing – UFCFA3-30-1 Week-11 Trees Data Structure Instructor : Mazhar H Malik Email : mazhar@gcet.edu.om Global College of Engineering and Technology

Trees A tree data structure can be defined recursively (locally) as a collection of nodes (starting at a root node), where each node is a data structure consisting of a value, together with a list of references to nodes (the "children"), with the constraints that no reference is duplicated, and none points to the root.

Tree Traversal

Traversal Algorithms Depth First Search preorder inorder postorder

Preoder, Inorder, Postorder In Preorder, the root is visited before (pre) the subtrees traversals In Inorder, the root is visited in-between left and right subtree traversal is visited after (pre) Preorder Traversal: Visit the root Traverse left subtree Traverse right subtree Inorder Traversal: Traverse left subtree Visit the root Traverse right subtree Postorder Traversal: Traverse left subtree Traverse right subtree Visit the root

PreOrder Traversal

Inorder Traversal

Postorder Traversal

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output:

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {b, c, d} output: a

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {b, c, d} output: a

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {e, f} output: a b

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {e, f} output: a b

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {j, k} output: a b e

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {j, k} output: a b e

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {j, k} output: a b e j

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {n, o, p} output: a b e j k

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {n, o, p} output: a b e j k

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j k n

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {n, o, p} output: a b e j k n

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j k n o

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {n, o, p} output: a b e j k n o

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j k n o p

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {n, o, p} output: a b e j k n o p

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {j, k} output: a b e j k n o p

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {e, f} output: a b e j k n o p

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p f

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j k n o p f

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {e, f} output: a b e j k n o p f

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {b, c, d} output: a b e j k n o p f

In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p f c d g l m h i

In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output:

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = ? s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output:

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output:

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = ? s = {} output:

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} output:

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = ? s = {} output:

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {} output: j

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {} r = j, l = ? s = {} output: j

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r =e, l = j s = {} output: j e

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} output: j e

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} r = k, l = ? s = {} output: j e

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} output: j e

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} r = n, l = ? s = {} output: j e n

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} output: j e n k

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} output: j e n k

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} r = o, l = ? s = {} output: j e n k o

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} output: j e n k o

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} r = k, l = ? s = {} output: j e n k o p

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} output: j e n k o p

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} r = e, l = j s = {k} output: j e n k o p

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {} output: j e n k o p b

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {f} output: j e n k o p b

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {f} r = f, l = e s = {} output: j e n k o p b f

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end r = b, l = e s = {f} output: j e n k o p b f

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b f

In which order does a inorder traversal visit the vertices in this ordered rooted tree? call stack r = a, l = b s = {c,d} procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b f

In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b f a c l g m d h i

Illustrations for Traversals Assume: visiting a node is printing its label Preorder: 1 3 5 4 6 7 8 9 10 11 12 Inorder: 4 5 6 3 1 8 7 9 11 10 12 Postorder: 4 6 5 3 8 11 12 10 9 7 1 1 3 11 9 8 4 6 5 7 12 10

Illustrations for Traversals (Contd.) Assume: visiting a node is printing its data Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30 Inorder: 2 3 6 7 8 10 11 12 14 15 20 22 27 30 Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15 6 15 8 2 3 7 11 10 14 12 20 27 22 30

a j b k c g m d l i h f e preorder: a j k m l b c g i h d f e inorder: m k l j a b i g h c f d e postorder: m l k j i h g f e d c b a