Download - WHAT THE COURSE IS ABOUT
![Page 1: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/1.jpg)
Course Name: DATA STRUCTURESCourse Code: 10B11CI211Course Credits: 4 (3-1-2)Session: Jan – Jun 2013
Data Structures Team:
Course Coordinator: Deepak, Nitin and Hemraj
![Page 2: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/2.jpg)
WHAT THE COURSE IS ABOUT
• Data structures is concerned with the representation and manipulation of data.
• All programs manipulate data.• So, all programs represent data in some way.• Data manipulation requires an algorithm.• We shall study ways to represent data and
algorithms to manipulate these representations.• The study of data structures is fundamental to
Computer Science & Engineering.
![Page 3: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/3.jpg)
PREREQUISITE
• C• Enumerated data type, void data type• typedef statement• Control statements• Use of memory by a program• Specification of pointers• Memory management functions• Problems with pointers• Various aspects of user defined functions
![Page 4: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/4.jpg)
WHAT IS DATA STRUCTURE
“A Conceptual and concrete way to organize data for efficient storage and manipulation”
![Page 5: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/5.jpg)
WHAT IS DATA STRUCTURE
A data structure is a logical and mathematical model of a particular organization of data.
The choice of particular data structure depends upon following consideration:
1.It must be able to represent the inherent relationship of data in the real world.
2.It must be simple enough so that it can process efficiently as and when necessary
![Page 6: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/6.jpg)
OVERVIEW OF DATA STRUCTURE
• Basic Terms related to data organization
• Data type
• Meaning of data structure
• Factor that influence the choice of data structure
• Different data structure
• Various operation performed on data structure
![Page 7: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/7.jpg)
BASIC TERMS RELATED TO DATA ORGANIZATION
• Data:Values or set of values.Eg. Observation of experiment, marks obtained by student.• Data item:A data item refers to a single unit of values.Eg. Roll no. name etc.• Entity:That has certain attribute or properties which may be
assigned values.Eg. Student is an entity
![Page 8: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/8.jpg)
BASIC TERMS RELATED TO DATA ORGANIZATION
• Entity set:Collection of similar entity.Eg. Student of a class• Record:Collection of related data items.Eg. Rollno, Dob, gender, class of a particular student.• File:Collection of related record.Eg. A file containing records of all students in a class
![Page 9: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/9.jpg)
BASIC TERMS RELATED TO DATA ORGANIZATION
• Key:
A key is a data item in a record that takes unique values and can be used to distinguish a record from other records.
• Information:
Meaningful data, coveys some meaning and hence can be used for decision making
![Page 10: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/10.jpg)
DATA TYPE
A data type is a collection of values and a set of operation that act on those values
Classification of data type:
1. Primitive data type
2. Abstract data type
3. Polymorphic data type
![Page 11: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/11.jpg)
PRIMITIVE DATA TYPE
• That is predefined. It is also known as built in data type.
Eg. C have built in data type int, long int, float, double, char.
![Page 12: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/12.jpg)
ABSTRACT DATA TYPE
• In computing, an abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations.
• The main contribution of the abstract data type theory is that it – (1) formalizes a definition of type (which was only intuitively
hinted on procedural programming) – (2) on the basis of the information hiding principle and – (3) in a way that such formalization can be explicitly
represented in programming language notations and semantics. This important advance in computer science theory (motivated by software engineering challenges in procedural programming) led to the emergence of languages and methodological principles of object-oriented programming.
![Page 13: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/13.jpg)
POLYMORPHIC DATA TYPE• Very often in programs, a generic operation must be
performed on data of different types. For example, in our bubble sort algorithm for the payroll records, when elements were found out of order in the id[] array, we needed to swap the integer elements in that array as well as the float elements in the hrs[] and rate[] arrays. If we decided to implement this swapping operation as a function, we would need to write two functions: one to swap integers, and another to swap floating point values; even though the algorithm for swapping is the same in both cases.
• The C language provides a mechanism which allows us to write a single swapping function which can be used on any data type. This mechanism is called a polymorphic data type, i.e. a data type which can be transformed to any distinct data type as required..
![Page 14: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/14.jpg)
THE STUDY OF DATA STRUCTURE INCLUDE:
• Logical description of data structure
• Implementation of data structure
• Quantative analysis of data structure, this include amount of memory, processing time
![Page 15: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/15.jpg)
TYPES OF DATA STRUCTURES
1. Linear data structure
2. Non linear data structure
![Page 16: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/16.jpg)
LINEAR DATA STRUCTURE
A data structure whose elements form a sequence, and every element in the structures has a unique predecessor and unique successor.
Eg. Array, linked list, stack and queues.
![Page 17: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/17.jpg)
NON LINEAR DATA STRUCTURE
A data structure whose elements do not form a sequence, and there is no predecessor and unique successor.
Eg. Trees, graphs
![Page 18: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/18.jpg)
ARRAYS
Collection of homogenous data elements.
Arrays can be:
1. One dimensional
2. Two dimensional
3. Multi dimensional
![Page 19: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/19.jpg)
LINKED LIST
• Linked list – Linear collection of self-referential class objects,
called nodes– Connected by pointer links– Accessed via a pointer to the first node of the list– Subsequent nodes are accessed via the link-pointer
member of the current node– Link pointer in the last node is set to null to mark the
list’s end• Use a linked list instead of an array when
– You have an unpredictable number of data elements– Your list needs to be sorted quickly
![Page 20: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/20.jpg)
The Linked List data structure
[0] [1] [2]array
A B CArray
linked
A B CLinked list
Linked lists are unbounded(maximum number of items limited only by memory)
node
![Page 21: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/21.jpg)
LINKED LIST
• Types of linked lists:– Singly linked list
• Begins with a pointer to the first node• Terminates with a null pointer• Only traversed in one direction
– Circular, singly linked• Pointer in the last node points back to the first node
– Doubly linked list• Two “start pointers” – first element and last element• Each node has a forward pointer and a backward pointer• Allows traversals both forwards and backwards
– Circular, doubly linked list• Forward pointer of the last node points to the first node and
backward pointer of the first node points to the last node
![Page 22: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/22.jpg)
LINKED LISTS (VARIATIONS)
• Basic elements:– Head– Node
• Simplest form: Linear-Singly-linked
headA
data pointer
node
A
Head
B C
![Page 23: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/23.jpg)
LINKED LISTS (VARIATIONS)
• Circular-linked Lists
– The last node points to the first node of the list
• Strengths– Able to traverse the list starting from any point– Allow quick access to first and last records through
a single pointer
• Weakness– A bit complicated during insertion, needs careful
setting of pointer for empty or one-node list
A
Head
B C
![Page 24: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/24.jpg)
LINKED LISTS (VARIATIONS)• Doubly-linked Lists
– Each inner node points to BOTH successor and the predecessor
• Strengths– Able to traverse the list in any direction– Can insert or delete a node very quickly given only that
node’s address
• Weakness– Requires extra memory and handling for additional
pointers
A
Head
B C
![Page 25: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/25.jpg)
LINKED LISTS (VARIATIONS)
• Putting together…Circular-doubly-linked lists!
A
Head
B C
![Page 26: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/26.jpg)
STACK
• Stack – New nodes can be added and removed only at the top– Similar to a pile of dishes– Last-in, first-out (LIFO) – Bottom of stack indicated by a link member to NULL– Constrained version of a linked list
• push– Adds a new node to the top of the stack
• pop– Removes a node from the top – Stores the popped value – Returns true if pop was successful
![Page 27: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/27.jpg)
DATA STRUCTURES -- STACKS
• A stack is a list in which insertion and deletion take place at the same end– This end is called top– The other end is called bottom
![Page 28: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/28.jpg)
QUEUES
• Queue– Similar to a supermarket checkout line– First-in, first-out (FIFO) – Nodes are removed only from the head – Nodes are inserted only at the tail
• Insert and remove operations – Enqueue (insert) and dequeue (remove)
![Page 29: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/29.jpg)
The Queue Operations
• A queue is like a line of people waiting for a bank teller. The queue has a front and a rear.
$ $
FrontRear
![Page 30: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/30.jpg)
TREES
• Tree nodes contain two or more links– All other data structures we have discussed
only contain one
• Binary trees– All nodes contain two links
• None, one, or both of which may be NULL
– The root node is the first node in a tree. – Each link in the root node refers to a child– A node with no children is called a leaf node
![Page 31: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/31.jpg)
TREE TERMINOLOGY
There is a uniquepath from the rootto each node.
Root is a level 0, childis at level(parent) + 1.
Depth/height of a treeis the length of the longest path.
level 0
1
2
3
4
![Page 32: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/32.jpg)
BINARY TREE• Each node has two successors
– one called the left child– one called the right child– left child and/or right child may be empty
A binary tree is either - empty or - consists of a root and two binary trees, one called the left subtree and one called the right subtree
![Page 33: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/33.jpg)
GRAPHSG = (V, E)
a vertex may have: 0 or more predecessors 0 or more successors
![Page 34: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/34.jpg)
abstract containers
hierarchical (1 to many)
graph (many to many)first ith last
sequence/linear (1 to 1)
set
![Page 35: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/35.jpg)
HEAPS
Heap is a binary tree that satisfy the following property:
• Shape property• Order propertyShape property states that heap is complete or
nearly complete binary tree.Order property states that:1. Either the element at any node is smallest of
all of its children, called min heap2. Element at any node is largest of all of its
children, called max heap.
![Page 36: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/36.jpg)
HASH TABLES
• There are many application that require a dynamic structure that support only insert, search and delete operations. These operations are commonly known as dictionary operations. A hash table is an effective data structure for implementing dictionaries.
![Page 37: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/37.jpg)
COMMON OPERATIONS ON DATA STRUCTURE
1. Traversal:-Accessing each element exactly once in order to process it.
2. Searching:-Finding the location of a given element.
3. Insertion:-Adding the new element to the structure.
4. Deletion:-Removing a existing element from the structure.
5. Sorting:-Arranging the elements in logical order.
6. Merging:-Combining the elements of two similar sorted structures into a single structure.
![Page 38: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/38.jpg)
ENUMERATED DATA TYPE
• Variable of enumerated data type enhance the readability of the program.
enum boolean {false, true};Here boolean is called tag name for the user
defined data type. Then we can declare variable of this type as follows:
enum boolean flag;Then we can assign value false or true to variable
flag, and also we can compare the value of flag with these values.
![Page 39: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/39.jpg)
void DATA TYPE• This is also known as empty data type, is useful in many situation;1. Void functionname(int x, int y)
{}
Functionname() does not return any value.2. Int functionname(void)
{}
Functionname() does not take any rgument.3. Void main()
{void *ptr;int x=5;ptr=&x;printf(“value pointed to pointer is now %d”,*(int*)ptr);
}Void pointer cannot be directly dereferenced without type casting. This
is because the compiler cannot determie the size of the value the pointer points to.
![Page 40: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/40.jpg)
REDEFINING DATA TYPES
• Using typedef statement we can make it possible to declare variables of user defined data types as with built in data types by redefining the user defined data type and giving our own name.
typedef enum{false, true} boolean;
The word boolean becomes name of the new defined data type.
boolean flag;
![Page 41: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/41.jpg)
CONTROL STATEMENTS
1. Decision making statements• if statement• if-else statement• switch statement2. Looping Statements• for statement• while statement• do while statement3. Jumping statements• brake statement• continue statement• goto statement
![Page 42: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/42.jpg)
MEMORY USE IN C
STACK
HEAP
BSS
CONST
DATA
TEXT
High Memory
Low Memory
Initialized and un initialized local variable
Memory allocated with malloc(). Calloc(), and realloc() functions
Un initialized static variable
Read only variable
Initialized & un initialized global variables & initialized static variables
Program code
![Page 43: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/43.jpg)
POINTER
• Pointer is a variable which contains reference of another variable
Address of x value of x
px x
Pointerpx=&x
![Page 44: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/44.jpg)
DECLARATION OF POINTER• “*” is used to declare and dereference the
pointers.data type *ptvar;
int *ip; /*declare ip to be pointer to an integer*/ *ip=5;/* assign 5 to the integer pointer to which ip
points*/
address 5
ip
![Page 45: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/45.jpg)
POINTER OPERATOR
• Two operators:
&-----address of
*------at address in
X=8 let x be at 100 (x at 100)
Ip=&x ip contains 100 (ip at 200)
a=*ip contains 8 (a at 250)
8
100
8
![Page 46: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/46.jpg)
ASSIGNMENT IN POINTER
GivenInt x;Double y;Int *a,*b;double *c;
a=&x; /* a now points to x*/b=a; /*b now points to the same variable as a
points */c=&y; /* c points to y */
![Page 47: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/47.jpg)
POINTER TO A POINTER
• Variable that hold an address of a another variable that in turn holds an address of another variable, this type of variable is know as pointer to pointer.
• Pointer to pointer will be declared as
**ptr;
![Page 48: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/48.jpg)
DYNAMIC MEMORY MANAGEMENT
Memory management functions
Function name
Description
malloc Allocate memory from heap
calloc Allocate memory from heap and initializes the allocated memory to zeros
realloc Readjusts the existing block and copies the contents to new location
free Deallocates block allocated by malloc, calloc and realloc fuctions
![Page 49: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/49.jpg)
DYNAMIC MEMORY ALLOCATION
• Dynamic memory allocation– Obtain and release memory during execution
• malloc– Takes number of bytes to allocate
• Use sizeof to determine the size of an object– Returns pointer of type void *
• A void * pointer may be assigned to any pointer• If no memory available, returns NULL
– ExamplenewPtr = malloc( sizeof( struct node ) );
• free– Deallocates memory allocated by malloc– Takes a pointer as an argument– free ( newPtr );
![Page 50: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/50.jpg)
calloc()
The calloc() function dynamically allocates memory and automatically initializes the memory to zeroes. Example
newPtr = calloc( 5,sizeof( struct node ) );
![Page 51: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/51.jpg)
realloc()
The realloc() function changes the size of previously dynamically allocated memory with malloc(),calloc() or realloc function() functions.
The prototype of realloc() function is Void *realloc(void *block,size_t size);It takes two arguments, first argumant is
pointer to the original object and second argument is new size of the object.
![Page 52: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/52.jpg)
free()
The free() function deallocates a memory block previously allocated with malloc(), calloc(), or realloc() functions. Prototype for free function is
void free(void *block);
It takes one argument that specify the pointer to the allocated block.
![Page 53: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/53.jpg)
DEBUGGING POINTERS
The pointer can be the source of mysterious and catastrophic program bugs. Common bugs related to related to pointer and memory management is
1. Dangling pointer
2. Null pointer assignment
3. Memory leak
4. Allocation failure
![Page 54: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/54.jpg)
STRUCTURES
A structure is a collection of data elements, called fields which may be of different type.
Individual elements of a structure variable are accessed using dot operator (.), if a pointer is used to point to a structure variable , then arrow operator (->) is used.
![Page 55: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/55.jpg)
STRUCTURE
Example of complex data structures and the corresponding self referential structure to represent these data structure.
Next pointer field
Information field
head
1200 1201 1202 X
![Page 56: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/56.jpg)
STRUCTURE
typedef struct nodetype{
int info;
struct nodetype *next;
} node;
node * head;
![Page 57: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/57.jpg)
Recursion
![Page 58: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/58.jpg)
Divide-and-conquer
![Page 59: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/59.jpg)
What is recursion?
![Page 60: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/60.jpg)
Recursive functions
![Page 61: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/61.jpg)
Nature of recursion
![Page 62: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/62.jpg)
Nature of recursion
![Page 63: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/63.jpg)
Types of Recursion• Direct Recursion• Indirect Recursion
![Page 64: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/64.jpg)
Iteration vs. recursion
![Page 65: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/65.jpg)
Recursion: sum()
![Page 66: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/66.jpg)
Recursion: sum()
![Page 67: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/67.jpg)
How does recursion work?
![Page 68: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/68.jpg)
How does recursion work?
![Page 69: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/69.jpg)
How does recursion work?
![Page 70: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/70.jpg)
RecursionRecursive functions
– Functions that call themselves
– Can only solve a base case
– Divide a problem up into• What it can do• What it cannot do
– What it cannot do resembles original problem– The function launches a new copy of itself (recursion
step) to solve what it cannot do
– Eventually base case gets solved• Gets plugged in, works its way up and
solves whole problem
![Page 71: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/71.jpg)
RecursionExample: factorials–5! = 5 * 4 * 3 * 2 * 1
–Notice that•5! = 5 * 4!•4! = 4 * 3! ...
–Can compute factorials recursively
–Solve base case (1! = 0! = 1) then plug in•2! = 2 * 1! = 2 * 1 = 2;•3! = 3 * 2! = 3 * 2 = 6;
![Page 72: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/72.jpg)
Recursion
5!
(a) Sequence of recursive calls. (b) Values returned from each recursive call.
Final value = 120
5! = 5 * 24 = 120 is returned
4! = 4 * 6 = 24 is returned
2! = 2 * 1 = 2 is returned
3! = 3 * 2 = 6 is returned
1 returned
5 * 4!
1
4 * 3!
3 * 2!
2 * 1!
5!
5 * 4!
1
4 * 3!
3 * 2!
2 * 1!
![Page 73: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/73.jpg)
1
2 Recursive factorial function */
3 #include <stdio.h>
4
5 long factorial( long number ); /* function prototype */
6
7 /* function main begins program execution */
8 int main()
9 {
10 int i; /* counter */
11
12 /* loop 10 times. During each iteration, calculate
13 factorial( i ) and display result */
14 for ( i = 1; i <= 10; i++ ) {
15 printf( "%2d! = %ld\n", i, factorial( i ) );
16 } /* end for */
17
18 return 0; /* indicates successful termination */
19
20 } /* end main */
21
![Page 74: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/74.jpg)
1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 36288010! = 3628800
22 /* recursive definition of function factorial */
23 long factorial( long number )
24 {
25 /* base case */
26 if ( number <= 1 ) {
27 return 1;
28 } /* end if */
29 else { /* recursive step */
30 return ( number * factorial( number - 1 ) );
31 } /* end else */
32
33 } /* end function factorial */
![Page 75: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/75.jpg)
Example Using Recursion: The Fibonacci Series
Fibonacci series: 0, 1, 1, 2, 3, 5, 8...– Each number is the sum of the previous two – Can be solved recursively:
• fib( n ) = fib( n - 1 ) + fib( n – 2 )
– Code for the fibonacci functionlong fibonacci( long n ){if (n == 0 || n == 1) // base case
return n;else
return fibonacci( n - 1) + fibonacci( n – 2 );}
![Page 76: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/76.jpg)
Example Using Recursion: The Fibonacci Series
f( 3 )
f( 1 )f( 2 )
f( 1 ) f( 0 ) return 1
return 1 return 0
return +
+return
![Page 77: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/77.jpg)
1
2 Recursive fibonacci function */
3 #include <stdio.h>
4
5 long fibonacci( long n ); /* function prototype */
6
7 /* function main begins program execution */
8 int main()
9 {
10 long result; /* fibonacci value */
11 long number; /* number input by user */
12
13 /* obtain integer from user */
14 printf( "Enter an integer: " );
15 scanf( "%ld", &number );
16
17 /* calculate fibonacci value for number input by user */
18 result = fibonacci( number );
19
20 /* display result */
21 printf( "Fibonacci( %ld ) = %ld\n", number, result );
22
23 return 0; /* indicates successful termination */
24
25 } /* end main */
26
![Page 78: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/78.jpg)
Enter an integer: 0Fibonacci( 0 ) = 0
Enter an integer: 1Fibonacci( 1 ) = 1
Enter an integer: 2Fibonacci( 2 ) = 1
Enter an integer: 3Fibonacci( 3 ) = 2
Enter an integer: 4Fibonacci( 4 ) = 3
27 /* Recursive definition of function fibonacci */
28 long fibonacci( long n )
29 {
30 /* base case */
31 if ( n == 0 || n == 1 ) {
32 return n;
33 } /* end if */
34 else { /* recursive step */
35 return fibonacci( n - 1 ) + fibonacci( n - 2 );
36 } /* end else */
37
38 } /* end function fibonacci */
![Page 79: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/79.jpg)
Enter an integer: 5Fibonacci( 5 ) = 5
Enter an integer: 6Fibonacci( 6 ) = 8
Enter an integer: 10Fibonacci( 10 ) = 55
Enter an integer: 20Fibonacci( 20 ) = 6765
Enter an integer: 30Fibonacci( 30 ) = 832040
Enter an integer: 35Fibonacci( 35 ) = 9227465
![Page 80: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/80.jpg)
Recursion vs. Iteration• Repetition
– Iteration: explicit loop– Recursion: repeated function calls
• Termination– Iteration: loop condition fails– Recursion: base case recognized
• Both can have infinite loops
• Balance – Choice between performance (iteration) and
good software engineering (recursion)
![Page 81: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/81.jpg)
GCD
else{
r=x%y;return (recgcd(y,r));
}
Output:Enter the two number 5 8G.C.D. of the 5 and 8 is 1
![Page 82: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/82.jpg)
![Page 83: WHAT THE COURSE IS ABOUT](https://reader035.vdocument.in/reader035/viewer/2022062717/56812ac2550346895d8e8d57/html5/thumbnails/83.jpg)
Problems on recursion• Factorial• Fibonacci number generation• Fibonacci series generation• Reversing an integer number• Calculating power of a number• Prime Number checking• Generating prime number series• Calculating GCD of two numbers• Generating table of a number