Principles of Computing – UFCFA3-30-1 Week-11 Trees Data Structure Instructor : Mazhar H Malik Email : mazhar@gcet.edu.om Global College of Engineering and Technology
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.
Tree Traversal
Traversal Algorithms Depth First Search preorder inorder postorder
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
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 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:
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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:
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:
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:
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:
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:
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:
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Illustrations for Traversals Assume: visiting a node is printing its label Preorder: 1 3 5 4 6 7 8 9 10 11 12 Inorder: 4 5 6 3 1 8 7 9 11 10 12 Postorder: 4 6 5 3 8 11 12 10 9 7 1 1 3 11 9 8 4 6 5 7 12 10
Illustrations for Traversals (Contd.) Assume: visiting a node is printing its data Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30 Inorder: 2 3 6 7 8 10 11 12 14 15 20 22 27 30 Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15 6 15 8 2 3 7 11 10 14 12 20 27 22 30
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