7. TREE TRAVERSAL Tree Traversal

Similar documents
PROF. BOYAN KOSTADINOV NEW YORK CITY COLLEGE OF TECHNOLOGY, CUNY

Algebra Review. How well do you remember your algebra?

Binary Representation of Numbers Autar Kaw

Polynomial Functions. Polynomial functions in one variable can be written in expanded form as ( )

Example 27.1 Draw a Venn diagram to show the relationship between counting numbers, whole numbers, integers, and rational numbers.

Math 135 Circles and Completing the Square Examples

How fast can we sort? Sorting. Decision-tree model. Decision-tree for insertion sort Sort a 1, a 2, a 3. CS Spring 2009

Experiment 6: Friction

6.2 Volumes of Revolution: The Disk Method

Integration by Substitution

FUNCTIONS AND EQUATIONS. xεs. The simplest way to represent a set is by listing its members. We use the notation

Use Geometry Expressions to create a more complex locus of points. Find evidence for equivalence using Geometry Expressions.

2 DIODE CLIPPING and CLAMPING CIRCUITS

SPECIAL PRODUCTS AND FACTORIZATION

LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES

Babylonian Method of Computing the Square Root: Justifications Based on Fuzzy Techniques and on Computational Complexity

Bayesian Updating with Continuous Priors Class 13, 18.05, Spring 2014 Jeremy Orloff and Jonathan Bloom

Regular Sets and Expressions

A.7.1 Trigonometric interpretation of dot product A.7.2 Geometric interpretation of dot product

Section 7-4 Translation of Axes

Novel Methods of Generating Self-Invertible Matrix for Hill Cipher Algorithm

Decision Rule Extraction from Trained Neural Networks Using Rough Sets

trademark and symbol guidelines FOR CORPORATE STATIONARY APPLICATIONS reviewed

P.3 Polynomials and Factoring. P.3 an 1. Polynomial STUDY TIP. Example 1 Writing Polynomials in Standard Form. What you should learn

Review guide for the final exam in Math 233

Homework 3 Solutions

Econ 4721 Money and Banking Problem Set 2 Answer Key

Factoring Polynomials

Drawing Diagrams From Labelled Graphs

Reasoning to Solve Equations and Inequalities

Small Business Networking

Section 5-4 Trigonometric Functions

Graphs on Logarithmic and Semilogarithmic Paper

Small Business Networking

Vectors Recap of vectors

Operations with Polynomials

and thus, they are similar. If k = 3 then the Jordan form of both matrices is

Small Business Networking

Small Business Networking

PHY 222 Lab 8 MOTION OF ELECTRONS IN ELECTRIC AND MAGNETIC FIELDS

Pentominoes. Pentominoes. Bruce Baguley Cascade Math Systems, LLC. The pentominoes are a simple-looking set of objects through which some powerful

Lecture 3 Gaussian Probability Distribution

9 CONTINUOUS DISTRIBUTIONS

According to Webster s, the

Treatment Spring Late Summer Fall Mean = 1.33 Mean = 4.88 Mean = 3.

One Minute To Learn Programming: Finite Automata

How To Network A Smll Business

Mathematics. Vectors. hsn.uk.net. Higher. Contents. Vectors 128 HSN23100

5.2. LINE INTEGRALS 265. Let us quickly review the kind of integrals we have studied so far before we introduce a new one.

Or more simply put, when adding or subtracting quantities, their uncertainties add.

Chapter 2 The Number System (Integers and Rational Numbers)

Integration. 148 Chapter 7 Integration


I calculate the unemployment rate as (In Labor Force Employed)/In Labor Force

4.11 Inner Product Spaces

Warm-up for Differential Calculus

FAULT TREES AND RELIABILITY BLOCK DIAGRAMS. Harry G. Kwatny. Department of Mechanical Engineering & Mechanics Drexel University

Scalar and Vector Quantities. A scalar is a quantity having only magnitude (and possibly phase). LECTURE 2a: VECTOR ANALYSIS Vector Algebra

Basic Analysis of Autarky and Free Trade Models

LECTURE #05. Learning Objective. To describe the geometry in and around a unit cell in terms of directions and planes.

MATH 150 HOMEWORK 4 SOLUTIONS

19. The Fermat-Euler Prime Number Theorem

Basic Research in Computer Science BRICS RS Brodal et al.: Solving the String Statistics Problem in Time O(n log n)

Solving BAMO Problems

Exponential and Logarithmic Functions

The Definite Integral

The Velocity Factor of an Insulated Two-Wire Transmission Line

9.3. The Scalar Product. Introduction. Prerequisites. Learning Outcomes

Network Configuration Independence Mechanism

5.6 POSITIVE INTEGRAL EXPONENTS

Object Semantics Lecture 2

EQUATIONS OF LINES AND PLANES

Basically, logarithmic transformations ask, a number, to what power equals another number?

Humana Critical Illness/Cancer

How To Set Up A Network For Your Business

Helicopter Theme and Variations

Measuring Similarity between Graphs Based on the Levenshtein Distance

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall Final Exam

AntiSpyware Enterprise Module 8.5

Module Summary Sheets. C3, Methods for Advanced Mathematics (Version B reference to new book) Topic 2: Natural Logarithms and Exponentials

Example A rectangular box without lid is to be made from a square cardboard of sides 18 cm by cutting equal squares from each corner and then folding

AREA OF A SURFACE OF REVOLUTION

On the Robustness of Most Probable Explanations

Solving the String Statistics Problem in Time O(n log n)

AA1H Calculus Notes Math1115, Honours John Hutchinson

Module 2. Analysis of Statically Indeterminate Structures by the Matrix Force Method. Version 2 CE IIT, Kharagpur

Multiplication and Division - Left to Right. Addition and Subtraction - Left to Right.

Applications to Physics and Engineering

e.g. f(x) = x domain x 0 (cannot find the square root of negative values)

Enterprise Risk Management Software Buyer s Guide

SUBSTITUTION I.. f(ax + b)

2. Transaction Cost Economics

How To Study The Effects Of Music Composition On Children

CS99S Laboratory 2 Preparation Copyright W. J. Dally 2001 October 1, 2001

MODULE 3. 0, y = 0 for all y

1. Find the zeros Find roots. Set function = 0, factor or use quadratic equation if quadratic, graph to find zeros on calculator

Answer, Key Homework 10 David McIntyre 1

Optiml Control of Seril, Multi-Echelon Inventory (E&I) & Mixed Erlng demnds

DlNBVRGH + Sickness Absence Monitoring Report. Executive of the Council. Purpose of report

Discovering General Logical Network Topologies

Transcription:

7. TREE TRAVERSAL 106 7. Tree Trversl 7.1. Ordered Trees. Definition 7.1.1. An ordered tree is rooted tree in which the set of children of ech vertex is ssigned totl order. In drwing of n ordered rooted tree, with the root shown t the top, the children of vertex re shown from left to right. The concept of n ordered tree in some wy generlizes the ide of binry tree in which children of vertex hve been designted s left or right, if we think of the left child of vertex s being less thn the right child. The nlogy only breks down for binry trees tht re not complete, however, since some vertex my hve only left child, wheres nother my only hve right child. 7.2. Universl Address System. Definition 7.2.1. The set of vertices of n ordered tree T cn be provided with totl order, clled universl ddress system, using the following recursive process. Bsis: Lbel the root 0, nd lbel the n children of the root 1, 2,..., n from left to right. Recursion: Given vertex v with lbel L t level k 1, lbel its children from left to right. L.1, L.2,..., L.n v Totlly order the vertices of T using the lexicogrphic ordering. Exmple 7.2.1. The following tble gives the universl ddress system nd the resulting ordering of the vertices for the ordered tree shown below. < b < e < f < c < g 0 < 1 < 1.1 < 1.2 < 2 < 2.1 g < h < i < d < j < k < l 2.1 < 2.2 < 2.3 < 3 < 3.1 < 3.2 < 3.3

7. TREE TRAVERSAL 107 b c d e f g h i j k l 7.3. Tree Trversl. Suppose we hve informtion stored in n ordered rooted tree. How do we recover informtion from the tree? Tht is, how do we visit the vertices in the tree? We shll look t severl procedures for visiting, or listing, the vertices of the tree. Ech procedure is defined recursively on the subtrees, nd ech is bsed on pth tht proceeds to the leftmost child of vertex tht hs not occurred in the pth before moving to vertex to the right. These lgorithms only differ s to when the root of subtree is visited (or listed) reltive to the vertices of its subtrees. Definition 7.3.1. A procedure used to systemticlly visit ech vertex in tree is clled trversl lgorithm, or trversl. Notice tht subtree of T tht does not include the root must hve fewer vertices. Therefore, the recursive definition mkes sense. Just keep pplying the recursion step until you get to the leves. Once you re down to subtree tht consists only of lef pply the bsis step. 7.4. Preorder Trversl. Definition 7.4.1. The preorder trversl of rooted tree, T, with n vertices is defined recursively s follows: Bsis: If n = 1, then the root is the only vertex, so we visit the root. Recursive Step: When n > 1 consider the subtrees, T 1, T 2, T 3,..., T k of T whose roots re ll the children of the root of T. Visit ech of these subtrees from left to right. Exmple 7.4.1. In the Preorder Trversl of the the vertices in the following tree the vertices re visited in the following order:, b, d, h, i, c, e, f, j, k, g.

7. TREE TRAVERSAL 108 b c d e f g h i j k To begin finding the preorder trversl we begin with. Next find the preorder trversl for the subtree b T 1 d h i List tht trversl nd then find nd list the prerorder trversl for the subtree c T 2 e f g j Now to find the preorder trversl of the subtree, T 1 we strt with b nd find the preorder trversl of k

7. TREE TRAVERSAL 109 S 1 d h i The preorder trversl of this one is d, h, i. The bsis step ws finlly used to find the preorder trversl of S 1 s subtrees. The preorder trversl of T 2 still needs to be found. It is c, e, f, j, k, g. By putting ll the vertices together in the order in which they were listed we get the preorder trversl, b, d, h, i, c, e, f, j, k, g. One recommendtion is to tke the originl grph nd point to ech vertex in the order listed in the preorder trversl to help coordinte the order they re given in the list to their plce in the tree. Notice tht if tree is ordered using the universl ddress system, then listing of the vertices in incresing order is preorder trversl of the tree. 7.5. Inorder Trversl. Definition 7.5.1. In n inorder trversl of tree the root of tree or subtree is visited fter the vertices of the leftmost subtree, but before the vertices of ll other subtrees. Note tht in the inorder trversl, if vertex, v, hs multiple subtrees originting from it v is plced between the vertices of the leftmost subtree nd the vertices of ll the other subtrees. Exercise 7.5.1. Give creful, recursive definition of n inorder trversl. Exmple 7.5.1. The inorder trversl of this tree below is h, d, i, b,, e, c, k, j, f, g. b c d e f g h i j k

7. TREE TRAVERSAL 110 Exmple 7.5.2. Here is nother exmple to look t. Agin, point to ech vertex in the order it is listed to develop n understnding of the reltionship between inorder trversl nd the grph. b c d e f g The inorder trversl is b,, e, c, f, g, d. 7.6. Postorder Trversl. Definition 7.6.1. In postorder trversl of tree, the root of tree/subtree is visited fter ll of the vertices of its subtrees re visited. Exmple 7.6.1. The postorder trversl of the tree below is h, i, d, b, e, k, j, f, g, c,. b c d e f g h i j k

7. TREE TRAVERSAL 111 The postorder trversl is when the root of subtree is visited fter ll its vertices hve been visited. Exmple 7.6.2. We cn find the postorder trversl of the tree b c d e f g Postorder Trversl: b, e, f, g, c, d,. Exercise 7.6.1. Give creful, recursive definition of n postorder trversl. 7.7. Infix Form. Definitions 7.7.1. (1) A fully prenthesized expression is in infix form. (2) The expression obtined by trversing the ordered rooted tree by prefix trversl is prefix form or Polish nottion. (3) The expression obtined by trversing the ordered rooted tree by postfix trversl is postfix form or reverse Polish nottion. Exmple 7.7.1. 3(x y) + (x + y)2 4 (1) Find the infix form. (2) Drw the rooted tree for the lgebric expression. (3) Find the prefix form. (4) Find the postfix form. (1) The infix form is Solutions (3 (x y)) + (((x + y) 2)/4)

(2) The rooted tree for the lgebric expression is 7. TREE TRAVERSAL 112 + * / 3-4 x y + 2 x y (3) The prefix form is + 3 x y / + x y 2 4 (4) The postfix form is 3 x y x y + 2 4 / + An lgebric representtion consists of finite number of vribles nd numbers binry opertions: ddition +, subtrction, multipliction, division /, exponentition. Py ttention to the fct tht the symbol is used for exponentition in this mteril. For exmple, 2 3 = 2 3. In these notes we define the infix, prefix, nd postfix forms of n lgebric expression nd give n exmple. The tree referred to in exmple 7.7.1 is found s follows. A binry ordered tree cn be built hving internl vertices lbeled by the opertors nd leves lbeled by the vribles nd numbers. We strt from the innermost expressions nd work our wy outwrd constructing subtrees from the innermost expressions. These re joined together to form lrger subtrees using the opertions tht join the innermost expressions. The inorder listing of the vertices then reproduces the

7. TREE TRAVERSAL 113 expression provided the prentheses re inserted s follows: s you begin to trverse subtree from n internl vertex (opertion) insert n open prenthesis; s you leve the subtree insert closed prenthesis. Prefix nd postfix nottion cn be found from the tree. Notice tht the prefix nd postfix nottions do not require prenthesis. A lgebric expression in prefix or postfix nottion is unmbiguous. Infix form requires prentheses, however, in order to resolve mbiguities. Despite this fct, infix nottion is pretty much the universlly ccepted form for writing lgebric expressions. We hve dopted convention, so well known tht we tke it for grnted, tht llows us to reduce, but not eliminte, the number of prentheses needed without creting mbiguities. Nmely, we gree tht, in the bsence of prentheses, the binry opertions re performed in the following order: exponentition, multipliction, division, ddition, subtrction. Exercise 7.7.1. Find the prefix nd postfix forms for the lgebric expressions (( b) + (c/(d 3))) nd ( (((b + c)/d) 3)). Exmple 7.7.2. Find the infix form of the expression given in prefix form. + x y 5 / 2 + x y 3 Solution (1) + x y 5 / 2 (+ x y) 3 (2) + x y 5 / 2 (x + y) 3 (3) + x y 5 / ( 2 (x + y)) 3 (4) + x y 5 / (2 (x + y)) 3 (5) + x y 5 (/ (2 (x + y)) 3) (6) + x y 5 ((2 (x + y))/3) (7) (+ x y) 5 ((2 (x + y))/3) (8) (x + y) 5 ((2 (x + y))/3) (9) ( (x + y) 5) ((2 (x + y))/3) (10) ((x + y) 5) ((2 (x + y))/3) (11) ( ((x + y) 5) ((2 (x + y))/3)) (12) (((x + y) 5) ((2 (x + y))/3)) To go from prefix form to infix form, we red the expression right from left. Look for the rightmost opertion nd the vribles or numbers immeditely to the right of it. Put prenthesis round these nd chnge to infix nottion. Move to the next right most opertion nd put prenthesis round it nd the expressions just to the right of it. Chnge to infix nottion, nd so on. Exmple 7.7.3. Find the vlue of the postfix expression 6 2 9 / 7 2 3 + 5 / + Solution

(1) (6 2 ) 9 / 7 2 3 + 5 / + (2) (6 2 ) 9 / 7 2 3 + 5 / + (3) 36 9 / 7 2 3 + 5 / + (4) (36 9 /) 7 2 3 + 5 / + (5) (36/9) 7 2 3 + 5 / + (6) 4 7 (2 3 ) + 5 / + (7) 4 7 8 + 5 / + (8) 4 (7 8 +) 5 / + (9) 4 15 5 / + (10) 4 (15 5 /) + (11) 4 3 + (12) 7 7. TREE TRAVERSAL 114 This time we look for the left most opertion nd the numbers immeditely to the left of it. The exct sme procedures my be pplied to logicl expressions s well s set opertion expressions. Exmple 7.7.4. Given the logicl expression (p q) r 1. Find the infix nottion. 2. Represent using n ordered rooted tree. 3. Find the preorder form. 4. Find the postorder form. Solution: 1. (p ( q)) ( r) 2. Tree: v p r 3. Preorder: p q r 4. Postorder: pq r q