Title: Tree Traversal
1Tree Traversal
2Traversal Algorithms
- preorder
- inorder
- postorder
3PreOrder Traversal
4Inorder Traversal
5Postorder Traversal
6In 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
7In 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
8In 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
9In 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
10In 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
11In 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
12In 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
13In 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
14In 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
15In 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
16In 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
17In 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
18In 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
19In 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
20In 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
21In 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
22In 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
23In 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
24In 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
25In 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
26In 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
27In 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
28In 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
29In 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
30In 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
31In 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
32In 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
33In 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
34In 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
35In 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
36In 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
37In 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
38In 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
39In 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
40In 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
41In 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
42In 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
43In 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
44In 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
45In 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
46In 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
47In 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
48In 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
49In 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
50In 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
51In 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
52In 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
53In 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
54In 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
55In 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
56In 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
57In 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
58In 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
59In 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
60In 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
61In 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
62In 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
63In 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
64In 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
65In which order does a postorder traversal visit
the vertices in this ordered rooted tree?
66Infix, 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
67- Express in
- Polish
- infix
- reverse Polish
68Evaluating a prefix expression
69Evaluate postfix expression 7 2 3 - 4 9 e /
70(No Transcript)
71How do you do trees in Java?