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