Tree Traversal. Traversal Algorithms preorder inorder postorder.
-
date post
22-Dec-2015 -
Category
Documents
-
view
234 -
download
3
Transcript of Tree Traversal. Traversal Algorithms preorder inorder postorder.
![Page 1: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/1.jpg)
Tree TraversalTree Traversal
![Page 2: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/2.jpg)
Traversal AlgorithmsTraversal Algorithms
• preorder• inorder• postorder
![Page 3: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/3.jpg)
PreOrder TraversalPreOrder Traversal
![Page 4: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/4.jpg)
Inorder TraversalInorder Traversal
![Page 5: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/5.jpg)
Postorder TraversalPostorder Traversal
![Page 6: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/6.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output:
![Page 7: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/7.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a
![Page 8: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/8.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a
for each of {b, c, d}
![Page 9: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/9.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a
for each of {b, c, d}
![Page 10: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/10.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b
![Page 11: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/11.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b
for each of {e, f}
![Page 12: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/12.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b
for each of {e, f}
![Page 13: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/13.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e
![Page 14: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/14.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e
for each of {j, k}
![Page 15: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/15.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e
for each of {j, k}
![Page 16: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/16.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j
![Page 17: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/17.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j
for each of {}
![Page 18: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/18.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j
for each of {j, k}
![Page 19: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/19.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k
![Page 20: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/20.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k
for each of {n, o, p}
![Page 21: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/21.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k
for each of {n, o, p}
![Page 22: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/22.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k n
![Page 23: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/23.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k n
for each of {}
![Page 24: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/24.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin 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}
![Page 25: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/25.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k n o
![Page 26: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/26.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k n o
for each of {}
![Page 27: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/27.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin 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}
![Page 28: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/28.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k n o p
![Page 29: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/29.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k n o p
for each of {}
![Page 30: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/30.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin 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}
![Page 31: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/31.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin 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}
![Page 32: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/32.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin 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}
![Page 33: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/33.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin T(c) := subtree with c as its root preorder(T(c))end
output: a b e j k n o p f
![Page 34: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/34.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin 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 {}
![Page 35: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/35.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin 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}
![Page 36: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/36.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin 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}
![Page 37: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/37.jpg)
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
In which order does a preorder traversal visit the vertices in this ordered rooted tree?
procedure preorder(T: ordered rooted tree)r := root of Tlist rfor each child c of r from left to rightbegin 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
![Page 38: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/38.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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:
![Page 39: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/39.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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:
callstack
r = a, l = ?s = {}
![Page 40: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/40.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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:
r = a, l = bs = {}
callstack
![Page 41: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/41.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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:
r = a, l = bs = {}
r = b, l = ?s = {}
callstack
![Page 42: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/42.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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:
r = a, l = bs = {}
r = b, l = es = {}
callstack
![Page 43: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/43.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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:
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = ?s = {}
callstack
![Page 44: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/44.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {}
callstack
![Page 45: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/45.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {}
r = j, l = ?s = {}
callstack
![Page 46: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/46.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r =e, l = js = {}
callstack
![Page 47: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/47.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
callstack
![Page 48: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/48.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
r = k, l = ?s = {}
callstack
![Page 49: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/49.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
r = k, l = ns = {}
callstack
![Page 50: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/50.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
r = k, l = ns = {}
callstack
r = n, l = ?s = {}
![Page 51: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/51.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
r = k, l = ns = {}
callstack
![Page 52: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/52.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
r = k, l = ns = {o,p}
callstack
![Page 53: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/53.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
r = k, l = ns = {o,p}
callstack
r = o, l = ?s = {}
![Page 54: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/54.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
r = k, l = ns = {o,p}
callstack
![Page 55: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/55.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
r = k, l = ns = {o,p}
callstack
r = k, l = ?s = {}
![Page 56: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/56.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
r = k, l = ns = {o,p}
callstack
![Page 57: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/57.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
r = e, l = js = {k}
callstack
![Page 58: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/58.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {}
callstack
![Page 59: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/59.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {f}
callstack
![Page 60: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/60.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {f}
r = f, l = es = {}
callstack
![Page 61: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/61.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
r = b, l = es = {f}
callstack
![Page 62: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/62.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {}
callstack
![Page 63: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/63.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
r = a, l = bs = {c,d}
callstack
![Page 64: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/64.jpg)
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
In which order does a inorder traversal visit the vertices in this ordered rooted tree?
procedure inorder(T: ordered rooted tree)r := root of Tif r is a leaf then list relsebegin 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
![Page 65: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/65.jpg)
In which order does a postorder traversal visit the vertices in this ordered rooted tree?
In which order does a postorder traversal visit the vertices in this ordered rooted tree?
![Page 66: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/66.jpg)
Infix, Prefix, and Postfix NotationInfix, 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
![Page 67: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/67.jpg)
Express in:a. Polishb. infixc. reverse Polish
![Page 68: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/68.jpg)
Evaluating a prefix
expression
![Page 69: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/69.jpg)
Evaluate postfix expression 7 2 3 * - 4 ^ 9 e / +
![Page 70: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/70.jpg)
![Page 71: Tree Traversal. Traversal Algorithms preorder inorder postorder.](https://reader034.fdocuments.in/reader034/viewer/2022052223/56649d785503460f94a5b487/html5/thumbnails/71.jpg)
How do you do trees in Java?How do you do trees in Java?