1 / 71

Tree Traversal

Tree Traversal. Traversal Algorithms. preorder inorder postorder. PreOrder Traversal. Inorder Traversal. Postorder Traversal. 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

nura
Download Presentation

Tree Traversal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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? procedurepreorder(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? procedurepreorder(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? procedurepreorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {b, c, d} output: a

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

  10. In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedurepreorder(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? procedurepreorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {e, f} output: a b

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

  13. In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedurepreorder(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? procedurepreorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {j, k} output: a b e

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

  16. In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedurepreorder(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? procedurepreorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j

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

  19. In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedurepreorder(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? procedurepreorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {n, o, p} output: a b e j k

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

  22. In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedurepreorder(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? procedurepreorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j k n

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

  25. In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedurepreorder(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? procedurepreorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j k n o

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

  28. In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedurepreorder(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? procedurepreorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j k n o p

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

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

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

  33. In which order does a preorder traversal visit the vertices in this ordered rooted tree? procedurepreorder(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? procedurepreorder(T: ordered rooted tree) r := root of T list r for each child c of r from left to right begin T(c) := subtree with c as its root preorder(T(c)) end for each of {} output: a b e j k n o p f

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related