71
Tree Traversal

Tree Traversal. Traversal Algorithms preorder inorder postorder

  • View
    246

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Tree Traversal. Traversal Algorithms preorder inorder postorder

Tree TraversalTree Traversal

Page 2: Tree Traversal. Traversal Algorithms preorder inorder postorder

Traversal AlgorithmsTraversal Algorithms

• preorder• inorder• postorder

Page 3: Tree Traversal. Traversal Algorithms preorder inorder postorder

PreOrder TraversalPreOrder Traversal

Page 4: Tree Traversal. Traversal Algorithms preorder inorder postorder

Inorder TraversalInorder Traversal

Page 5: Tree Traversal. Traversal Algorithms preorder inorder postorder

Postorder TraversalPostorder Traversal

Page 6: Tree Traversal. Traversal Algorithms preorder inorder postorder

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Express in:a. Polishb. infixc. reverse Polish

Page 68: Tree Traversal. Traversal Algorithms preorder inorder postorder

Evaluating a prefix

expression

Page 69: Tree Traversal. Traversal Algorithms preorder inorder postorder

Evaluate postfix expression 7 2 3 * - 4 ^ 9 e / +

Page 70: Tree Traversal. Traversal Algorithms preorder inorder postorder
Page 71: Tree Traversal. Traversal Algorithms preorder inorder postorder

How do you do trees in Java?How do you do trees in Java?