Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree Traversal. Traversal Algorithms preorder inorder postorder.

Similar presentations


Presentation on theme: "Tree Traversal. Traversal Algorithms preorder inorder postorder."— Presentation transcript:

1 Tree Traversal

2 Traversal Algorithms preorder inorder postorder

3 PreOrder Traversal

4 Inorder Traversal

5 Postorder Traversal

6 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output:

7 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a

8 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a for each of {b, c, d}

9 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a for each of {b, c, d}

10 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b

11 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b for each of {e, f}

12 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b for each of {e, f}

13 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e

14 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e for each of {j, k}

15 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e for each of {j, k}

16 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j

17 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j for each of {}

18 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j for each of {j, k}

19 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k

20 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k for each of {n, o, p}

21 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k for each of {n, o, p}

22 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n

23 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n for each of {}

24 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n for each of {n, o, p}

25 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o

26 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o for each of {}

27 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o for each of {n, o, p}

28 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p

29 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p for each of {}

30 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p for each of {n, o, p}

31 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p for each of {j, k}

32 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p for each of {e, f}

33 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p f

34 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p f for each of {}

35 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p f for each of {e, f}

36 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p f for each of {b, c, d}

37 In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedure preorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end output: a b e j k n o p f c d g l m h i

38 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output:

39 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: call stack r = a, l = ? s = {}

40 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: r = a, l = b s = {} call stack

41 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: r = a, l = b s = {} r = b, l = ? s = {} call stack

42 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: r = a, l = b s = {} r = b, l = e s = {} call stack

43 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: r = a, l = b s = {} r = b, l = e s = {} r = e, l = ? s = {} call stack

44 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {} call stack

45 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {} r = j, l = ? s = {} call stack

46 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e r = a, l = b s = {} r = b, l = e s = {} r =e, l = j s = {} call stack

47 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} call stack

48 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = ? s = {} call stack

49 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} call stack

50 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n 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 = {}

51 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} call stack

52 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k 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

53 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o 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 = {}

54 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o 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

55 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p 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 = {}

56 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p 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

57 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p r = a, l = b s = {} r = b, l = e s = {} r = e, l = j s = {k} call stack

58 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b r = a, l = b s = {} r = b, l = e s = {} call stack

59 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b r = a, l = b s = {} r = b, l = e s = {f} call stack

60 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b f r = a, l = b s = {} r = b, l = e s = {f} r = f, l = e s = {} call stack

61 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b f r = a, l = b s = {} r = b, l = e s = {f} call stack

62 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b f r = a, l = b s = {} call stack

63 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b f r = a, l = b s = {c,d} call stack

64 In which order does a inorder traversal visit the vertices in this ordered rooted tree? procedure inorder(T: ordered rooted tree) r := root of T if r is a leaf then list r else begin l:= first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list r for each child c of r except for l left to right T(c) := subtree with c as its root preorder(T(c)) end output: j e n k o p b f a c l g m d h i

65 In which order does a postorder traversal visit the vertices in this ordered rooted tree?

66 Infix, Prefix, and Postfix Notation represent complicated expressions using an ordered rooted tree (typically binary) Algebraic expressions preorder – Polish notation inorder – infix notation postorder – reverse Polish notation

67 Express in: a.Polish b.infix c.reverse Polish

68 Evaluating a prefix expression

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

70

71 How do you do trees in Java?


Download ppt "Tree Traversal. Traversal Algorithms preorder inorder postorder."

Similar presentations


Ads by Google