View
222
Download
0
Category
Preview:
Citation preview
7/29/2019 Tree Travers Al 06
1/71
Tree Traversal
7/29/2019 Tree Travers Al 06
2/71
Traversal Algorithms
preorder
inorder
postorder
7/29/2019 Tree Travers Al 06
3/71
PreOrder Traversal
7/29/2019 Tree Travers Al 06
4/71
Inorder Traversal
7/29/2019 Tree Travers Al 06
5/71
Postorder Traversal
7/29/2019 Tree Travers Al 06
6/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output:
7/29/2019 Tree Travers Al 06
7/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a
7/29/2019 Tree Travers Al 06
8/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a
for each of {b, c, d}
7/29/2019 Tree Travers Al 06
9/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a
for each of {b, c, d}
7/29/2019 Tree Travers Al 06
10/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b
7/29/2019 Tree Travers Al 06
11/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b
for each of {e, f}
7/29/2019 Tree Travers Al 06
12/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b
for each of {e, f}
7/29/2019 Tree Travers Al 06
13/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e
7/29/2019 Tree Travers Al 06
14/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e
for each of {j, k}
7/29/2019 Tree Travers Al 06
15/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e
for each of {j, k}
7/29/2019 Tree Travers Al 06
16/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j
7/29/2019 Tree Travers Al 06
17/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j
for each of {}
7/29/2019 Tree Travers Al 06
18/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j
for each of {j, k}
7/29/2019 Tree Travers Al 06
19/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j k
7/29/2019 Tree Travers Al 06
20/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j k
for each of {n, o, p}
7/29/2019 Tree Travers Al 06
21/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j k
for each of {n, o, p}
7/29/2019 Tree Travers Al 06
22/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j k n
7/29/2019 Tree Travers Al 06
23/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j k n
for each of {}
7/29/2019 Tree Travers Al 06
24/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j k n
for each of {n, o, p}
7/29/2019 Tree Travers Al 06
25/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j k n o
7/29/2019 Tree Travers Al 06
26/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j k n o
for each of {}
7/29/2019 Tree Travers Al 06
27/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom left to right
begin
T(c) := subtree with c as its root
preorder(T(c))
end
output: a b e j k n o
for each of {n, o, p}
7/29/2019 Tree Travers Al 06
28/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
7/29/2019 Tree Travers Al 06
29/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
for each of {}
7/29/2019 Tree Travers Al 06
30/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
for each of {n, o, p}
7/29/2019 Tree Travers Al 06
31/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
for each of {j, k}
7/29/2019 Tree Travers Al 06
32/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
for each of {e, f}
7/29/2019 Tree Travers Al 06
33/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
7/29/2019 Tree Travers Al 06
34/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
for each of {}
7/29/2019 Tree Travers Al 06
35/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
for each of {e, f}
7/29/2019 Tree Travers Al 06
36/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
for each of {b, c, d}
7/29/2019 Tree Travers Al 06
37/71
In which order does a preorder traversal visit
the vertices in this ordered rooted tree?
procedurepreorder(T: ordered rooted tree)
r:= root ofT
list rfor each child c ofrfrom 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
7/29/2019 Tree Travers Al 06
38/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output:
7/29/2019 Tree Travers Al 06
39/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output:
call
stack
r = a, l = ?
s = {}
7/29/2019 Tree Travers Al 06
40/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output:
r = a, l = b
s = {}
call
stack
7/29/2019 Tree Travers Al 06
41/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output:
r = a, l = b
s = {}
r = b, l = ?
s = {}
call
stack
7/29/2019 Tree Travers Al 06
42/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output:
r = a, l = b
s = {}
r = b, l = e
s = {}
call
stack
7/29/2019 Tree Travers Al 06
43/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output:
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l = ?
s = {}
call
stack
7/29/2019 Tree Travers Al 06
44/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {}
call
stack
7/29/2019 Tree Travers Al 06
45/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {}
r = j, l = ?
s = {}
call
stack
7/29/2019 Tree Travers Al 06
46/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e
r = a, l = b
s = {}
r = b, l = e
s = {}
r =e, l =j
s = {}
call
stack
7/29/2019 Tree Travers Al 06
47/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
call
stack
7/29/2019 Tree Travers Al 06
48/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
r = k, l = ?
s = {}
call
stack
7/29/2019 Tree Travers Al 06
49/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
r = k, l = n
s = {}
call
stack
7/29/2019 Tree Travers Al 06
50/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e n
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
r = k, l = n
s = {}
call
stack
r = n, l = ?
s = {}
7/29/2019 Tree Travers Al 06
51/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthenlist r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e n k
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
r = k, l = n
s = {}
call
stack
7/29/2019 Tree Travers Al 06
52/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e n k
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
r = k, l = n
s = {o,p}
call
stack
7/29/2019 Tree Travers Al 06
53/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthenlist r
else
beginl:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e n k o
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
r = k, l = n
s = {o,p}
call
stack
r = o, l = ?
s = {}
7/29/2019 Tree Travers Al 06
54/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e n k o
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
r = k, l = n
s = {o,p}
call
stack
ll
7/29/2019 Tree Travers Al 06
55/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e n k o p
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
r = k, l = n
s = {o,p}
call
stack
r = k, l = ?
s = {}
ll
7/29/2019 Tree Travers Al 06
56/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e n k o p
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
r = k, l = n
s = {o,p}
call
stack
ll
7/29/2019 Tree Travers Al 06
57/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its root
preorder(T(c))
end
output: j e n k o p
r = a, l = b
s = {}
r = b, l = e
s = {}
r = e, l =j
s = {k}
call
stack
hi h d d i d l i i
ll
7/29/2019 Tree Travers Al 06
58/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthenlist r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept 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
r = a, l = b
s = {}
r = b, l = e
s = {}
call
stack
I hi h d d i d l i i
ll
7/29/2019 Tree Travers Al 06
59/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept 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
r = a, l = b
s = {}
r = b, l = e
s = {f}
call
stack
I hi h d d i d l i i
ll
7/29/2019 Tree Travers Al 06
60/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthenlist r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept 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
r = a, l = b
s = {}
r = b, l = e
s = {f}
r = f, l = e
s = {}
call
stack
I hi h d d i d l i i
ll
7/29/2019 Tree Travers Al 06
61/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its rootpreorder(T(c))
end
output: j e n k o p b f
r = a, l = b
s = {}
r = b, l = e
s = {f}
call
stack
I hi h d d i d t l i it
call
7/29/2019 Tree Travers Al 06
62/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its rootpreorder(T(c))
end
output: j e n k o p b f
r = a, l = b
s = {}
call
stack
I hi h d d i d t l i it
call
7/29/2019 Tree Travers Al 06
63/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its rootpreorder(T(c))
end
output: j e n k o p b f
r = a, l = b
s = {c,d}
call
stack
I hi h d d i d t l i it
7/29/2019 Tree Travers Al 06
64/71
In which order does a inorder traversal visit
the vertices in this ordered rooted tree?
procedureinorder(T: ordered rooted tree)
r:= root ofT
ifr is a leafthen list r
else
begin
l:= first child ofrfrom left to right
T(l) := subtree with l as its root
inorder(T(l))
list r
for each child c ofrexcept for l left to right
T(c) := subtree with c as its rootpreorder(T(c))
end
output: j e n k o p b f a c l g m d h i
In which order does a postorder traversal visit
7/29/2019 Tree Travers Al 06
65/71
In which order does a postorder traversal visit
the vertices in this ordered rooted tree?
7/29/2019 Tree Travers Al 06
66/71
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
7/29/2019 Tree Travers Al 06
67/71
Express in:a. Polish
b. infix
c. reverse Polish
7/29/2019 Tree Travers Al 06
68/71
Evaluating
a prefixexpression
7/29/2019 Tree Travers Al 06
69/71
Evaluate postfix expression 7 2 3 * - 4 ^ 9 e / +
7/29/2019 Tree Travers Al 06
70/71
7/29/2019 Tree Travers Al 06
71/71
How do you do trees in Java?
Recommended