Tree Traversal. Traversal Algorithms preorder inorder postorder.

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.
CS 171: Introduction to Computer Science II
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
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.
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.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Binary Trees. Node structure Data A data field and two pointers, left and right.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
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.
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.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
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.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
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.
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.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
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.
CS 103 Discrete Structures Lecture 23 Trees (1). Second Midterm Exam 1 st Lecture in December (same time as the lecture) 75 minute duration Will cover.
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.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
Discrete Mathematics Chapter 10 Trees. Outline 10.1 Introduction to Trees 10.2 Applications of Trees 10.3 Tree Traversal 10.4 Spanning Trees 10.5 Minimal.
Discrete Mathematics Chapter 10 Trees.
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Discrete Mathematics Trees.
Planning & System installation
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Recursive Objects (Part 4)
Planning & System installation
Trees Chapter 11.
Paul Tymann and Andrew Watkins
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Abstract Data Structures
Paul Tymann, Andrew Watkins,
Principles of Computing – UFCFA3-30-1
Binary Tree Traversal Methods
Section 9.3 by Andrew Watkins
Paul Tymann, Andrew Watkins,
if the tree is empty, do nothing,
Chapter 20: Binary Trees.
Trees Chapter 11.
Binary Tree Traversal.
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

Tree Traversal

Traversal Algorithms preorder inorder postorder

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 output: a for each of {b, c, d}

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 for each of {b, c, d}

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 output: a b for each of {e, 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 for each of {e, 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

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 for each of {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 for each of {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

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 for each of {}

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 for each of {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

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 for each of {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 for each of {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

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 for each of {}

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 for each of {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

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 for each of {}

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 for each of {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

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 for each of {}

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 for each of {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 for each of {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 o p for each of {e, 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

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 for each of {}

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 for each of {e, 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 for each of {b, c, d}

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? 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: call stack r = a, l = ? s = {}

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: r = a, l = b s = {} call stack

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: r = a, l = b s = {} r = b, l = ? s = {} call stack

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: r = a, l = b s = {} r = b, l = e s = {} call stack

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: r = a, l = b s = {} r = b, l = e s = {} r = e, l = ? s = {} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {} r = j, l = ? s = {} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r =e, l = j s = {} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = ? s = {} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} call stack r = n, l = ? s = {}

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} call stack r = o, l = ? s = {}

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} call stack r = k, l = ? s = {}

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} call stack

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 r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} call stack

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 r = a, l = b s = {} r = b, l = e s = {} call stack

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 r = a, l = b s = {} r = b, l = e s = {f} call stack

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 r = a, l = b s = {} r = b, l = e s = {f} r = f, l = e s = {} call stack

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 r = a, l = b s = {} r = b, l = e s = {f} call stack

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 r = a, l = b s = {} call stack

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 r = a, l = b s = {c,d} call stack

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

In which order does a postorder traversal visit the vertices in this ordered rooted tree?

Infix, Prefix, and Postfix Notation represent complicated expressions using an ordered rooted tree (typically binary) Algebraic expressions preorder – Polish notation inorder – infix notation postorder – reverse Polish notation

Express in: a.Polish b.infix c.reverse Polish

Evaluating a prefix expression

Evaluate postfix expression * - 4 ^ 9 e / +

How do you do trees in Java?