Data 04
Data 04
(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)
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
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
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…..
23
MATRICES and SPARSE MATRICES