0% found this document useful (0 votes)
13 views

Data 04

The document discusses recursion, pointers, and records as data structures. Recursion is defined as a function that calls itself, and examples like factorials and Fibonacci numbers are provided. Pointer arrays and using pointers to reference elements in arrays are also covered.

Uploaded by

hattie.martin911
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Data 04

The document discusses recursion, pointers, and records as data structures. Recursion is defined as a function that calls itself, and examples like factorials and Fibonacci numbers are provided. Pointer arrays and using pointers to reference elements in arrays are also covered.

Uploaded by

hattie.martin911
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

CSE-213

(Data Structure)
Lecture on
Recursion, Pointers and Records

1
Recursion

2
Recursion
• Recursion in data structure is when a function calls itself indirectly
or directly, and the function calling itself is known as a recursive
function.
• It's generally used when the answer to a larger issue could be
depicted in terms of smaller problems.

3
Recursion: Function
 A function is said to be Recursively defined if the function definition
refers to itself.
 A Recursive Function must have the following two properties:
 There must be certain arguments, called BASE VALUE, for which
the function does not refer to itself.
 Each time the function does refer to itself, the argument of the
function must be closer to a BASE VALUE.
 A Recursive Function with two properties is also said to be well-
defined.

4
Recursion: Factorial Function
 In some problems, it may be natural to define the problem in terms
of the problem itself.
 Recursion is useful for problems that can be represented by a
SIMPLER VERSION of the same problem.
 Example: the factorial function
6! = 6 * 5 * 4 * 3 * 2 * 1
We could write:
6! = 6 * 5!

5
Recursion: Factorial Function
 In general, we can express the factorial function as follows:
n! = n * (n-1)!
Is this correct? Well… almost.
 The factorial function is ONLY DEFINED for positive integers. So we
should be a bit more precise:
i) n! = 1 (if n is equal to 1)
ii) n! = n * (n-1)! (if n is larger than 1)

 Observe that, this definition of n! is recursive, since it refers to


itself when it uses (n-1)! , However,
 i)the value of n! is explicitly given when n=0 (BASE VALUE)
 ii)the value of n! for arbitrary n is defined in terms of a smaller
6
value of n which is closer to the BASE VALUE
Recursion: Factorial Function
EXAMPLE: Let’s calculate 3! Using the recursive definition.
(1) 3! = 3 . 2!
(2) 2! = 2 . 1!
(3) 1! = 1 . 0!
(4) 0! = 1 (BASE VALUE)
(5) 1! = 1 . 1 = 1
(6) 2! = 2 . 1 = 2
(7) 3! = 3 . 2 = 6
 Observe that we back track in the reverse order of the original
postponded evaluations.
 Recall that this type of postponed processing tends itself to the
use of STACKS. 7
Recursion: Function

Assume the number typed is 3, that is, numb=3.


fac(3) :
3 <= 1 ? No.
fac(3) = 3 * fac(2)
fac(2) :
2 <= 1 ? No.
fac(2) = 2 * fac(1) i) n! = 1 (if n is equal to 1)
ii) n! = n * (n-1)! (if n is larger than 1)
fac(1) :
1 <= 1 ? Yes.
return 1 int fac(int numb){
fac(2) = 2 * 1 = 2 if(numb<=1)
return fac(2) return 1;
fac(3) = 3 * 2 = 6 else
return fac(3) return numb * fac(numb-1);
}

fac(3) has the value 6


8
Recursion: Function
For certain problems (such as the factorial function), a
recursive solution often leads to short and elegant code.
Compare the recursive solution with the iterative solution:

Recursive solution Iterative solution


int fac(int numb){
int fac(int numb){
if(numb<=1) int product=1;
return 1; while(numb>1){
else product *= numb;
return numb*fac(numb-1);
numb--;
}
}
return product;
} 9
Iteration Vs Recursion

Two approaches to writing repetitive algorithms:


1. Iteration
2. Recursion

10
Recursion: Fibbonacc

11
Recursion: Fibbonacci

12
Recursion: Fibbonacc
// Calculate Fibonacci numbers using recursive function.
// A very inefficient way, but illustrates recursion well
int fib(int number)
Recursive definition:
{ F(0) = 0;
if (number == 0) return 0; F(1) = 1;
F(number)=F(number-1)+ F(number-2);
if (number == 1) return 1;
return (fib(number-1) + fib(number-2));
}

13
Pointers
Let DATA be any array. A variable P is called a pointer if P “points” to an
element in DATA, i.e., if P contains the address of an element in DATA.
Pointer Arrays
An array PTR is called a pointer array if each element of PTR is a pointer
Pointer and Pointer array are used to facilitate the processing the
information in DATA

How the membership list can be stored in memory keeping track of


the different groups? 14
POINTER ARRAYS
Pointer arrays is introduced in the last two space-efficient data
structure.

The pointer array contains the


locations of the…..
 Different groups, or
 First element in the different
groups.
 GROUP[L] and GROUP[L+1]-1
contain respectively, the first
and last element in group L.

15
POINTER ARRAYS: Example

Suppose L=3
GROUP[L]=GROUP[3]= 14= Davis (1st
Element of grp 3)
GROUP[L+1]-1=GROUP[3+1]-1=16-1=15=
Segel (last element of grp 3)

16
POINTER ARRAYS: Extended
 Here unused memory cells are
indicated by the shading.
 Observe that now there are some
empty cells between the groups.
 Accordingly, a new element may be
inserted in a new group without
necessarily moving the elements in
any other group.
 Using the data structure, one requires
an array NUMB which gives the
number of elements in each group.
 Observe that GROUP[K+1]-GROUP[K]
is the total number of space available
for group K. Hence

FREE[K]=GROUP[K+1]-GROUP[K]-NUMB[K]
Gives the number of empty cells following GROUP K 17
POINTER ARRAYS: Extended, Example
Suppose, we want to print only the
number of FREE cells of GROUP 2. Then

FREE[K]=GROUP[K+1]-GROUP[K]-NUMB[K]
FREE[2]=GROUP[2+1]-GROUP[2]-NUMB[2]
= 19-7-9
=3

For GROUP 4?
Try now

18
RECORDS
 A record is a collection of related data items, each of which is called
a field or attribute, and
 a file is a collection or similar records.
 Although, a record is a collection of data items, it differs from a linear
array in the following ways…............
 A record may be a collection of nonhomogeneous data;
 The data items in a record are indexed by attribute names, so
there may not be a natural ordering of its elements.

19
RECORDS: Structure Example
1. Newborn
2. Name Under the relationship of group item to sub- item, the
2. Sex data items in a record form a hierarchical structure
2. Birthday
3. Month
which can be described by mean of “Level” numbers
3. Day
3. Year
2. Father
3. Name
3. Age
2. Mother
3. Name
3. Age

Name Sex Birthday Father Mother


Name Age Name Age

20
Indexing Items in a Record
 Suppose we want to access some data item in a record.
 We can not simply write the data name of the item since the same
may appear in different places in the record. For example…..

1. Newborn  In order to specify a particular item,


2. Name
2. Sex
 we may have to qualify the name by using
2. Birthday appropriate group item names in the
3. Month structure.
3. Day
3. Year
 This qualification is indicated by using decimal
2. Father points (periods) to separate group items from
3. Name subitems.
3. Age
2. Mother
 Example: Newborn.Father.Age or Father.Age
3. Name
3. Age
Fully qualified
reference
21
Indexing Items in a Record
1. Newborn 1. Newborn(20)
2. Name 2. Name Newborn is
2. Sex 2. Sex defined to be a
2. Birthday 2. Birthday file with 20
3. Month 3. Month records
3. Day 3. Day
3. Year 3. Year
2. Father 2. Father
3. Name 3. Name
3. Age 3. Age
2. Mother 2. Mother
3. Name 3. Name
3. Age 3. Age

 The Name of the sixth newborn to be referenced by writing …....


Newborn. Name[6]
 The age of the father of the 6th newborn may be referenced by writing…..
Newborn.Father.Age[6] 22
Representation of RECORDS in memory
Since records may contain nonhomogeneous data, the element of a
record can not be stored in an array.

See Example: 4.18, 4.20, 4.21

23
MATRICES and SPARSE MATRICES

Teach Yourself with example

Try to understand the SOLVED Problems


24

You might also like