Presentation is loading. Please wait.

Presentation is loading. Please wait.

Principles of Computing – UFCFA3-30-1

Similar presentations


Presentation on theme: "Principles of Computing – UFCFA3-30-1"— Presentation transcript:

1 Principles of Computing – UFCFA3-30-1
Week-11 Trees Data Structure Instructor : Mazhar H Malik Global College of Engineering and Technology

2 Trees A tree data structure can be defined recursively (locally) as a collection of nodes (starting at a root node), where each node is a data structure consisting of a value, together with a list of references to nodes (the "children"), with the constraints that no reference is duplicated, and none points to the root.

3 Tree Traversal

4

5 Traversal Algorithms Depth First Search preorder inorder postorder

6 Preoder, Inorder, Postorder
In Preorder, the root is visited before (pre) the subtrees traversals In Inorder, the root is visited in-between left and right subtree traversal is visited after (pre) Preorder Traversal: Visit the root Traverse left subtree Traverse right subtree Inorder Traversal: Traverse left subtree Visit the root Traverse right subtree Postorder Traversal: Traverse left subtree Traverse right subtree Visit the root

7 PreOrder Traversal

8 Inorder Traversal

9 Postorder Traversal

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:

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

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 for each of {b, c, d} output: a

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 for each of {b, c, d} output: a

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

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 for each of {e, f} output: a b

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 for each of {e, f} output: a b

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

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 for each of {j, k} output: a b e

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 for each of {j, k} output: a b e

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

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 for each of {} output: a b e j

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 for each of {j, k} output: a b e j

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

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 for each of {n, o, p} output: a b e j k

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 for each of {n, o, p} output: a b e j k

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

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 for each of {} output: a b e j k n

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 for each of {n, o, p} output: a b e j k n

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

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 for each of {} output: a b e j k n o

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 for each of {n, o, p} output: a b e j k n o

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

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 for each of {} output: a b e j k n o p

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 for each of {n, o, p} output: a b e j k n o p

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 for each of {j, k} output: a b e j k n o p

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 for each of {e, f} output: a b e j k n o p

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

38 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 for each of {} output: a b e j k n o p f

39 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 for each of {e, f} output: a b e j k n o p f

40 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 for each of {b, c, d} output: a b e j k n o p f

41 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

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:

43 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = ? s = {} 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:

44 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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:

45 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = ? s = {} output:

46 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} output:

47 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = ? s = {} output:

48 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {} output: j

49 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {} r = j, l = ? s = {} output: j

50 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r =e, l = j s = {} output: j e

51 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} output: j e

52 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} r = k, l = ? s = {} output: j e

53 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} output: j e

54 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} r = n, l = ? s = {} output: j e n

55 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {} output: j e n k

56 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} output: j e n k

57 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} r = o, l = ? s = {} output: j e n k o

58 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} output: j e n k o

59 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} r = k, l = ? s = {} output: j e n k o p

60 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} r = k, l = n s = {o,p} output: j e n k o p

61 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} r = e, l = j s = {k} output: j e n k o p

62 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {} output: j e n k o p b

63 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {f} output: j e n k o p b

64 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {f} r = f, l = e s = {} output: j e n k o p b f

65 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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 r = b, l = e s = {f} output: j e n k o p b f

66 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {} 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

67 In which order does a inorder traversal visit the vertices in this ordered rooted tree?
call stack r = a, l = b s = {c,d} 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

68 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

69 Illustrations for Traversals
Assume: visiting a node is printing its label Preorder: Inorder: Postorder: 1 3 11 9 8 4 6 5 7 12 10

70 Illustrations for Traversals (Contd.)
Assume: visiting a node is printing its data Preorder: Inorder: Postorder: 6 15 8 2 3 7 11 10 14 12 20 27 22 30

71 a j b k c g m d l i h f e preorder: a j k m l b c g i h d f e
inorder: m k l j a b i g h c f d e postorder: m l k j i h g f e d c b a


Download ppt "Principles of Computing – UFCFA3-30-1"

Similar presentations


Ads by Google