Docsity

11.3 Tree Traversal, Study notes of Discrete Mathematics

Toggle sidebar
bg1
ICS 241: Discrete Mathematics II (Spring 2015)
11.3 Tree Traversal
Universal Address Systems
A way to totally order the vertices of and ordered rooted tree. We do this recursively:
•Label the root with the integer 0. Then label its kchildren (at level 1) from left to right with
1,2,3, . . . , k.
•For each vertex vat level nwith label A, label its kvchildren, as they are drawn from left to
right, with A.1, A.2, . . . , A.kv.
0
3
3.1
3.1.2
3.1.1
2
2.3
2.2
2.1
1
1.3
1.2
1.1
1.1.2
1.1.1
The lexicographic ordering for this tree is 0<1<1.1<1.1.1<1.1.2<1.2<1.3<2<2.1<
2.2<2.3<3<3.1<3.1.1<3.1.2
Traversal Algorithms
Preorder Traversal
Let Tbe an ordered rooted tree with root r. If Tconsists only of r, then ris the preorder traversal
of T. Otherwise, suppose that T1, T2, . . . , Tnare the subtrees at rfrom left to right in T. The pre-
order traversal begins by visiting r. It continues by traversing T1in preorder, then T2in preorder,
and so on, until Tnis traversed in preorder.
Algorithm preorder(T: ordered rooted tree)
1: r= root of T
2: list r
3: for each child cof rfrom left to right do
4: T(c)= subtree with cas its root
5: preorder(T(c))
6: end for
Inorder Traversal
Let Tbe an ordered rooted tree with root r. If Tconsists only of r, then ris the inorder traversal
of T. Otherwise, suppose that T1, T2, . . . , Tnare the subtrees at rfrom left to right. The inorder
traversal begins by traversing T1in inorder, then visiting r. It continues by traversing T2in inorder,
then T3in inorder, .. . , and finally Tnin inorder.
1
pf3
pf4
pf5

Partial preview of the text

Download 11.3 Tree Traversal and more Study notes Discrete Mathematics in PDF only on Docsity!

11.3 Tree Traversal

Universal Address Systems

A way to totally order the vertices of and ordered rooted tree. We do this recursively:

  • Label the root with the integer 0. Then label its k children (at level 1) from left to right with 1 , 2 , 3 ,... , k.
  • For each vertex v at level n with label A, label its kv children, as they are drawn from left to right, with A. 1 , A. 2 ,... , A.kv.

The lexicographic ordering for this tree is 0 < 1 < 1. 1 < 1. 1. 1 < 1. 1. 2 < 1. 2 < 1. 3 < 2 < 2. 1 <

  1. 2 < 2. 3 < 3 < 3. 1 < 3. 1. 1 < 3. 1. 2

Traversal Algorithms

Preorder Traversal

Let T be an ordered rooted tree with root r. If T consists only of r, then r is the preorder traversal of T. Otherwise, suppose that T 1 , T 2 ,... , Tn are the subtrees at r from left to right in T. The pre- order traversal begins by visiting r. It continues by traversing T 1 in preorder, then T 2 in preorder, and so on, until Tn is traversed in preorder.

Algorithm preorder(T : ordered rooted tree)

1: r = root of T 2: list r 3: for each child c of r from left to right do 4: T (c) = subtree with c as its root 5: preorder(T (c)) 6: end for

Inorder Traversal

Let T be an ordered rooted tree with root r. If T consists only of r, then r is the inorder traversal of T. Otherwise, suppose that T 1 , T 2 ,... , Tn are the subtrees at r from left to right. The inorder traversal begins by traversing T 1 in inorder, then visiting r. It continues by traversing T 2 in inorder, then T 3 in inorder,... , and finally Tn in inorder.

Algorithm inorder(T : ordered rooted tree)

1: r = root of T 2: if r is a leaf then 3: list r 4: else 5: l = first child of r from left to right 6: T (l) = subtree with l as its root 7: inorder(T (l)) 8: list r 9: for each child c of r except for l from left to right do 10: T (c) = subtree with c as its root 11: inorder(T (c)) 12: end for 13: end if

Postorder Traversal

Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T. Otherwise, suppose that T 1 , T 2 ,... , Tn are the subtrees at r from left to right. The postorder traversal begins by traversing T 1 in postorder, then T 2 in postorder,... , then Tn in postorder, and ends by visiting r.

Algorithm postorder(T : ordered rooted tree)

1: r = root of T 2: for each child c of r from left to right do 3: T (c) = subtree with c as its root 4: postorder(T (c)) 5: end for 6: list r

Infix, Prefix, and Postfix Notation

Complicated expressions, such as compound propositions, combinations of sets, and arithmetic expressions can be represented by ordered root trees. For arithmetic expressions:

  • Internal vertices: operations
  • Leaves: variables or numbers

The lexicographic ordering is 0 < 1 < 1. 1 < 1. 2 < 2 < 3.

11.3 pg. 783 # 7

Determine the order in which a preorder traversal visits the vertices of the given ordered rooted tree.

The preorder traversal is: a, b, d, e, f, g, c.

11.3 pg. 783 # 11

Determine the order in which an inorder traversal visits the vertices of the given ordered rooted tree.

The inorder traversal is: d, b, i, e, m, j, n, o, a, f, c, g, k, h, p, l.

11.3 pg. 783 # 13

Determine the order in which a postorder traversal visits the vertices of the given ordered rooted tree.

The postorder traversal is: d, f, g, e, b, c, a

  • c) +− ↑ 3 2 ↑ 2 3/ 6 −
    • +− ↑ 3 2 ↑ 2 3/ 6 −
    • +− ↑ 3 2 ↑ 2 3/6
    • +− ↑ 3 2 ↑
    • +− ↑
      • −
    • +1
  • d) ∗ + 3 + 3 ↑ 3 + - ∗ + 3 + 3 ↑ 3 + - ∗ + 3 + 3 ↑ - ∗ + 3 + - ∗ + - ∗735