Principles of Computing UFCFA 3 30 1 Week10
- Slides: 71
Principles of Computing – UFCFA 3 -30 -1 Week-10 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 • In Preorder, the root is visited after (pre) the subtrees traversals Preorder Traversal: 1. Visit the root 2. Traverse left subtree 3. Traverse right subtree Inorder Traversal: 1. Traverse left subtree 2. Visit the root 3. Traverse right subtree Postorder Traversal: 1. Traverse left subtree 2. Traverse right subtree 3. Visit the root 6
Pre. Order 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 for childeach c of roffrom to right {b, left c, d} 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 for childeach c of roffrom to right {b, left c, d} 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 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 of r from leftf}to right for ceach of {e, 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 of r from leftf}to right for ceach of {e, 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 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 of r from for ceach of {j, left k}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 of r from for ceach of {j, left k}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 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 r from left to right forc of each of {} 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 of r from for ceach of {j, left k}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 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 for childeach c of roffrom to right {n, left o, p} 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 for childeach c of roffrom to right {n, left o, p} 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 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 r from left to right forc of each of {} 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 for childeach c of roffrom to right {n, left o, p} 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 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 r from left to right forc of each of {} 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 for childeach c of roffrom to right {n, left o, p} 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 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 r from left to right forc of each of {} 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 for childeach c of roffrom to right {n, left o, p} 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 of r from for ceach of {j, left k}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 of r from leftf}to right for ceach of {e, 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 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 r from left to right forc of each of {} 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 of r from leftf}to right for ceach of {e, 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 for childeach c of roffrom to right {b, left c, d} 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 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 = b, l = r : = root of T if r is a leaf then list r s = {} 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 = b, l = r : = root of T if r is a leaf then list r s = {} 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: 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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = ? begin s = {} 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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {} l: = first child of r from left to right T(l) : = subtree with l as its root r = j, l = ? inorder(T(l)) s = {} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r =e, l = j begin s = {} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} l: = first child of r from left to right T(l) : = subtree with l as its root r = k, l = ? inorder(T(l)) s = {} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} l: = first child of r from left to right T(l) : = subtree with l as its root r = k, l = n inorder(T(l)) s = {} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} l: = first child of r from left to right T(l) : = subtree with l as its root r = k, l = n inorder(T(l)) s = {} list r for each child c of r except for l left rto=right n, l = ? T(c) : = subtree with c as its root s = {} preorder(T(c)) end 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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} l: = first child of r from left to right T(l) : = subtree with l as its root r = k, l = n inorder(T(l)) s = {} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} l: = first child of r from left to right T(l) : = subtree with l as its root r = k, l = n inorder(T(l)) s = {o, p} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} l: = first child of r from left to right T(l) : = subtree with l as its root r = k, l = n inorder(T(l)) s = {o, p} list r for each child c of r except for l left rto=right o, l = ? T(c) : = subtree with c as its root s = {} preorder(T(c)) end 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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} l: = first child of r from left to right T(l) : = subtree with l as its root r = k, l = n inorder(T(l)) s = {o, p} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} l: = first child of r from left to right T(l) : = subtree with l as its root r = k, l = n inorder(T(l)) s = {o, p} list r for each child c of r except for l left rto=right k, l = ? T(c) : = subtree with c as its root s = {} preorder(T(c)) end 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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} l: = first child of r from left to right T(l) : = subtree with l as its root r = k, l = n inorder(T(l)) s = {o, p} 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
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 = b, l = e r : = root of T if r is a leaf then list r s = {} else r = e, l = j begin s = {k} 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
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 = b, l = r : = root of T if r is a leaf then list r s = {} 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 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 = b, l = r : = root of T if r is a leaf then list r s = {f} 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 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 = b, l = e r : = root of T if r is a leaf then list r s = {f} else r = f, l = e begin s = {} 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 = {} procedure inorder(T: ordered rooted tree) r = b, l = r : = root of T if r is a leaf then list r s = {f} 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 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 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 4 • 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 7 5 8 9 10 6 11 12 69
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 15 20 8 6 10 12 3 27 11 2 7 22 30 14 70
a j b k m c g l i d h f 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 e
- Ufcfa
- Ufcfa
- Conventional computing and intelligent computing
- Principles for end user computing controls
- Principles of distributed computing
- Cs 498 cloud computing applications
- Types of computing device
- Pervasive and mobile computing
- Essential computing
- Robust and error-free geometric computing
- Ubiquitous computing nedir
- Client server architecture model
- Gsm architecture
- Apa itu end user computing
- Ubiquitous computing
- Virtualization of clusters in cloud computing
- Ready made environment in cloud computing
- Harvard research computing
- Parallel computing
- The purpose of computation is insight not numbers
- Web application architecture example
- Design considerations for mobile computing
- What does denary mean in computing
- Introduction to parallel computing ananth grama ppt
- Introduction to cloud computing
- Motivation of cloud computing
- Grid and cloud computing definition
- Approximate computing
- Conventional computer system
- Computing table 1
- Language
- Microprocessor in hci
- Collaborating via social networks and groupware
- Bina bhaskar
- School of computing and informatics
- Approximate computing
- Cloud computing layers
- Mac issues in mobile computing
- Quantum computing and the entanglement frontier
- Issr emilia cloud
- Network interconnection studies
- It 101 - introduction to computing
- Cloud computing reference model
- Xen cloud computing
- Computing applications building
- High performance computing modernization program
- Hyperdimensional computing
- Design techniques computing
- Mhpcc
- Mobile computing platforms
- Contoh penerapan green computing di perusahaan
- Vector clock in distributed system
- Distributed, parallel, and cluster computing
- End user computing governance
- Grid and cloud computing lmu
- Syarat cloud computing
- History of grid computing
- Total cost of ownership in cloud computing
- Cern grid computing
- Characteristics of mobile computing
- More personal computing
- Gustafson’s law
- Wtls in mobile computing
- Pam lahoud
- Above the clouds: a berkeley view of cloud computing
- Security in computing pfleeger
- Basic period of computer
- Cs 267
- Crowd computing projects
- Computational radiology
- Paul watson newcastle
- Matlab spmd vs parfor