Tree Traversal - PowerPoint PPT Presentation

1 / 71
About This Presentation
Title:

Tree Traversal

Description:

... which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) ... preorder(T(c)) end. output: ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 72
Provided by: dmcl8
Category:

less

Transcript and Presenter's Notes

Title: Tree Traversal


1
Tree Traversal
2
Traversal Algorithms
  • preorder
  • inorder
  • postorder

3
PreOrder Traversal
4
Inorder Traversal
5
Postorder Traversal
6
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
7
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
8
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
for each of b, c, d
output a
9
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
for each of b, c, d
output a
10
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
11
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
for each of e, f
output a b
12
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
for each of e, f
output a b
13
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
14
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
for each of j, k
output a b e
15
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
for each of j, k
output a b e
16
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
17
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
for each of
output a b e j
18
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
for each of j, k
output a b e j
19
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
20
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
for each of n, o, p
output a b e j k
21
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
for each of n, o, p
output a b e j k
22
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
23
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
for each of
output a b e j k n
24
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
for each of n, o, p
output a b e j k n
25
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
26
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
for each of
output a b e j k n o
27
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
for each of n, o, p
output a b e j k n o
28
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
29
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
for each of
output a b e j k n o p
30
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
for each of n, o, p
output a b e j k n o p
31
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
for each of j, k
output a b e j k n o p
32
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
for each of e, f
output a b e j k n o p
33
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
34
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
for each of
output a b e j k n o p f
35
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
for each of e, f
output a b e j k n o p f
36
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
for each of b, c, d
output a b e j k n o p f
37
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
38
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
39
In 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
40
In 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
41
In 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
42
In 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
43
In 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
44
In 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
45
In 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
46
In 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
47
In 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
48
In 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
49
In 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
50
In 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
51
In 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
52
In 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
53
In 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
54
In 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
55
In 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
56
In 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
57
In 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
58
In 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
59
In 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
60
In 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
61
In 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
62
In 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
63
In 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
64
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
65
In which order does a postorder traversal visit
the vertices in this ordered rooted tree?
66
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

67
  • Express in
  • Polish
  • infix
  • reverse Polish

68
Evaluating a prefix expression
69
Evaluate postfix expression 7 2 3 - 4 9 e /
70
(No Transcript)
71
How do you do trees in Java?
Write a Comment
User Comments (0)
About PowerShow.com