Data - Structures Using C PDF
Data - Structures Using C PDF
M – Scheme
e-TEXTBOOK
on
DATA STRUCTURES USING C
for
IV Semester Diploma in Computer Engineering
A.Ghousia Jabeen,
Principal,
Thanthai Periyar E.V. Ramasamy Govt. Polytechnic College for Women,
Vellore – 636 002 600 113
K. Selvamalathi
Lect(Sr. Gr.)/Computer Engg.
Ayyanadar Janaki Ammal Polytechnic College,
Sivakasi.
V. Banumathi
Lect. (Sr. Gr.)/Computer Engg.
ADJ Dharmambal Poly. College,
Nagapattinam.
S.A.Amudha,
Lecturer/ Computer Engg.
V.S.V.N. Polytechnic College,
Virudhunagar – 626001
Validated By
Mrs. L.Agnes Lavanya
Lecturer
Govt. Polytechnic College,
Cheyyar.
STATE BOARD OF TECHNICAL EDUCATION & TRAINING, TAMILNADU.
Instructions Examination
Subject Hours / Internal Board Duration
Hours / Total
Semester Assessment Examination
Week
DATA
STRUCTURES 6 90 25 75 100 3 Hrs
USING C
RATIONALE
Data structures are the techniques of designing the basic algorithms for real-life projects. In
the present era, it is very essential to develop programs and organize data in such a way
that it solves a complex problem efficiently. Understanding of data structures is essential and
this facilitates to acquire sound knowledge of the insight of hardware requirement to any
1.1. Introduction to Data Structures : Introduction - Data and Information - Elementary 6 Hrs
data structure organization - Types of data structures - Primitive and Non
Primitive data structures – Operations on data structures : Traversing, Inserting,
Deleting, Searching, Sorting, Merging - Different Approaches to designing an
algorithm : Top-Down approach , Bottom-up approach - Complexity : Time
complexity , Space complexity - Big ‘O’ Notation.
1.2 ARRAYS: Introduction - Characteristics of Array - One Dimensional Array - Two 7 Hrs
Dimensional Arrays - Multi Dimensional Arrays – Advantages and Disadvantages
of linear arrays - Row Major order - Column Major order - Operations on arrays
with Algorithms (searching, traversing, inserting, deleting - Pointer and Arrays –
Pointers and Two Dimensional Arrays - Array of Pointers - Pointers and Strings –
Implementation of arrays -
1.3 Strings : Strings and their representations - String Conversion- String manipulation, 3 Hrs
String arrays
UNIT – II STACKS , RECURSION AND QUEUES … 16 Hours
2.1 Definition of a Stack - Operations on Stack (PUSH & POP)- Implementing Push 6 Hrs
and Pop Operations - Implementation of stack through arrays – Applications of
Stack : Reversing a list - Polish notations - Conversion of infix to postfix expression
2.2 Recursion - Recursive definition – Algorithm and C function for : Multiplication of 4 Hrs
Natural numbers - Factorial Function - GCD function - Properties of Recursive
algorithms/functions – Advantages and Disadvantages of Recursion
2.3 Queues: The queue and its sequential representation - implementation of Queues 6 Hrs
and their operations - implementation of Circular queues and their operations -
Dequeue and Priority queues(Concepts only)
UNIT – III LINKED LISTS …… 16 Hours
3.1 Terminologies: Node, Address, Pointer, Information, Null Pointer, Empty list -. 16 Hrs
Type of lists : Singly linked list , Doubly linked list, Circular list - Representation
of singly linked lists in Memory-Difference between Linked & sequential List –
Advantages and Disadvantages of Linked list- Operations on a singly linked list
( only algorithm) : Traversing a singly linked list , Searching a singly linked list ,
Inserting a new node in a singly linked list ( front, middle, end), Deleting a node
from a singly linked list (front, middle, rear) - Doubly linked list, Circular linked lists
(Concepts only, no implementations)
UNIT – IV TREES AND GRAPHS …… 17 Hours
4.1 Trees: Terminologies: Degree of a node, degree of a tree, level of a node, leaf 10
node, Depth / Height of a tree, In-degree & out-Degree, Path, Ancestor & Hrs
descendant nodes-, siblings - Type of Trees : Binary tree - List representation of
Tree - Binary tree traversal ( only algorithm ) : In order traversal , Preorder
traversal , Post order traversal - Expression tree – Binary Search Tree – Creation
of a Binary Serach tree without duplicate node.
4.2 Graphs : Introduction - Terminologies: graph, node (Vertices), arcs (edge), 7 Hrs
directed graph, in-degree, out-degree, adjacent, successor, predecessor, relation,
weight, path, length - Representations of a graph - Adjacency Matrix
Representation - Adjacency List Representation - Traversal of graphs : Depth-
first search (DFS) , Breadth-first search (BFS) - Applications of Graph
UNIT – V SORTING ,SEARCHING AND HASHING ….. 15 Hours
5.1 Sorting Techniques : Introduction – Algorithms and “ C” programs for : Selection 6 Hrs
sort , Insertion sort , Bubble sort – Algorithms only : Merge Sort ,Radix sort, Shell
sort , Quick sort
5.2 Searching : Introduction - Algorithms and “ C” programs for Linear search and 4 Hrs
Binary search
5.3 Hashing : Hash tables – methods- Hash function - Collision resolution techniques 5 Hrs
1.1.2 Data
Data or Data item means a value or set of values. Eg.453, 16/03/1998, java, Chennai
etc.
If the data or data item is divided into subitem, then that is called group item.
Eg.Address. Address is a group item and may be divided into doorno., street, place, pincode
etc.
If the data could not be divided further, then that is called elementary data.
Eg.Regno.,Name, price etc.
Entity
An entity is a real thing and has set of properties or attributes having numeric or non
numeric values. Eg. Entity named Employee may have the following attributes
Information
Meaningful data or processed data is called Information
Eg. The data or value 27 has no meaning. But if we say, the age of employee is 27, then the
data 27 has meaning and becomes information.
Page 1 - 1
1.1.3 Classification of Data structures
Data structures are classified into various types based on their characteristics. The
following figure represents the various classifications of data structures.
Data structure
boolean
Page 1 - 2
b. Non-Linear Data structure
In non-linear data structure the data are not stored in sequential order. The various non-
linear data structures are
Graph - It is used to store data based on the relationship among pair of data.
Tree - It is used to store data based on the hierarchical relationship among data.
Table -It is used to store data in row and column order.
Sets -It is used to store combined data.
In each step, design is refined into most concrete level until we reach there is no more
refinement.
In this approach we look at the overall requirement of calculator and start coding. We
build the main function first, then add calls to various functions like add, subtract etc... and then
display the output. So what we did here was, approached the problem by first looking at the
overall objective which is the top, then deciding when and where to call the functions and
eventually going down to the lowest level detail of designing the functions themselves. Hence it
is called the "top-down" approach.
Page 1 - 3
1.1.7.2 Bottom Up Approach
In this approach the individual base elements of the system are first specified in great
detail. These elements are then linked together to form larger subsystems, which then in turn
are linked, sometimes in many levels, until a complete top-level system is formed.
A a = a+1 1
For i = 1 to n
B n
a= a+1
For i = 1 to n
C For j = 1 to n n2
a = a+1
The space needed by the program is the sum of the following components.
1. Fixed Space requirement: This includes instructions space, variable space and
constants.
2. Variable Space requirement: This includes space needed by structured variables
whose size depends on particular instance of variable. It also includes additional space like
functions using recursion.
Page 1 - 4
1.1.8 Big O notation
Big O notation helps to determine time as well as space complexity of the
algorithm. This is useful to set the prerequisites of algorithm and helps to develop and design
efficient algorithm in terms of time and space complexity.
Eg: Sorting can be done using methods like bubble sort, insertion sort, selection sort,
heap sort, quick sort etc. Each algorithm is having its own complexity. Two of them are given
below. O(n) is pronounced as order of n.
Sort Method Best case Worst case Complexity
Complexity
Bubble O(n) O(n2)
Quick O(n log n) O(n2)
If f(n) represents the computing time of some algorithm and g(n) represents a known
standard function like n, n2, n log n etc. then f(n) => O g(n) means that f(n) of n is equal to
biggest order of function g(n).
1.2 ARRAYS
1.2.1 Introduction
A data structure is said to be linear if its elements form a sequence. Such linear sequence
of data are to be stored in consecutive memory locations for easy retrieval. If data are stored in
consecutive memory locations then it is called array.
Definition
Array means set of finite number of elements of same (or homogeneous) data type.
1.2.2 Characteristics of Array
1) An array holds elements of the same data type
2) Array elements are stored in subsequent memory locations
3) Two-dimensional array elements are stored row by row in subsequent memory
locations.
4) Array name represents the address of the starting element
5) Array size should be mentioned in the declaration. Array size must be a constant
and not a variable.
6) An array index starts from value zero
Terminology
Base Address : This is the address of the memory where the first element of the array is
located
Index : The element of the array is referred by a subscript or index like A[i]. Here ‘i’ is the
subscript or index
Lower Bound : Lower bound of array is the starting position of array. Usually this is zero.
Upper Bound : Upper Bound of array is the last position of array. Usually this is n-1
Range or Range of Indices: The range is lower bound to upper bound
Size of the Array : Size of the array = Upper Bound – Lower Bound + 1
Page 1 - 5
Eg: int A[100];
Lower Bound = 0; Upper Bound = 99; Range = 0 to 99; Memory required to store 100 array
elements = 100 integer data x 2 bytes = 200 bytes
datatype arrayname[size];
Example
int a[100];
During execution, the complier allocates 100 consecutive memory locations and name
the starting Memory location as a.
Computer memory is a sequence of physical storage cells and each cell is identified by
an address. Assume an array a[100] is to be stored in memory and the starting address (base
address) = 1000 and each element takes one word, then the figure given below shows the
memory allocation.
999
1000 a[0]
1001 a[1]
1002 a[2]
1003 … Base Address = 1000
1004 …
…
…
1099 a[99]
…
…
N
During execution, the compiler allocates 100 consecutive memory location and the
starting address is named as ‘a’ ie the name of the array. The lower bound of array is 0 and
upper bound of array is 99 and range = 100
int a[100];
During execution, 100 consecutive memory locations will be allocated in the memory.
Page 1 - 6
Assume
dsize = the size of byte to store one data (dsize = 2 bytes for integer; 4bytes for float
and 1byte for char)
Two dimensional arrays are set of homogeneous elements arranged in row and column
order and needs two subscript to refer one data.
Example
During execution the complier allocates 15 consecutive memory locations and name the
starting memory location as a.
The size of row is called row range and size of column is called column range and maximum
element in 2D array is row size * column size
a[0][0]
a[0][1] First row
a[0][2]
a[1][0]
Second
a[1][1] row
a[1][2]
In Column major order, the elements of the array are stored on column by column
basis, that is, all the elements in the first column first , then the second column second and so
on as given below.
a[0][0]
a[1][0] First Column
a[0][1]
Second Column
a[1][1]
a[0][2]
Third Column
a[1][2]
During execution 50 consecutive memory locations will be allocated in the memory. That is
dsize be the size of byte to store one data (dsize = 2 bytes for integer; 4 bytes for float
and 1 byte for char)
Page 1 - 8
1.2.5 Multi Dimensional Arrays
In advanced applications, three or more dimensional arrays are used. Multi dimensional
array is declared like below.
datatype arrayname[size1][size2][size3]…[size[n];
Example
int fees[branch][semester][name][amount];
The above example has four dimensions.
1.2.7.1 Traversing
It is defined as the process of accessing or visiting all elements in an array exactly once.
Generally arrays are visited to do the following
i. to read an element.
ii. to print an element.
iii. to process an element.
Explanation:
Here, A is a linear array with upper bound UB and lower bound LB. This algorithm
applies an operation on each element of linear array A. We are using a counter I which counts
the number of elements in A. So, in the starting, the value of I is 0 since LB is also 0.
Page 1 - 9
Operation Example
read for(i=0;i<n;i++)
scanf(“%d”, &a[i]);
print for(i=0; i<n; i++)
printf(“%d”,a[i])
Insertion means adding a new element into an array. The new element can be inserted
at any one of the following positions of the array.
i. at the end of the array.
ii. at the middle of the array.
Assume
Array name is A
the position to insert is K
upper bound is UB
size of the array is n.
The following steps are used to insert an element into the array A.
i. If K = UB+1
store the new element at A[K]
ii. Else check if K <=UB
Shift the elements like below
A[UB] → A[UB+1]
A[UB-1] → A[UB]
.
.
A[K] → A[K+1]
.
Store the new element at A[K]
iii. Change the UB value = UB+1 and n = n+1
As per algorithm K = 3. So
A[5] → A[6].
A[4] → A[5];
A[3] → A[4];
The following figure shows the memory representation before and after insert.
Page 1 - 10
Before insert After shifting After insert
1000 23 A[0] 1000 23 A[0] 1000 23 A[0]
1001 56 A[1] 1001 56 A[1] 1001 56 A[1]
1002 89 A[2] 1002 89 A[2] 1002 89 A[2]
1003 10 A[3] 1003 A[3] 1003 65 A[3]
1004 55 A[4] 1004 10 A[4] 1004 10 A[4]
1005 20 A[5] 1005 55 A[5] 1005 55 A[5]
1006 20 A[6] 1006 20 A[6]
UB=5 A3 position is free UB=6
The following steps are used to delete an element from the array A.
i. Shift the elements like below
A[K+1] → A[K]
A[K+2] → A[K+1]
.
.
.
A[UB] → A[UB-1]
Page 1 - 11
1.2.7.4 Searching an element in Linear Array (Linear Search)
Searching means finding and locating an element in array. There are two types of
search.
1. Linear Search
2. Binary Search
Linear Search :
Assume
The array name is A
The element to be searched is x
The array size is n
The following steps are used to search an element.
i. Compare x with A[0]. If equals print “Success” and print the position, stop the process.
Else Compare x with A[1]. If equals print “Success” and print the position,stop the
process
Else repeat the process upto the last element i.e.A[n-1].
ii. If no array value matches, print “Search Fail”
Example
Consider an array having 6 elements and search the element 25.
Given Array Data : 12 16 4 25 69 43
Search Data x :25
Pass 1 :12 16 4 25 69 43 : compare 12 with 25, no match
Pass 2 :12 16 4 25 69 43 : compare 16 with 25, no match
Pass 2 :12 16 4 25 69 43 : compare 4 with 25, no match
Pass 2 :12 16 4 25 69 43 : compare 25 with 25, match.
So Print Success and position = 3
Value 10 20 30 40 50
Base Address
Page 1 - 12
Also x = &x[0]= 1000. If we use a pointer variable ‘p’ to store the address, then we can
write like below.
p = x; or p = &x[0] ; ;
We can access every value of array x by using p. The relationship between p and x is
given below.
p = &x[0] = 1000
th
In general Address of n element = Base address + (n * scale factor of datatype)
Here
p points to first row
th
p+i points to i row
th
*(p+i) points to element in i row
th th
(*p+i)+j points to j element in i row
th th
*(*p+i+j) gives value stored in i row and j column
Page 1 - 13
1.2.9 Array of Pointers
Since a pointer is a variable , we can create an array of pointers just like we can create
any array of any other type.
Example
int *p[]; any number of rows with any number of columns of integer
Char name[3][25]; ;
declares that it is a table of strings containing 3 names and all names are of
maximum 25 characters. So,3x25 bytes = 75 bytes are allocated during compilation. It is shown
in the following diagram.
0 1 2 3 4 5 6 7 8 23 24
A N D H R A \0
K A R N A T A K A \0
T A M I L N A D U \0
If the individual names are of not equal length like the table given below, this type of
declaration is useful.
Example
Page 1 - 14
will take only 28 bytes like below.
A N D H R A \0
K A R N A T A K A \0
T A M I L N A D U \0
The disadvantage of pointer to string is that, the string value should be initialized
during declaration itself since scanf() function could not be used to read strings.
datatype arrayname[size];
Example
int a[100];
During execution the complier allocates 100 consecutive memory locations and named
the starting memory location as a.
Example
During execution the complier allocates 15 consecutive memory locations and named
the starting memory location as a.
1.3 String
Introduction
Computers are used for processing non-numerical data called character data. The
processing may include pattern matching, replacing text ,editing text etc. Computer terminology
uses the term string for a sequence of characters rather than the term word or text.
Page 1 - 15
When a string is stored in computer memory, a null character ‘\0’ is automatically stored
as last character to indicate the end of string. Hence one extra space is needed to store the null
character.
Example
Consider the following statement
char str[9] = “Computer”
The figure given below shows the memory representation of the string “Computer”
C o m p u t e r \0
Program Example
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
void main()
{
char ch, str[3];
int value;
printf(“Enter any alphabet character\n”);
scanf(“%c”,&ch);
value = toascii(ch);
printf(“ Ascii value = %d\n”,value);
printf(“Enter any string \n”);
scanf(“%s”,str);
value = atoi(str);
printf(“ Integer value = %d\n”,value);
getch();
}
Output:
Enter any alphabet character
A
Ascii value = 65
Enter any string
25
Integer value = 25
Page 1 - 16
1.3.4 String Manipulation
The library file string.h is used to perform string operations. The important string
functions are given below.
strlen()
It is used to find the length of a given string. The syntax is given below
int n = strlen(string);
Example
n= strlen(“computer”); n=8
strcpy()
It is used to copy one string to another. The syntax is given below
strcpy(string1,string2);
Example
Assume string1 = “Computer” string2 = “Engineering”
strcpy(string1,string2); results in string1 = “Engineering”
string2 = “Engineering”
strcat()
It is used to concatenate two strings. The syntax is given below
strcat(string1,string2);
Example
Assume string1 = “Computer” string2 = “Engineering”
strcat(string1,string2); results in string1 = “ComputerEngineering”
string2 = “Engineering”
It is noted that, there is no space between two strings while concatenating. i.e.
“ComputerEngineering” is correct and “Computer Engineering” is not correct.
Page 1 - 17
strcmp()
It is used to compare two strings.
This functions returns a value 0 if two strings are equal.
This function returns positive value i.e. >1 if first string is bigger than second
string
This function returns negative value i.e. < 1 if first string is lower than second
string
strcmp(string1,string2);
Example
1. strcmp(“Computer”,”Computer”); results in zero value
2. strcmp(“Computer”,”Engineering”); results in value (-2)
since Ascii value of C = 67 &Ascii value of E = 69, so 67-69 = -2
3. strcmp(“Balu”,”Arun); results in value (1)
since Ascii value of B = 66 &Ascii value of A = 65, so 66-65 = 1
strrev()
It is used to reverse a string. The syntax is
string2 = strrev(string1);
Example
Assume string1 = “Computer”
string2 = strrev(string1); will give string2 = “retupmoC”
strlwr()
It is used to convert the alphabets in given string to lowercase. The syntax is
strlwr(string);
Example
Assume string1 = “Computer”
strlwr(string1) results in string1 = “computer”
strupr()
It is used to convert the alphabets in string to uppercase. The syntax is
strupr(string);
Example
Assume string1 = “Computer”
strupr(string1) results in string1 = “COMPUTER”
strstr()
It is used to find the position of any substring in a given string. The syntax is
Page 1 - 18
Example
Assume string1 = “Computer” string2 = “put”
int pos = strstr(string1,string2); gives the result pos = 3
#include <stdio.h>
#include <string.h>
void main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
char str3[20];
int len ;
When the above program is compiled and executed, it produces the following result.
If you follow the rule of array initialization, then you can write the above statement as
follows.
Page 1 - 19
Following is the memory representation of the above defined string in C
The C compiler automatically places the '\0' character at the end of the string when it
initializes the array. Let us try to print a sample string.
#include<stdio.h>
void main ()
{
char str[10]={“Good Luck”};
printf("Greeting message: %s\n",str);
}
When the above code is compiled and executed, it produces the following result.
Page 1 - 20
Big O notation helps to determine time as well as space complexity of the algorithm
Array means set of finite number of elements of same (or homogeneous) data type
There are two ways to represent 2 D array in memory. They are
1. Row Major representation
2. Column Major representation
Pointersare used to store the address of any variable
The array name itself is a pointer to the first element
The 2D array name represents the address of 0th row and 0th column element.
A common use of an array of pointers is to create an array of strings
The advantage of pointer to string is memory space saving.
string is defined as a set of characters enclosed in double quotes and ended with null
character ‘\0’
Array of Characters are used to represent strings in computer memory
stdlib.hheader file contains functions to convert string
The library file string.h is used to perform string operations
Review Questions
Part A (2 Marks)
1. What is data and information?
2. What is the difference between data and information?
3. Define entity.
4. What are the primitive data structures?
5. What are the non – primitive data structures?
6. What are the operations that can be performed on data structures?
7. What are the various approaches in designing an algorithm?
8. What is called top down approach?
9. What is called bottom up approach?
10. What is called time Complexity?
11. What is called Space Complexity?
12. What is the use of Big O Notation?
13. What is an array?
14. How will you represent one dimensional array?
15. How will you declare two dimensional array?
16. What is called string?
17. How will you declare a string?
18. List few operations that can be performed on strings.
19. What is called null character?
20. How will you read a string?
21. What is the disadvantage of pointer to string?
Page 1 - 21
Part B (3 Marks)
Part C (5 or 10 Marks)
1. List and explain the types of data structures.
2. Explain traversing with algorithm .
3. Explain how will you insert and delete data in array.
4. Explain primitive and non – primitive data structure in detail.
5. Explain sorting with example.
6. Assume an array contains 8 data. Explain how will you search an element in that array.
7. Write the algorithm for merging and explain with example.
8. Explain how will you store 1D array in detail.
9. What are the two ways of representing array? Explain.
10. What is called row major and column major representation?
11. Explain top down and bottom up approach in detail.
12. Write a brief note on complexity.
13. What is called multidimensional array? Give examples.
14. Explain pointer and arrays.
15. Explain ‘pointers and two dimensional array.
16. What is called array of pointers?
17. Explain how pointers are used in representing strings.
18. Explain how will you implement array.
19. List the use of string conversion functions.
20. Write a program to illustrate the use of string functions.
21. Explain string handling functions in detail.
*****************************
Page 1 - 22
Unit 2 STACKS , RECURSION AND QUEUES
OBJECTIVES
To understand Stack and Queue
To know about the Operations of Stack
To know about the applications of stack
To understand Linked lists and its implementation
Stack is a linear data structure in which addition and deletions of items can be
made at the top of the stack. There are two operations , which can be performed on stack.
When push operation is performed the top moves upwards and when pop
operation is performed the top moves downwards.
10 Top
8
6
4
2
12 Top
10
Now the top element is 12. 8
6
Remove an element from stack : 4
2
Page 2 - 1
pop()
Poped or removed element=12
10 Top
8
6
4
2
Pop ( )
Poped or removed element=10
8 Top
6
4
2
push(16)
Note:
The last item pushed onto a stack is always the first that will be popped from the
stack. So stack is called last in first out (LIFO).
Declare an array large enough so that the stack can grow and shrink within the
space allocated for it. During program execution, Fix one end of array as bottom of stack.
The other end of array may be used as top of stack which keeps shifting constantly as
Page 2 - 2
items are popped and pushed. The index of the array containing top element is stored in
another field. Therefore declare stack as a structure containing two fields.
1. An array to hold elements of stack.
2. An integer to indicate the position of current top of the stack.
#define maxele 100
struct stack
{
int top;
int items[maxele];
};
struct stack s;
Operations on stack:
Push:- This operation is used to add a new item into the stack.
i. Check whether the stack is full or not
if top = n-1 then stack is full, so exit.
ii. If stack is not full increment the value of top by one (top = top+1)
iii. Place the item on top position of the array as stack[top]=item
Page 2 - 3
Procedure:
push(stack,top,n,item)
{
if(top == n-1) //check whether the stack is full or not.
{
print “stack full”;
exit;
}
else
{
top = top+1 //increment the value of top by1
stack[top]= item //add the new item on the top of the stack.
}
}
Pop:-
This operation is used to remove an item from the top of the stack.
i. Check whether the stack is empty or not if top= -1 then stack is empty so
exit
ii. If stack is not empty copy the top element into a variable item
item = stack[top]
iii. Decrement the value of top by 1
top = top -1
Procedure:
pop(stack,top,item)
{
if(top = -1) //check whether the stack is empty or not
{
print “Stack is Empty”;
exit;
}
else
{
item = stack[top]
top = top-1
}
}
Page 2 - 4
Application of stack:
i. For applications in which information must be saved and later retrieved in
reverse order.
ii. Stack is used in expression evalution. For this the expression must be
converted as postfix expression.
Reversing List : The list of elements can be reversed using stack. To reverse a string
that do the following
i. Create an empty stack.
ii. One by one push all characters of string to stack.
iii. One by one pop all characters from stack and put them back to string.
Example:
To reverse string COMP
Steps:
1. Initialize an empty character array named as result string.
2. Initialize a stack.
3. Scan the string character wise from left to right till the terminating character like
$ or # or ’\0’
a. First character is ‘C’ push it into stack.
2
1
top 0 C
2
Top 1 O
0 C
Page 2 - 5
c. Third characters Second is ‘M’ push it into stack.
TOP 2 M
1 O
0 C
d. Fourth character is ‘P’ push it into stack.
Top 3 P
2 M
1 O
0 C
e. Fifth character is ‘\o’ so pop up the characters one by one from the stack
till end of stack and add to result string
resultstring = PMOC
2
1
0
Top -1
Algorithm :
Page 2 - 6
{
top = top + 1
stack[top] = x ; //push into stack
x = nexttoken(ST);
}
while(top ≠ -1)
{
RST=RST+STACK[top];
top=top-1;
}
}
Infix expression: If the operator in an expression are placed in between the operands
then the expression is called infix expression.
Example:
i. A+B
ii (A+B)/(C+D)
iii (C*D)+(A+B)
Polish notation:
If the operator in an expression are present before or after the operands, then that
expression is said to be in polish notation. There are two types of polish notation.
They are
a. postfix expression
b. prefix expression
Example
i. AB+ => A+B
ii AB+CD+ / => (A+B)/(C+D)
iii CD* AB++ => (C*D)+(A+B)
Page 2 - 7
b. Prefix expression: If the operator in an expression are placed before the operands then
the expression is called prefix expression.
Example
i. +AB =>A+B
ii. /+AB+CD =>(A+B)/(C+D)
iii. +*CD+AB =>(C*D)+(A+B)
i. Add a dollar symbol at the end of the given infix expressions. initialize a stack
and an array to hold characters
ii. Scan the expression characterwise from left to right.
iii. If the character is operand add it to character array.
iv. If the character is operator or open parentheses, push it into the stack, while
pushing check the priority of the current input operator and the topmost operator
inside the stack(stack priority).
If the priority of the current operator is less than the top most operator in the
stack, pop up the operator from the stack and add it to the character array until
an operator having less priority than the input operator is reached and push the
current operator into the stack.
v. If the character is opening parenthesis push it into the stack.
vi. If the character is closing parenthesis pop up the operator from the stack up to
opening parenthesis and add it to the character array.
vii. If the stack is not empty after the scanning is over, pop up the remaining
operators from the stack till top = -1 is reached and add it to the character array.
viii. The character array contains the postfix form of the given infix expression.
Example:
Convert the infix expression A into postfix expression.
A=(5+3)*3-2
Steps:
1. Add dollar symbol to the end of given infix expression.
A=(5+3)*3-2$
2. Initialize an empty character array named as postfix string.
Page 2 - 8
3. Initialize a stack.
4. Scan the expression character wise from left to right till $ symbol.
a. First character is opening parenthesis, push it into stack.
2
1
top 0 (
b. Second character is an operand 5 add it to the character array
Postfix_string = 5
c. Third character is an operator ‘+’, push it to the stack, check the priority,
the input priority of + is greater than stack priority of (. So place + at the
top.
2
top 1 +
0 (
2
1
0
Top -1
Page 2 - 9
f. Sixth character is an operator * so push it to the stack. Since the stack is
empty no need to check for priority.
2
1
top 0 *
h. Eighth character is an operator – check for input priority, its input priority
is less than stack priority of top element * in the stack so popup operator
and add it to the character array and push the operator – into the stack.
postfix_string = 5 3 + 3 *
2
1
top 0 -
postfix_string = 5 3 + 3 * 2
j. Tenth character is $. so the scanning of character is over. But stack is not
empty so pop up all the operators from the stack and add it to the character
array.
postfix_string = 5 3 + 3 * 2 -
k. The character array contains the postfix expression for the given infix
expression.
Page 2 - 10
polish(IE,PE) // IE Infix Expression and with $
PE Postfix Expression
{
declare stack(n); // Intialise stack.
PE = null // Intialise character array
top = -1 // Now stack is empty.
x=nexttoken(IE); // Extract a character from infix expression till x ≠ $
while(x! = $)
{
switch(x)
{
x is an operand:
PE = PE + operand //add operand to character array
x = “(“ :
top = top + 1
stack[top] = “(”; //push into stack
x=”)”:
while(stack[top]!= “(”) // Remove operators from the
{ //stack and add it to the
PE = PE + stack[top]; //character array
top = top – 1;
}
top = top -1 // delete ‘(’ from the stack.
x is an operator :
Push the operator into the stack as per the priority
If the input operator is of less priority pop the stack and add
it to PE until the condition is satisfied
}
X = nexttoken(IE);
}
while(top>-1)
{ PE=PE+stack[top]
Page 2 - 11
Top=top-1
}
}
Example:
A = 53+3*2-
The infix form is A = (5+3)*3-2
A = 8*3-2
A = 24-2
A = 22
Step:
i. Initialize stack
2
1
0
Top -1
Page 2 - 12
iii. Scan the expression from left to right character wise
a. The first character is 5. push it into the stack.
2
1
top 0 5
2
top 1 3
0 5
c. Third character is +. It is an binary operator. So pop up two operands 5 and 3
from the stack and do the operator 5+3 and push the result 8 into stack.
2
1
top 0 8
d. Fourth character is 3. Push it into the stack.
2
top 1 3
0 8
e. Fifth character is *, It is an binary operator. So pop up two operands 8 and 3 from the
stack and perform the operation 8 * 3 and push the result into the stack.
Page 2 - 13
2
1
0 24
2
1 2
0 24
Procedure :
Eval (PE, value) // PE is postfix expression end with $
{
stack [ N ] // Initialise stack
x = next_token (PE);
while ( x != $ )
{
switch (x )
{
x is an operand :
top = top + 1;
stack [ top ] = x; // place the operand in the stack
x is an operator :
a = stack[top]; // pop second operand
Page 2 - 14
top = top -1;
b = stack [top]; // pop first operand
value = b operator a
stack[top] = value; // place result in stack
}
x = nexttoken (PE);
}
value = stack[top]
}
2.2 RECURSION
DEFINITION:
Recursion is a process in which a function calls itself as a subroutine. This
allows the function to be repeated several times, since it calls itself during its execution.
Functions that incorporate recursion are called recursive functions.
Factorial Function
The product of the positive integers from 1 to n, inclusive, is called "n factorial"
and is usually denoted by n!: n! = 1 . 2 . 3 . . . (n - 2) (n - 1) n. It is also defined that 0! = l.
Thus we have,
5! = 1 . 2 . 3 . 4 . 5 = 120 and 6! = 1 . 2 . 3 . 4 . 5 . 6 = 720
Page 2 - 15
This is true for every positive integer n; that is, n! = n . (n - 1)! Accordingly, the
factorial function may also be defined as follows:
The following procedure calculates n! and returns the value in the variable FACT.
The above procedure is a recursive procedure, since it contains a call statement to itself.
Page 2 - 16
GCD FUNCTION
The GCD of two integers is the largest integer that can exactly divide
both numbers (without a remainder).
GCD Example
Step 2: Find the greatest number that these two lists share in common. In this example
the GCD is 9.
Base criteria − There must be at least one base criteria or condition, such that,
when this condition is met the function stops calling itself recursively.
Progressive approach − The recursive calls should progress in such a way that
each time a recursive call is made it comes closer to the base criteria.
Page 2 - 17
ADVANTAGES OF RECURSIVE FUNCTIONS:
2.3 QUEUE:
A queue is an ordered group of elements in which elements are added at one end
known as the rear end and elements are removed from the other end known as front end.
The element which is stored first in the queue is removed as first element from the
queue. Therefore queue is commonly known as FIFO or First In First Out.
1 2 3 4
Front rear
Implementation of Queue:
A queue is a linear list of elements in which deletions can take place only at one end ,
called the front and insertions can take place only at the other end, called the rear. The
terms front and rear are used in describing a linear list only when it is implemented as a
queue. Queues are also called first in first out (FIFO) lists.
Representation
Page 2 - 18
We use two pointers FRONT and REAR. FRONT containing the location of front
element of the queue and REAR containing the location of rear element of the
queue.
The condition FRONT =NULL will indicate that the queue is empty.
Whenever an element is deleted from the queue, the value of FRONT is increased
by 1. FRONT= FRONT+1
Whenever an element is added to the queue, REAR is increased by 1.
REAR=REAR+1
Storing of an ITEM can be implemented as QUEUE[REAR]=ITEM
Whenever queue is containing only one element then FRONT=REAR ≠ NULL
and suppose after this scenario element is deleted: FRONT=NULL and
REAR=NULL which indicate that queue is empty.
The Queue can be represented using the array. There are two variables front and
rear to indicate the positions of first and last element of the queue within the array.
The queue is empty when front = 0
Array Index
4
3
2
Front = 0, rear =0
1
Page 2 - 19
Note : Since the element can be removed only through front end the elements 7,9 are
removed and front is changed to array index 3.
5
Add one element 13 to the queue. 4 13
3 11
front =3,rear=4 2
1
Basic operation:
Insert means to insert element in the end of the queue. To insert an element we
increment rear. Then insert an element at the position rear.
Delete means to delete an element from the beginning of the queue. Get the
element from the position front. Then increment front.
i. Insert:
Procedure
The following steps followed to add a new element into the queue
i. If rear =N, queue is full. So addition is not possible.
ii. If rear ≠ N queue is not full . So addition is possible.
iii. Increment rear by one as rear=rear+1
iv. Place the new item in queue[rear] = item
Program:
Insert(Q,rear,N,Item)
{
if(rear=N) then
{
Printf(“queue is full”)
}
else
{ if(front=0) then
front=rear=1
else
rear = rear+1
Q[rear]= item
}
}
Page 2 - 20
ii.Delete:
Procedure:
The following steps followed to add a new element into the queue
i. If front=0, queue is empty. So deletion is not possible.
ii. If front ≠ 0 queue is not empty . So deletion is possible.
iii. Place the new item in item = queue[front]
iv. Increment rear by one as front=front+1
Program
Delete(Q,front,N,Item)
{
if( front=0) then
{
Printf(“queue is empty”)
}
else
{
item = queue[front]
if (front=n-1) then
front=rear=0
else
front=front+1
}
}
Circular Queue:
Circular Queue is a linear data structure in which the operations are performed
based on FIFO (First In First Out) principle and the last position is connected back to the
first position to make a circle.
Circular queue is a queue in which the pointers rear and front varies in a circular
manner. The initial condition for the circular queue is front=rear=0 or null.
Page 2 - 21
Implementation of Circular Queue
To implement a circular queue data structure using array, we first perform the following
steps before we implement actual operations.
Step 1: Create a one dimensional array with above defined SIZE (int
cQueue[SIZE])
Step 2: Define two integer variables 'front' and 'rear' and initialize both with ‘0'.
(int front = 0, rear = 0)
Step 3: Implement main method by displaying menu of operations list and make
suitable function calls to perform operation selected by the user on circular queue.
Page 2 - 22
Step 4: Set queue[rear] = value and check 'front == -1' if it is TRUE, then set
front = 0.
Page 2 - 23
Dequeue:
A double ended queue is known as dequeue. In this the insertion and deletion can take place
from both the sides. There are two types of dequeue. They are
1. Input restricted queue.
2. Output restricted queue.
The output restricted dequeue allows deletions from only one end and input restricted dequeue
allows insertions at only one end.
Deletion
Insertion
Priority Queues:
Priority queue is an ordered collection of elements are placed according to the defined
priority. Insertions and deletions are done according to this priority.
Array Representation:
Let N be the number of priority levels assigned to the elements. Let M be the
maximum number of elements with same priority. Then each priority level can be represented in
memory as a separate circular queue. So we need N circular queues of size M. This can be
represented as a 2D array named CQ of size N X M. In this, each row represents a circular queue
of size M. In addition to this we are in need 1-D array of size N to hold the front and rear pointers
to the circular queues.The following figure shows the representation of priority queue in memory
1 2 M-1 M
1
2
Page 2 - 24
front rear
1 1
2 2
N N
Example:
Consider the following priority queue for getting the library book. This has 3
priority levels and each level contains maximum of 4 members.
Level1 - HOD
Level2 - Staff
Level3 - Student
Front rear
1 1 1 2
2 1 2 3
3 1 3 4
The level1 members are placed in the first row, level2 members are placed in the second
row and level3 members are placed in the third row.
Page 2 - 25
Review Question
PART A
1. Define Stack?
2. State the condition for empty stack in array implementation?
3. What is recursive function?
4. What is the property of recursive function?
5. Define queue.
6. State difference between queue and circular queue.
PART B
1. Write down the algorithm for push operation.
2. What is reverse polish notation? Explain with example.
3. Write down the algorithm to find factorial of given number
4. Write algorithm to find gcd of two natural numbers
5. Explain priority queue.
PART C
1. Explain array implementation of stack.
2. Explain algorithm to convert infix expression to postfix expression
3. Explain algorithm to evaluate postfix expression
4. Explain algorithm to do basic operation in queue
5. Explain algorithm to perform PUSH and POP operation
Page 2 - 26
UNIT 3 LINKED LISTS
Objectives
3.1.3 To study the differences between sequential list and Linked list
3.1Terminologies:
Node:
Linked list is an ordered collection of elements. Each element is called a node. Each
node contains two fields
i.Data field
ii.Link field
Data field contains the actual data and the link field contains the address of the next
node(element).
Node structure
P
Data link
Fig 3.1
P=pointer
Address:
It is stored in the link field of the node(next node address), except last node. The
address of the linked list are divided into:
i.External address
ii.Internal address
iii.Null address
1
External address is the address of the first node in the list and is assigned to a pointer
variable. With the help of this variable we can access the entire linked list.
Internal addresses are addresses stored in the link field of the nodes(next node
address)except last node.
Null address is the address stored in the last node of the list and it indicates the end of the list.
Pointer
A pointer is a variable whose value is the address of another variable, i.e., direct
address of the memory location.Linked list is a linear collection of data elements, called nodes,
each pointing to the next node by means of a pointer called link
Information
The actual data stored in the data field of the linked list.
Null Pointer
Null pointer is a special reserved value of a pointer. A pointer of any type has such a
reserved value. Formally, each specific pointer type ( int * , char * etc.) has its own
dedicated null-pointer value. Conceptually, when a pointer has that null value it is not pointing
anywhere.
Empty List
An empty list is a list that contains no data records. In other words it is a linked list with zero
node.
3.1.1Types of linked List
The way to represent a linear list is to expand each node to contain a link or
pointer to the next node. This representation is called a one-way chain or singly linked
list.
i.Data field
ii.Link field
2
Data field contains the actual data and the link field contains the address of the next
node(element). The first node address is called the pointer to the linked list.The last node link
field contains null address.
P
5 10 15 NULL
Fig 3.2
A doubly linked list is a linked list in which each node is divided into three fields
Node structure
Fig 3.3
A linked list in which the last node’s link field contains the address of the first node is
called circular linked list.
Node structure
A B C D
Fig3.4
As shown in the above figure,the last node does not contain a NULL pointer,but it points to the
first node.Therefore circular linked list is also called a list without beginning and end.
3
3.1.2 Representation of linked list
Implementation is defined as the process of representing the linked list in memory using
computer languages .Linked list can be implemented by any one of the following methods.
ARRAY IMPLEMENTATION
In array implementation the variables used in the implementation are declared while writing the
program therefore the memory space for the variables will be allocated during compilation and
remain unchanged (static)as long as program is running
Implementation
struct N
datatype data;
int link ;
struct N node[MAXNODES];
Where
struct - keyword
N - structure name
Data type - valid data type such as int ,float, char etc….
4
- - -------------------
- --------------------
Example
Let LIST is linear linked list. It needs two linear arrays for memory representation. Let these
linear arrays are INFO and LINK. INFO[K] contains the information part and LINK[K] contains
the next pointer field of node K. A variable START is used to store the location of the beginning
of the LIST and NULL is used as next pointer sentinel which indicates the end of LIST. It is
shown below:
Fig 3,5
A linear linked list is a chain of structures where each node points to the next node to
create a list. To keep track of the starting node's address a dedicated pointer (referred as start
5
pointer) is used. The end of the list is indicated by a NULL pointer. In order to create a linked list
of integers, we define each of its element (referred as node) using the following declaration.
struct node_type {
int data;
struct node_type *next;
};
struct node_type *start = NULL;
Access In array, elements can be accessed using In linked list, elements can’t be
index/subscript value, i.e. elements can be accessed randomly but can be
randomly accessed like arr[0], arr[3], etc. So accessed sequentially
array provides fast and random access.
Memory In array, elements are stored In linked list, elements can be stored
Structure in consecutive manner in memory. at any available place as address of
node is stored in previous node.
Insertion & Insertion & deletion takes more time Insertion & deletion are fast & easy
Deletion
Types Array can be single dimensional, two Linked list can be singly, doubly or
dimension or multidimensional. circular linked list.
6
location. previous element/node.
Extra Space No need of extra space in memory for extra memory space is needed.
in memory pointer.
Figure
Fig 3.6
3.1.4 Advantages
Disadvantages
We can perform various operations on linked list. The most common operations are:
Traversing the list to access all elements (e.g., to print them, or toperform some
specific operation);
Searching a node to modify it or to obtain the information in it;
Inserting a new node in a SLL (e.g., front, middle ,or end )
Deleting a node from a SLL (e.g., front, middle, or rear);
7
ALGORITHMS FOR LIST OPERATIONS
. Traversing linked list means visiting each and every node of the Singly linked list.
Following steps are involved while traversing the singly linked list –
i)Check whether the linked list pointer P is NULL or not.
iv)Fetch the data from the node and perform the operations depending on data type.
b) If not , advance pointer to next node and perform operation on visited node
v) Go to step iv .
Searching is a process to search the first occurrence of the given item in the list and to
return the address of the node containing the item.
a)If equal searching over.The search item is present in the first node.Return P.
b)if not equal find the next node address as ptr= p-> link
iv) Check ptr. If not null compare search item with ptr -> data
b)if not equal find the next node address as ptr= ptr-> link
8
3.1.5.3 Insertion into a linked list
Insertion is a process to add a new element in the linked list. The new element is
inserted either at first or middle or last position.
i) Get a free node x from the memory using the function getnode[]. That is x =getnode[].
ii) Copy the data item into the data field and the linked list pointer value p [address] into the
link field.
iii) change the linked list pointer value [address] p with the address of the new node x.
Example
Consider a linked list pointer value [address] of the list as shown below
P=address of A
P
A C H NULL
To insert a new data B into the list, get a new node, using getnode[] function. Let x be the
address of the new node and it is shown below.
9
To insert this as a first node, the link field of x is filled by the pointer p [address of A] of the given
list and then pointer [address] p is changed by x [address of the new node] as shown below.
P=x
B A C H NULL
Fig 3.8
NULL
b)Insertion after a given node address y NULL
The steps to be followed are
i) Get a fee node x from the memory using the function getnode(). That is x=
getnode().
ii) Copy the data item into the data field and copy the link value of given node y into
the link field of the new node x.
Example
Consider a linked list with pointer p which contains the address of the list as shown
below
P=address of A
P
A C H NULL
To insert a new data B into the list,get a new node, using getnode() function. Let x be the
address of the new node and it is shown below.
10
To the data field store the data B and is shown below
The link field of new node x is filled by the link field of y and link field of y is changed by x
(i.e. address of the new node) and is shown below.
P=address of A
P
A C H null
B
NUL
Fig 3.10 L
NUL
L
c)Insertion at the end of the list
i)Find the address t of the last node in the list by using the address of the linked list p.
Last node is a node with NULL link field.
ii)Get a free node from the memory using the function getnode(). That is x= getnode().
ii)Copy the data item into the data field and NULL in the link field of the new node x.
iv)Change the link value of the last node t by the address of the new node x.
Example
Consider a linked list with pointer p which contains the address of the list as
shown below
P=address of A
P
A C H NULL
Fig 3.11
NULL
NULL
11
To insert a new data B into the list, get a new node, using getnode() function. Let x be
the address of the new node and it is shown below.
To insert this as a last node, the link field of x is filled by NULL and the link field of the
last node in the list is filled by the address of the new node x as shown below.
P=address of A
P t x
A C H B NULL
Deletion is a process to remove an existing node from the linked list. The node to
be deleted may be the first node,or last node or intermediate node.
12
Example
Consider a linked list with pointer P which contains the address of the list as
P=address of A
P
A C H NULL
Fig 3.12
NULL
NULL
To delete the first node ,change the list address P by the second node address
and it is shown below
P=address of C
A C H NULL
Fig 3.13
NULL
NULL
b)Deleting the given node
Let q be the address of the node to be deleted and r be the address of the
previous node.
Example
Consider a linked list with pointer P which contains the address of the list as
P=address of A
P
A C H NULL
Fig 3.14
NULL
NULL
13
To delete the given node q, ,change the link field r of the preceding node of the
node to be deleted by the address stored in the link field of the node q as shown below
r q
A C H NULL
Fig 3.15
NULL
now node q will be deleted
NULL
c)Deleting the last node of list
1. find the number of nodes n in the list by using the address of the
Linked list p.
2. find the address k of the previous node of the last node using the
Number of nodes n .
Example
Consider a linked list with pointer p which contains the address of the
P=address of A
P
A C H NULL
Fig 3.16
NULL
To delete the last node change the link field of the node with address k to NULL
NULL
as shown below.
P=address of A
P k
A C NULL H NULL
N= 1 2 3
NULL
NULL
Fig 3.17
14
3.1.6 Doubly Linked List
A linked list which can be traversed both in backward as well as forward direction is
called doubly linked list. It uses set of pointers.
A doubly linked list is a linked list in which each node is divided into three fields
Node structure
Using the pointer First ,we can traverse the list in the forward direction and using the pointer
last, we can traverse the list in the backward direction.The figure given below shows a two
way list
First Last
null null
Fig 3.18
Declaration
struct node
datatype data;
};
Where
struct - keyword
15
Node - structure name
(i) data - variable to store the data. This can be any valid data type.
(ii) plink - to store the address of the previous node containing the previous data
(iii) slink - to store the address of the next node containing the next data
The plink and slink members are pointer members which points the same structure
,therefore this structure is called self referential structure.
A linked list in which the last node’s link field contains the address of the first node is
called circular linked list.
Node structure
A B C D
Fig 3.19
As shown in the above figure,the last node does not contain a NULL pointer,
but it points to the first node.Therefore circular linked list is also called a list without
In the circular linked list we can insert elements anywhere in the list whereas in the array we
cannot insert element anywhere in the list because it is in the contiguous memory. In the circular
linked list the previous element stores the address of the next element and the last element
stores the address of the starting element.
Advantages
(i) There is no null reference in the nodes.This is useful for certain applications like
playing video/audio repeatedly.
(ii) From any node we can go to any node.But in linked list it is not possible to go to the
previous node.
(iii) Moving from last node to first node can be done in a single step.But in DLL one has
to traverse from the last node to the first node.
Disadvantages
16
Review Questions
Part A
Part B
Part C
17
7.. Explain How to insert a node as a last node in a list?
18
4.1.TREES
Objectives
4.1.1.Terminologies
a)Degree of a node
In a tree data structure, the total number of children of a node is called as DEGREE of
that node. In simple words, the Degree of a node is total number of children it has.
b)Degree of a tree
The highest degree of a node among all the nodes in a tree is called as 'Degree of
Tree'. Here Degree of tree is 3.
c)Level of a node
In a tree data structure, the root node is said to be at Level 0 and the children of root
node are at Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so
Page 4.1 - 1
on. In simple words, in a tree each step from top to bottom is called as a Level and the Level
count starts with '0' and incremented by one at each level (Step).
d)Leaf node
In a tree data structure, the node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes. External node is also
a node with no child. In a tree, leaf node is also called as 'Terminal' node.
e)Depth of a tree
In a tree data structure, the total number of egdes from root node to a particular node is called
as DEPTH of that node. In a tree, the total number of edges from root node to a leaf node in the
longest path is said to be Depth of the tree. In simple words, the highest depth of any leaf node
in a tree is said to be depth of that tree. In a tree, depth of the root node is '0'.
Page 4.1 - 2
f)Height of a tree
In a tree data structure, the total number of egdes from leaf node to a particular node in the
longest path is called as HEIGHT of that Node. In a tree, height of the root node is said to
be height of the tree. In a tree, height of all leaf nodes is '0'.
Indegree
Outdegree
h)Path
Page 4.1 - 3
In a tree data structure, the sequence of Nodes and Edges from one node to another node is
called as PATH between that two Nodes. Length of a Path is total number of nodes in that
path. In below example the path A - B - E - J has length 4.
Ancestors of a node are all the nodes along the path from the root to that node.
H is the descendant of A
Ancestor: The predecessor of a node together with all the ancestors of the
predecessor of a node. The root node has no ancestors.
Descendant: The children of a node together with all the descendants of the
children of a node. A leaf node has no descendants.
j)siblings
In a tree data structure, nodes which belong to same parent are called as SIBLINGS. In simple
words, the nodes with same parent are called as sibling nodes.
Page 4.1 - 4
4.1.2Types of Trees
4.1,2.1Binary tree
Binary tree is a tree in which each node has at most two children called left and
right . [OR]
A binary tree is a finite set of element which are either empty or consist of a
root and two disjoint sub trees called left sub tree and right sub tree .
Each element in the binary tree is called the node of the binary tree. A left or
right or both sub trees can be empty. A binary tree is shown in the figure.
A
Root node Level 0
A
B C
Level 1
Level 2
D E F G
This tree contains 7 nodes in which A is root node. Left sub tree of A is rooted at
B and right sub tree of A is rooted at C. In other words A is the father of B and
C or B and C are left and right child of A.
Note
Page 4.1 - 5
Representation of binary tree
Let T be a binary tree we can represent this in memory using any one of the
following methods.
5 7 -- -- 9 -- --
7
0 1 2 3 4 5 6
9
The height of the tree is 3 . therefore to store the above binary tree we are in need
of an array of size 23 -1=7 and it is shown above.
The location 2,3,5,6 are left free because there are no nodes corresponding to these
locations.
DATA field contains information.LEFT field holds the address of left child node and
RIGHT field holds the address of right child node. If there are no children,the address fields
hold NULL value.The first node address is called Pointer of the tree.
Page 4.1 - 6
Using this address only we can access the tree.Consider the tree given below
Root node
A
Level 0
A
B C
Level 1
Level 2
D E
ROOT
Root
Null B null C
Binary tree traversal is defined as a process of visiting all the nodes in the binary
tree once . The visit always start from the root node we visit the node to do any one
of the following
Page 4.1 - 7
3 To move to the right of that node It is denoted by letter R
There are three types of traversal they are
(a) Inorder traversal
(b) Preorder traversal
(c) Post order traversal
This traversal is denoted by the letters LVR . The steps to be followed are
(a) Start from the root node and move left until there is no left child
Then visit the last node (print the data)
(b) Move right and move left until there is no left child Then visit the
last node
(c) If it is possible to move right go back one node visit the node
(print the data) and do step(b).This traversal is called
inorder traversal.
Example
A
Root node Level 0
A
B C
Level 1
Level 2
D E
Steps
i)Start from the root node A and move left till last node that is upto node B and print the data B.
iii)Then move right to node C and move left till last node that is upto D ,print the data D.
v) Then move right to node E since no path to move .print the data E.
This traversal is denoted by the letter VLR .The steps to be followed are
(a) Start from the root node and visit this node (print the data)
(b) Move left , print the data and this process is continued till there is no
path to move.
Page 4.1 - 8
(c) Move right print the data and move left , print the data and this
process is continued till there is no path to move.
(d) Move one node back and do step (c)
Example
A
Root node Level 0
A
B C
Level 1
Level 2
D E
Steps
iii)Since there is no node to move left or right move one node back that is to node A
iv)From this node move right to node C and print the data C and move left to node D and print
data D.
v) From node D there is no node to move left or right so move one node back to C and move
right to node E and print the data E
(a)Start from the root node and move left until no left node to
move and print the data
(b)Move right and then move left until there is no left node and print
the data
Page 4.1 - 9
Example
A
Root node Level 0
A
B C
Level 1
Level 2
D E
Steps
i)Start from the root node A and move left until no left node to move that is to node B and print
the data B
ii) Then go back to node A and move right to node C and move left until no left node to move
that is to node D and print the data D
iii)Then go back to node C and move right to node E.Since no left or right to move print the
data E
iv) Move one node back to node C print the data C and move one node back to node A and
print the data A.
4.1.4.Expression tree
Expression tree is a binary tree in which each internal node corresponds to operator and
each leaf node corresponds to operand such as constants or variable names.
It is possible for nodes to have more than two children. It is also possible for a node to have
only one child, as is the case with the unary minus operator.
An expression tree, T, can be evaluated by applying the operator at the root to the values
obtained by recursively evaluating the left and right sub trees.[
Page 4.1 - 10
4.1.5Binary search tree
Definition
1. All elements in the left sub tree of a node n should be less than the value
of the node n.
2. All elements in the right sub tree of a node n should be greater than the
All keys in n's left subtree are less than the key in n, and
all keys in n's right subtree are greater than the key in n.
Here are some BSTs in which each node just stores an integer key:
Page 4.1 - 11
These are not BSTs:
In the left one 5 is not greater than 6. In the right one 6 is not greater than 7.
The reason binary-search trees are important is that the following operations can be
implemented efficiently using a BST:
4.1.6 Creation of binary search tree for the given set of data .
The steps given below are followed to create a binary search tree for the
1. Get the first data say d0 and place it as a root node data with empty left
ROOT
Null d0 Null
2. Get the next data in the list and compare it with the root value.
(a) If the data value is less do step (b) else if else data value is greater do
step (c) .
(b) Check if left child is NULL or not . if NULL place the data value as
The new left child.
Else
Compare it with the existing left child data value and do step (a).
(c) Check if right child is NULL or not . if NULL place the data value as
The new right child .
Else
Page 4.1 - 12
Compare it whith the existing right child data value and do step (a)
Example
Steps
1. Get a free node by calling the function getnode () and store the first
Data 25 in the data field and NULL in the lchild and rchild field as.
Null 25 Null
2. The next data is 10. This is compared with root and is less (10< 25).
Since left child is empty place it as the left child of root with Null value
In the lchild and rchild field as.
Root
25 Null
Null 10 Null
3. The next data is 40. Compare it with the root it is greater . since right
child is empty place it as the right child of root with Null value in the
lchild and rchild field as.
Root
25
Page 4.1 - 13
4. The next data is 17 . compare it with the root, it is less . since left child
of root is not empty compare it with the value of the left child 10,
It is greater. Since right child is empty place it as the right child as
Root
25
Null 17 Null
5. The next data is 24 . compare it with the root, it is less . since left child
of root is not empty compare it with the value of the left child 10,
It is greater. Since right child of node 10 is not empty,compare it with the right
child value 17,it is greater. Since right child of node 17 is null,place it as the right
child as
Root
25
Null 17
Null 24 Null
Page 4.1 - 14
Review questions
Part A
1.Define tree
2.What is node?
3.What do you mean by root?
4.What is sibling?
5.Define level?
6.Define height?
7.what is depth of a tree?
8.What is degree of a tree?
9.What is binary tree?
10.What is path?
Part B
1.What is indegree and out degree ?
2.Define ancestor and descendant nodes
3.What are the different types of traversal?
4.Define binary search tree Give example
5.what is expression tree .Give example.
Part C
1Explain any five basic terminologies of tree
2.Explain i)binary tree ii) Expression tree
3.Explain list representation of binary tree with example
4.Explain inorder traversal of binary tree with example
5 Explain preorder traversal of binary tree with example
6.Explain post order traversal of binary tree with example
7.Explain how to create a binary search tree with example
.
Page 4.1 - 15
OBJECTIVES
To understand graph terminologies
To know about the different ways of traversing a graph.
4.2 Graph:
A graph is defined as a set of nodes or vertices and a set of lines or edges
that connect the two vertices. The set of vertices is specified by listing the vertices as in a
set and the set of edges is specified as a sequence of edges.
v1
e1 e2
v2 v4 e5 v5
e3 e4
v3
v1, v2, v3, v4, v5 Nodes
e1, e2, e3, e4, e5 edges
A graph is denoted as G=(V,E), Where
V= {v1, v2, v3, v4, v5}
E={e1, e2, e3, e4, e5}
Graph Terminologies:
v1 node
e1 e2
v2 v3 e5 v5
e3 e4 edge
v4
Node(Vertices) :
Each element of a graph is called node of the graph.
Page 4.2 - 1
Edge (Arcs) :
Line joining two nodes is called edge. It is denoted by e=[u,v] where u and v are
adjacent nodes.
Example
e1 =[v1,v2]
Directed Graph
A graph G is called directed graph if each edge has a direction. Each edge is
denoted as an ordered pair of vertices e = (u,v) ≠ (v,u). The directed edges are called
axis.
v1
e1 e2
v4 e5
v2 v5
e3 e4
v3
Here e1 =(v1,v2) e2= (v4,v1) e3 =(v3,v2) e4=(v4,v3) e5 =(v4,v5)
e(u,v) => u Initial point
v Terminal point
u and v are adjacent vertex
Here e(v1,v2) ≠ e(v2,v1) because of direction .
Degree of a node:
Degree of a node is the number of edges connecting the node in the graph. It is
denoted as deg(v) where v is a node.
V1 v6 .
e1 e2
v2 v4 e5 v5
e3 e4
v3
Page 4.2 - 2
deg(v1)=2 , deg(v2)=2 , deg(v3)=2 ,deg(v4)=3, deg(v5)=1, deg(v6)=0
Indegree :
The indegree of a vertex is the number of edges pointing to that vertex.i.e.
number of edges having the vertex as terminal point.
Outdegree :
The outdegree of a vertex is the number of edges pointing from that vertex. i.e.
number of edges having the vertex as initial point.
Example :
v1
e1 e2
v4 e5
v2 v5
e3 e4
v3
indegree(v1) = 1 outdegree(v1) = 1
indegree(v2) = 2 outdegree(v2) = 0
indegree(v3) = 1 outdegree(v3) = 1
indegree(v4) = 0 outdegree(v4) = 3
indegree(v5) = 1 outdegree(v5) = 0
Adjacent or neighbours:
Two nodes u and v are adjacent if there is an edge between u and v . (i.e) e=[u,v]
Example:
v1 and v2 are adjacent since there is an edge e1 between them.
v1 and v3 are not adjacent since there is no edge between them.
Page 4.2 - 3
Successor and Predecessor:
If a node is reachable from another node, then the first node is a predecessor of
second one and second node is a successor of first one. If there is an arc from first node
to second node, then first node is a direct predecessor of second one, and second node is a
direct successor of first one.
Example :
v1
e1 e2
v4 e5
v2 v5
e3 e4
v3
Successor of v1 : v2,v4
Direct Successor of v1 : v2
Predecessor of v4 :v2,v1
Direct Predecessor of v4 :v2
Path :
A path is an alternate sequence of nodes and edges . The uninterrupted sequence
of edges from starting vertex and ending vertex
V1
e1 e4
v2 e6 v4 e5 v5
e2 e3
v3
The different paths are p1=(v1,v2,v3) , p2=(v1,v4,v3)
Page 4.2 - 4
Length of the Path :
The length of the path is the number of edges in the path. In other words, number
of vertices – 1
Example :
p1=(v1,v2,v3)
Length(p1) = 3 -1 = 2
Simple Path :
A path is simple, if all nodes are distinct except the first and last.
Example :
p1=(v1,v2,v3) Simle path
P2=(v1,v4,v5,v4,v3) Not simple path because vertex v4 is repeated.
Weighted Graph:
The edge of a graph may carry a value which may represent some important
information. Such a graph is called weighted graph.
Example:
The following graph represents various bus stops and the edges indicates that the
bus stops are connected through a bus route. The weights attached edges represent the
distance in kilometers between pair of bus stops.
V1
e1 e2
5 3 V4 e5
V2 2 V5
e3 8 9 e4
V3
W(e1) = 5 W(e2) = 3 W(e3) = 8 W(e4) = 9 W(e5) = 2
Traversal of Graphs:
Depth First Search algorithm:
DFS traverses a graph in a depth ward motion and uses a stack to remember to get the
next vertex to start a search when a dead end occurs in any iteration.
Page 4.2 - 5
As in example given above, DFS algorithm traverses from A to B to C to D first
then to E, then to F and lastly to G. It employs following rules.
Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display it. Push it in a
stack.
Rule 2 − If no adjacent vertex found, pop up a vertex from stack. (It will pop up
all the vertices from the stack which do not have adjacent vertices.)
Rule 3 − Repeat Rule 1 and Rule 2 until stack is empty.
Page 4.2 - 6
2. Mark S as visited and put it
onto the stack. Explore any
unvisited adjacent node from
S. We have three nodes and we
can pick any of them. For this
example, we shall take the
node in alphabetical order.
Page 4.2 - 7
6. We check stack top for return
to previous node and check if
it has any unvisited nodes.
Here, we find D to be on the
top of stack.
As C does not have any unvisited adjacent node so we keep popping the stack
until we find a node which has unvisited adjacent node. In this case, there's none and we
keep popping until stack is empty.
Page 4.2 - 8
As in example given above, BFS algorithm traverses from A to B to E to F first then to C
and G lastly to D. It employs following rules.
Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display it. Insert it in a
queue.
Rule 2 − If no adjacent vertex found, remove the first vertex from queue.
Rule 3 − Repeat Rule 1 and Rule 2 until queue is empty.
Page 4.2 - 9
Next, the unvisited adjacent node
4. from S is B. We mark it as visited
and enqueue it.
At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we
keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied, the
program is over.
Page 4.2 - 10
Applications of Graph
Nodes could represent positions in a board game, and edges the moves that
transform one position into another ..
Connecting with friends on social media, where each user is a vertex, and when
users connect they create an edge.
Nodes could represent computers (or routers) in a network and weighted edges the
bandwidth between them
Street Map
Streets are one- or two-way.
• A single directed edge denotes a one-way street
• A two directed edge denotes a two-way street
Page 4.2 - 11
• nodes could represent towns and weighted edges road distances between them, or train
journey times or ticket prices ...
Representations of a graph:
Representation of graph is a process to store the graph data into the computer
memory.
i. Set Representation
ii. Sequential Representation
iii. Linked list Representation
i. Sequential Representation:
In this representation , the graph G is represented in
a matrix form using the following methods.
i. Adjacency matrix
ii. Path matrix
Adjacency matrix:
Let G be a simple directed graph with n nodes . The nodes are v1,v2,v3,. . . , vn
then the adjacency matrix A= (aij) of order n x n
i,j = 1,2,3, . . . ,n
Page 4.2 - 12
This matrix is also called bit matrix or boolean matrix because all entries
of matrix are 0 or 1
v2
v1 v3
v4
G = (V,E)
V = {v1,v2,v3,v4}
E = {(v1,v2), (v2,v3), (v3,v3), (v3,v4), (v4,v3)}
Adjacency matrix:
v1 v2 v3 v4
v1 0 1 0 0
v2 0 0 1 0
v3 0 0 1 1
v4 0 0 1 0
The number of vertices is 4, then the adjacency matrix A is size 4 x 4. Since there
are 5 edges in the edge set , the matrix contains five entries of 1.
ii. Linked list Representation:
In this representation the graph G is represented as a
collection of data nodes and head nodes. Each node contains two fields called data and
link. Data field contains information and the link field contains the address of the next
adjacent node. This is called Linked List Representation
Example:
V1 V2
V3 V4
Page 4.2 - 13
Head Node:
V1 V2 null
V2 V1 V3 V4 null
V3 V2 V4 Null
V4 V2 V3 null
Review Question
PART A
1. Define directed graph?
2. Define path?
3. Define length of a graph?
4. Expand DFS and BFS.
PART B
1. Write short notes on the following
a) in-degree b) out-degree c) edge
2. Write down the application of the graph
3. Draw BFS graph with 8 node.
4. Draw DFS graph with 8 node.
5. Explain Set representation of graph.
PART C
1. Explain DFS with example
2. Explain BFS with algorithm.
3. Explain Adjacency List Representation of graph.
4. Explain Matrix representation of graph.
5. Write down the comparison of graph representation.
Page 4.2 - 14
Unit 5 : Sorting, Searching & Hashing
Objectives
To define sorting and searching
To list types of sorting and searching
To explain the difference between different methods of sorting and searching
To understand algorithms of various sorting and searching methods
To choose the appropriate method of sorting and searching
To write program for selection, insertion and bubble sort and linear and binary
searching
To define hashing
To understand hash table
To use appropriate hashing technique
To resolve collision
5.1 Sorting
Introduction
Sorting is a fundamental operation in Computer. Sorting means arranging the data in
some sequence ie, in increasing order or decreasing order for numerical data or
alphabetically for character data. There are many sorting algorithms available. One may
choose particular algorithm, depends upon the properties of data and the operations he
wants to perform on the data.
Definition
Sorting means arranging the data in some sequence i.e. increasing order or
decreasing order. Sorting is divided into two groups.
Internal sorting
External sorting
Internal sorting means arranging the numbers in order if all data are in primary memory.
External sorting means arranging the numbers in order if data are in secondary memory
also.
Page 5. 1
Pass 2
i. Take a[0] i.e. first element. Compare it with a[1] i.e. second element. If it is not in
order, interchange .
ii. Compare a[1] element with a[2] i.e. third element. If it is not in order, interchange.
iii. This process is repeated till (n-3)th element is compared with (n-2)th element.
iv. At the end of Pass 2 the second biggest element comes to n-2 position
----
----
----
Pass n-1
i. Take a[0] i.e. first element. Compare it with a[1] i.e. second element. If it is not in
order, interchange .
ii. At the end of the Pass n-1, all elements will be arranged.
Example
Consider the following array with 5 elements ( 5 elements need 4 Pass)
Pass 1 ( 4 Comparisions)
a[0] a[1] a[2] a[3] a[4]
38 65 12 27 8
No interchange
38 65 12 27 8
Interchange
38 12 65 27 8
Interchange
38 12 27 65 8
Interchange
38 12 27 8 65
Pass 2 ( 3 Comparisions)
a[0] a[1] a[2] a[3] a[4]
38 12 27 8 65
Interchange
12 38 27 8 65
Interchange
12 27 38 8 65
Interchange
12 27 8 38 65
Page 5. 2
Pass 3 ( 2 Comparisions)
a[0] a[1] a[2] a[3] a[4]
12 27 8 38 65
No Interchange
12 27 8 38 65
Interchange
12 8 27 38 65
Pass 4 ( 1 Comparision)
a[0] a[1] a[2] a[3] a[4]
12 8 27 38 65
Interchange
8 12 27 38 65
At the end of Pass 4, all elements are arranged in ascending order.
C Program:
# include<stdio.h>
#include <conio.h>
void main()
{
int a[100], n,i,j,temp;
clrscr();
printf(“How many elements in array”);
scanf(%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=1;i<=n-1;i++) // n-1 scan for n data
{
for(j=0;j< n-i; j++)
{
if(a[j]>a[j+1]
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf(“\n The Sorted array is”);
for(i=0;i<n; i++)
printf(“%d”,a[i]);
getch();
}
Page 5. 3
5.1.2 Selection Sort
The selection sort technique is based upon selecting the minimum or maximum
value in array. Array is scanned to locate the minimum value, once it is found, it is placed in
the first position of the array (position 0 ). The remaining elements are scanned to find the
second smallest element and is placed in the second position (position 1 ) and so on until
the array is sorted.
Algorithm
This algorithm sorts an array a with n elements.
1. Set k = 0
2. Find the position of minimum value of array from kth location, and mark it as loc
3. Interchange a[k] and a[loc]
4. Set k = k+1; Repeat step 2 & 3 until k<n-1
5. Exit
Example
Consider a = {16,,15,,2,,10, 7}
Pass 4 2 7 10 15 16
C Program
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100], i,j,n,min,loc;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i=0;i<n;i++)
scanf("%d", &a[i]);
for (i=0;i<n-1;i++)
{
Page 5. 4
min = a[i];
loc = i
for (j=i+1;j<n;j++)
{
if ( a[j] < min )
{ min = a[j];
loc = j;
}
}
a[loc] = a[i];
a[i] = min;
}
printf("Sorted list is \n");
for (i=0;i<n;i++)
printf("%d\n", a[i]);
getch();
}
Algorithm
Scan the array a from a[0] to a[n-1] and insert a[r] for r = 0 to n-1 into its proper
position into the previously sorted sub array.ie
Pass 3 : a[2] is inserted into its proper place such that a[0],a[1] & a[2] are in order
Pass 4 : a[3] is inserted into its proper place into the previous sorted sub array
----
----
----
Pass n : a[n-1] is inserted into its proper place so that all elements are in sorted form
Page 5. 5
Example
Consider following set of values to sort
38 22 12 27 8
Pass 2 22 38 12 27 8 22 is inserted
Sorted Subarray
Pass 3 12 22 38 27 8 12 is inserted
Sorted Subarray
Pass 4 12 22 27 38 8 27 is inserted
Sorted Subarray
Pass 5 8 12 22 27 38 8 is inserted
Sorted array
C Program
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100],n,,i,j,k,temp;
clrscr();
printf(“How many number of elements \n”);
scanf (%d”,&n);
printf(“enter elements in the array \n”);
for (i=0; i<n; i++)
scanf(%d”,&a[i]);
for (i=0;i<n;i++)
{
temp=a[i];
j=i;
while((temp< a[j-1]) && (j-1>=0))
j--;
for( k =i ; j<k; k--)
{
a[k]=a[k-1];
}
a[j] = temp;
}
printf(“Sorted Array is \n”);
for(i=0;i<n;i++)
printf(%d\n”,a[i]);
Page 5. 6
getch();
}
Algorithm
Assume the array A has n elements and merge sort uses an auxiliary array B
1. Set L = 1 (Initialize the number of elements in the subarrays)
2. Repeat step 3 to 4 while L<N
3. Merge 2 * L elements and sort them using any sort. (Usually insertion sort)
4. set L = L * 2.
5. Exit.
Example
Consider an array A with n elements a[1] a[2] …. … a[n] is in memory. The merge
sort algorithm sorts the data as described below.
Suppose the array a contains 14 elements as follows
66 33 40 22 55 88 60 11 80 20 50 44 70 30
Pass 1. Merge each pair of elements to obtain the following list of sorted pairs
33 66 22 40 55 88 11 60 20 80 44 50 30 70
Pass 2. Merge each pair of elements to obtain the following list of sorted quadruplets
22 33 40 66 11 55 60 88 20 44 50 80 30 70
Pass 3. Merge each pair of sorted quadruplets to obtain the following list of sorted sub arrays
11 22 33 40 55 60 66 88 20 30 44 50 70 80
Pass 3. Merge two sorted sub arrays to obtain the following single sorted array
11 20 22 30 33 40 44 50 55 60 66 70 80 88
The original array a is now sorted
After pass K, the array a will be partitioned into sorted sub arrays where each sub
array except possibly the last, will contain exactly 2K elements.
Example
Assume the data are : 624 852 426 987 269 146 415 301 730 78 593.
Here the msb position is 100. Hence
During pass 1, the ones or unit place digits are ordered.
During pass 2, the tens place digits are ordered, retaining the relative positions of values set
by the earlier pass.
On pass 3 the hundreds place digits are ordered, again retaining the previous relative
ordering
After three passes the result is an ordered list.
Sequence of values in each Queue during radix sort
Queue Pass 1 Pass 2 Pass 3
0 730 301 78
7 987 78 730
8 78 987 852
Collect all the data Queue wise. We get the following ordered list.
78 146 269 301 415 426 593 624 730 652 987
Page 5. 8
Algorithm
Step 1 − Initialize the value of h
Step 2 − Divide the list into smaller sub-list of equal interval h
Step 3 − Sort these sub-lists using insertion sort
Step 3 − Repeat until complete list is sorted
Example
Principle:
Choose any number in the array and name it as partition element P. For simplicity
take first element a[0] as the partition element.
With respect to the value of P (i.e. a[0]) divide the array into two partitions such that
the number which are less than P are placed in the left side of P and the numbers
which are greater than P are placed in the right side of P.
Page 5. 9
Suppose if the partition element is placed in the jth position, the following
conditions are satisfied.
a) Each number in the position 0 through j -1 is less than or equal to P(i.e.
a[0]).
b) Each number in the position j + 1 through n -1 is greater than or equal to
P.
c) The partition element P fixes its sorted position.
The above steps are repeated in the left partition a[0] through a[j-1] and right partition
a[j+1] through a[n-1].
Algorithm:
i. Initialize two pointers low and high. At the beginning low = 0 and high = n-1.
ii. Fix the partition element P = a[0].
iii. Scan the array from left to right and compare P with 2nd element i.e. a[1]. If it
is greater than P, stop scanning and keep the location of the higher element
in low. Else, compare next element and so on.
iv. Scan the array from right to left and compare P with the last element i.e.
a[n-1]. If it is less than or equal to P, stop scanning and keep the location of
the smaller element in high. Else compare next element and so on.
v. Check the value of low and high. If low < high , then interchange the low
position data and high position data and repeat steps (iii) and (iv).
vi. Else if low >= high, interchange the partition element and high position
element. Now, the partition element P fixes its proper location.
vii. Every element left of P is less than or equal to P and every element right of P
is greater than P.
viii. The above steps (ii) to (vi) are repeated separately for left and right partition
sub arrays.
Example
The following example illustrates this. Fix Partition Element P = a[0] ie 43
a[0] a[1] a[2] a[3] a[4] a[5] a[6] Scan Direction Status low high
43 72 10 23 80 1 75 Left to Right 72>43 . So Stop Fix low = 1 1
Check low and high ie. 1<5 . So Interchange a[1] and a[5] ie 1 and 72
Page 5. 10
a[0] a[1] a[2] a[3] a[4] a[5] a[6] Scan Direction Status low high
43 1 10 23 80 72 75 Left to Right 1<43 Compare next
Check low and high ie 4>3 So, Interchange P and a[high] ie 43 and 23
23 1 10 43 80 72 75
Apply the above procedure to left partition and right partition. Finally we get the data in
sorted form like below.
1 10 23 43 72 75 80
5.2 Searching
A fundamental operation of a computer is to store huge volume of information and
retrieve them as quickly as possible. Searching plays an important role in information
retrieval. Searching methods are governed by how data and how much data are stored
in computer.
Definition
Searching means finding an element in array or locating the position of element in
array.
Types of Searching
Searching is divided into two categories:
Linear or Sequential Search
Binary search. .
5.2.1 Sequential or Linear search
Definition
In Linear search, the element is searched from 0th element of array to last element in
sequential order.
Page 5. 11
In linear or sequential search, the searching element is compared with each element
of an array one by one ie sequentially, to check whether the given element is found or not. If
the element is found then Search is successful. A search will be unsuccessful if all the
elements are compared and the desired element is not found.
Algorithm
Assume
The array name is A
The element to be searched is x
The array size is n
i. Compare x with A[0]. If equals print “Success” and print the position also.
Else Compare x with A[1]. If equals print “Success” and print the position also.
Else repeat the process upto the last element ie A[n-1].
ii. If no array value matches, print “Search Fail”
Example
Consider an array having 6 elements. Search the element 25.
Given Array Data : 12 16 4 25 69 43
Search Data :25
Pass 1 : 12 16 4 25 69 43 : compare 12 with 25, no match
Pass 2 : 12 16 4 25 69 43 : compare 16 with 25, no match
Pass 2 : 12 16 4 25 69 43 : compare 4 with 25, no match
Pass 2 : 12 16 4 25 69 43 : compare 25 with 25, Equals => Print Success
and position = 3
C program:
# include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,loc = -1;
clrscr()
printf(“\n enter the number of element:”);
scanf(“%d”,&n);
printf(“enter the numbers:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“\n enter the number to be searched:”);
scanf(“%d”,&item);
for(i=0;i<n;i++)
{
if(item==a[i])
{
loc=i;
printf(“\n %d is found in array position %d\n”,item,loc);
Page 5. 12
break;
}
}
if(loc = -1)
printf(“\n item does not exist”);
getch();
}
Example
Consider a sorted array a with 7 elements.
9 12 24 30 36 45 70
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
Page 5. 13
Pass 2
Step 1: mid = int (( lb+ub)/2)
= int ((4+6)/2)
=int (10/2)
=5
Step 2: a[mid]=a[5]=45
Compare x and a [mid].45= 45. So search success. Array Location = 5
C program
# include<stdio.h>
#include<conio.h>
void main()
{
int a[100], i, mid, lb,ub, n, x;
clrscr();
printf(“How many elements”);
scanf(“%d”,&n);
printf(“Enter the elements of the array \n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter the element to be searched \n”);
scanf(“%d”,&x);
lb=0;
ub=n-1;
while (lb <= ub)
{
mid=((lb+ub)/2);
if (x== a[mid])
{
printf(“search is successful and position of array = %d \n”,mid);
break;
}
else
if (x < a[mid])
ub = mid-1;
else
lb = mid+1;
}
if (lb>ub)
printf(“search is not successful\n”);
getch();
}
Page 5. 14
5.3 HASHING
Introduction
In Sequential search, the table that stores the elements is searched successively. In
Binary search, the table that stores the element is divided successively into two halves and
determine which half is to be checked.
There is another technique in which, the searching calculates the position of the key
in the table. It is a one to one correspondence between a key value and an index in the
table to place the key value . The technique is called hashing. Shortly saying, Hashing is a
technique to convert a range of key values into a range of indices (plural of index) of an
array.
The hashing technique and hash table can be expressed as in the following figure
The key value to index value is found out using a hash function H:K→I in such a way
that the function H should be very easy and quick to compute.
Example
Consider a hash table of size 10 whose indices are 0,1,2,3,….9. Assume a set of
key values 10,19,35,43,62,59,31,49,77,33. Let us assume the hash function H is as stated
below
Add the two digits in the key.
Take the digit at the unit place of the result as the index; ignore the digit at the
tenth place if any.
Using this hash function, the mappings from key value to indices and to hash table are
shown in following figure.
Page 5. 15
Before Hashing After Hashing
Index
Key Value K Index I I Key Value K
10 1 0 19
19 0 1 10
35 8 2
43 7 3 49
62 8 4 59,31,77
59 4 5
31 4 6 33
49 3 7 43
77 4 8 35,62
33 6 9
h(ki)=index
Where,
hhash function
kikey value. i=1,2….
The figure given below shows the logical structure of hash function.
The following are the different types of hash functions. They are,
(i) Division method hash function
(ii) Mid square method hash function
(iii) Folding method hash function
h(k) = k mod m
= hash value
Page 5. 16
Where
k key value ranges from k1,k2………kn
m prime number Close to size of index range 0,1,2,….m-1.
The hash function calculates an integer index value in the range 0 to m-1.
Example
Let there are 80 employees record each with a four digit unique employee number.
Let the index value ranges from 0 to 99.
Let m=97[ it is a prime number close to 99]
For k=3205, h(3205) =3205 mod 97=4. That is the employee with employee
number 3205 is stored in index 4.
For k = 7148
h(7148) = k2 = 51093904
... h(2345) = 93
Add all the parts excluding the last part having less number of digits. This
gives the hash value.
The general form is
h(k) = k1,+k2+……kr
Page 5. 17
Example
Let there are 80 employees record each with a four digit unique employee
number.
Let the index value ranges from 0 to 99.
For k = 3205
h(3205) =32 + 05 = 37
For k = 2345
h(2345) =23 + 45 = 68
5.3.3 Collision
Collision is defined as a situation where two different key values hash (map) to the
same memory slot (table index) in the hash table.
Collision resolution
The following are the two important collision resolution methods. They are,
Separate chaining
Open addressing
As shown in the above figure, the slots in the hash table T will not store data
elements. But the slots points to the linked list. For example, the data element with key k is
stored in the linked list pointed by array index value T[h(k)]
Example
Let the size of the array is 10 and the hash function h(k) =k mod 10.
Let the keys are k =75,66,42,192,91,40,49,87,67,16,417,130,372,227.
Therefore,
h(75) = 75 mod 10 = 5
h(66) = 66 mod 10 = 6
h(42) = 42 mod 10 = 2
h(192) = 192 mod 10 = 2
h(91) = 91 mod 10 = 1
----
Page 5. 18
----
h(227) = 227 mod 10 = 7
The figure given below shows the hash table for the given keys.
If the hash table slot for h(k) is already occupied by another key value, a sequence
called probe sequence is computed by using the key k. That means when two key values
are hashed to the same table slot, the probe sequence is successively examined or probed
until an empty hash table slot is found.
Example
Let the size of the array is 10 and the hash function h(k) =k mod 10.
Let the keys are k =7000,7397, 6395,7667.
Therefore,
h(7000) = 7000 mod 10 = 0
h(7397) = 7397 mod 10 = 7
h(6395) = 6395 mod 10 = 5
h(7997) = 7997 mod 10 = 7
In the given keys, two keys 7367 and 7997 hash to the same slot 7. Therefore , the
key 7997 which hashes to the slot 7 which is already occupied by key 7397 is placed in the
next available slot 8 as shown in the figure.
Page 5. 19
Summary
Sorting means arranging the data in increasing order or decreasing order.
Sorting is divided into two groups - Internal sorting & External sorting.
In bubble sort, the array is scanned sequentially by comparing consecutive two
elements and the elements are interchanged if needed.
The selection sort technique is based upon selecting the minimum / maximum value
in array
In Insertion sort, the set of values are sorted by inserting values into an existing
sorted file
The quick sort algorithm works by partitioning the array into sub arrays.
In Radix sort the values are successively ordered on digit positions(called base or
radix), from right to left.
In Merge sort, merge pair of elements to form sorted sub array
The shell sort divides the array into several sub arrays by picking every hith element
as part of sub array.
Searching means finding an element is present or not in the array.
Searching is divided into two categories: Linear or Sequential Search & Binary
search.
In Linear search, the element is searched from 0th element of array to last element in
sequential order.
In Binary search, the searching starts by dividing the sorted array into two halves
using mid value and determine which sub array is used to find the element.
Hashing is a technique to convert a range of key values into a range of indices of an
array.
Hash function is defined as a function that calculates the index value or hash value
The different types of hash functions are
Division method hash function
Mid square method hash function
Folding method hash function
Collision occurs when two different key value hash to the same memory slot in the
hash table.
In separate chaining method , the key values that hash to the same slot in the hash
table are placed in a linked list
In Open addressing method, the key values that hash to the same slot in the hash
table are placed in the next available position in the array.
Page 5. 20
Review Questions
Part A (2 mark Questions)
1. Define sorting. What are the two types of sorting?
2. What is the principle used in bubble sort?
3. What is the principle used in selection sort?
4. What is the logic behind insertion sort?
5. What is the principle used in Merge sort?
6. What is the principle used in Shell sort?
7. What is the logic behind Radix sort?
8. What is the principle used in Quick sort?
9. Define searching. What are the two types of searching?
10. Define Linear Search.
11. Define Binary search.
12. Define Hashing.
13. What is called hash table?
14. What is called collision?
15. What is meant by hash function?
16. What are the two methods of Collision Resolution?
17. What is the principle used in separate chaining?
18. What is called open addressing?
Part B (3 Mark Questions)
1. Write the algorithm for bubble sort.
2. Write the algorithm for Insertion sort.
3. Write the algorithm for Selection sort.
4. Write the algorithm for Merge sort.
5. Write the algorithm for Radix sort.
6. Write the algorithm for Shell sort.
7. Write the algorithm for Quick sort.
8. Write the algorithm for linear or sequential search
9. Write the algorithm for binary search.
10. What is meant by hashing.
11. Write any one method of hash function.
12. Write short notes on any one collision resolution method..
Part c (5 or 10 Mark Questions)
1. Explain bubble sort by taking 8 data in array.
Page 5. 21
2. Write a program to implement bubble sort.
3. Explain with example Selection sort.
4. Write a program to implement selection sort.
5. Explain with example Insertion sort.
6. Write a program to implement Insertion sort.
7. Explain with example Merge sort.
8. Explain with example Radix sort.
9. Explain with example Shell sort.
10. Explain with example Quick sort.
11. Explain linear searching method with suitable example.
12. Explain Binary searching method with suitable example.
13. Explain hashing.
14. Explain Hashing function.
15. Explain any one Collision Resolution method in detail.
16. What is open addressing?. Explain with example.
17. What is called separate chaining?. When will you use it?. Explain.
Page 5. 22