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

Data Structures Notes Unit IV

Uploaded by

bittukamble0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Data Structures Notes Unit IV

Uploaded by

bittukamble0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 207

lOMoARcPSD|35685630

Data Structures Notes - unit -IV

Btech (Visvodaya Engineering College)

Studocu is not sponsored or endorsed by any college or university


Downloaded by Karan Kamble ([email protected])
lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY ANANTAPUR

I B.TECH – II Semester

(19A05201T) DATA STRUCTURES


(Common to All Branches of Engineering)
Unit-I: Introduction
Algorithm Specification, Performance analysis, Performance Measurement, Arrays: Arrays,
Dynamically Allocated Arrays. Structures and Unions. Sorting: Motivation, Quick sort, How
fast can we sort, Merge sort, Heap sort.

Unit-II: Stack, Queue and Linked Lists


Stacks, Stacks using Dynamic Arrays, Queues, Circular Queues using Dynamic Arrays,
Evaluation of Expressions, Multiple Stacks and Queues. Linked lists: Singly Linked Lists
and Chains, Representing Chains in C, Linked Stacks and Queues, Additional List operations,
Doubly Linked Lists.

Unit-III: Trees
Introduction, Binary Trees, Binary Tree Traversals, Additional Binary Tree Operations,
Binary Search Trees, Counting Binary Trees, Optimal Binary Search Trees, AVL Trees, B-
Trees: B-Trees, B+ Trees.

Unit-IV: Graphs and Hashing


The Graph Abstract Data Type, Elementary Graph Operations, Minimum Cost Spanning
Trees, Shortest Paths and Transitive Closure.
Hashing: Introduction to Hash Table, Static Hashing, Dynamic Hashing.

Unit-V: Files and Advanced sorting


File Organization: Sequential file organization, Direct file organization, Indexed sequential
file organization.
Advanced sorting: Sorting on several keys, Lists and Table sorts, Summary on Internal
sorting and External sorting.

Text Books:

1. Ellis Horowitz and Sartaj Sahni, “Fundamentals of Data Structures in C”, 2 nd


editins, Galgotia Book Source, Pvt. Ltd., 2004.
2. Alan L. Tharp, “File Oroganization and Processing”, Wiley and Sons, 1998.

Reference Books:

1. D. Samanta, “Classic Data Structures”, 2nd edition, Prentice-Hall of India, Pvt,


Ltd., India, 2012.
2. Peter Bras, “Advanced Data Structures”, Cambridge University Press, 2016.
3. Richard F. Gilberg, Behrouz A. Forouzan, “Data Structures A Pseudo Code
Approach with C”, Second edition, Cengage Learning 2005.

***

Visvodaya Technical Academy Page 1

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

UNIT-I
Introduction
Algorithm Specification, Performance analysis, Performance Measurement, Arrays: Arrays,
Dynamically Allocated Arrays. Structures and Unions. Sorting: Motivation, Quick sort, How
fast can we sort, Merge sort, Heap sort.

ALGORITHM

An algorithm is a step-by-step procedure of solving the given problem statement.


Algorithms are designed by using pseudo code. Pseudo code is a language independent code.
All algorithms must satisfy the following characteristics.

 Input : Values submitted for the processing the instructions are


known as input values. Here, zero or more quantities are
externally supplied.
 Output : Values generated after processing the instructions are
known as output values. Here, at least one quantity must be
produced.
 Definiteness : Each instruction is in clear format.
Example: Add 10 to X (Valid)
Add 10 or 20 X (Invalid)
 Finiteness : If we trace out the instructions, the algorithm must
terminate after a finite sequence of steps.
 Effectiveness : Every instruction must be in basic format.

Examples: 1. Addition of given two numbers

Step 1: START
Step 2: READ x, y
Step 3: sum ← x + y
Step 4: WRITE sum
Step 5: STOP

The study of algorithms includes four important active areas such as:

a) How to devise algorithms: Algorithms are designed by using design strategies like
Divide-and-Conquer strategy, Greedy method, Dynamic programming, Branch and
bound etc.,

b) How to validate algorithms: Once an algorithm is designed, it is necessary to show


that it computes the correct answer for all possible legal inputs. Such a process is known
as algorithm validation.
After validation of the algorithm, it is converted into programs which are referred to
as program proving/verification.

Visvodaya Technical Academy Page 2

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

c) How to analyze algorithms: As an algorithm is executed, it uses the computer’s


central processing unit to perform operations and its memory to hold the program and
data.
Analysis of algorithms or performance analysis refers to the task of determining
computing time and memory space of an algorithm requires.

d) How to test a program: Testing of a program consists of two phases: debugging and
profiling.
Debugging is the process of detecting and correcting the errors while compiling the
programs for proper execution. Profiling/Performance measurement is the process of
executing a correct program on data sets and measuring the time and space it takes to
compute the results.

To focus on performance evaluation, it includes performance analysis and


performance measurement. In performance analysis, estimation of time and space are
machine independent whereas in performance measurement, those are machine dependent.

ALGORITHM TYPES:

In general, the steps in an algorithm can be divided into three basic categories as:

a) Sequence algorithm
b) Selection algorithm
c) Iteration algorithm

a) Sequence algorithm:

A sequence algorithm is a series of steps in sequential order without any break. Here,
instructions are executed from top to bottom without any disturbances.

Example: Algorithm for addition of given two numbers


Step 1: START
Step 2: READ x, y
Step 3: sum ← x + y
Step 4: WRITE sum
Step 5: STOP

b) Selection algorithm:

Steps of an algorithm are designed by selecting appropriate condition checking is


called as selection algorithms. Selection algorithms are designed using selection control
statements such as IF, IF-ELSE, Nested IF-ELSE, ELSE-IF Ladder and SWITCH statements.

Example: Algorithm for maximum of given three numbers

Step 1: START
Step 2: READ x, y and z values
Step 3: IF x>y AND x>z THEN
Max ← x

Visvodaya Technical Academy Page 3

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

ELSEIF y>z THEN


Max ← y
ELSE
Max ← z
ENDIF
Step 4: WRITE Max
Step 5: STOP

c) Iteration algorithm:

Steps of an algorithm are designed based on certain conditions and repeatedly


processed the same statements until the specified condition becomes false is called as
iteration algorithms. Iteration algorithms are designed using iterative control statements such
as WHILE, D0-WHILE and FOR statements.

Example: Algorithm for reverse of a given number

Step 1: START
Step 2: READ n value
Step 3: rev ← 0
Step 4: Repeat WHILE n > 0
k ← n MOD 10
rev ← rev * 10 + k
n ← n / 10
EndRepeat
Step 5: WRITE rev
Step 6: STOP

RECURSIVE ALGORITHMS

A function calls itself is known as recursion and the function is called as recursive
function. The main advantage of recursion concept is to reduce length of the code.

Example: sum()
{
----
----
sum();
----
----
}

Recursion can be classified into two types as:

1. Direct recursion
2. Indirect recursion

Visvodaya Technical Academy Page 4

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

1. Direct recursion: A function calls itself directly is known as direct recursion.

Ex: void sum()


{
----
----
sum();
----
----
}

2. Indirect recursion: A function calls another function, which initiates to call of the initial
function is known as indirect recursion.

Ex: void sum() void call()


{ {
---- ----
---- ----
call(); sum();
---- ----
---- ----
} }

While designing recursive concept, we must place two conditions namely base
condition and recursive condition. Base condition is a condition that avoids recursive call
without falling into infinite loop and recursive condition is a condition that calls recursive
procedures. Both conditions use a return statement associated with if / if-else statements.

Examples:

1. Recursive algorithm for factorial of a given number

Fact(N) : IF N = 1 THEN
RETURN 1
ELSE
RETURN N*Fact(N-1)
ENDIF

2. Recursive algorithm for Fibonacci sequence numbers

Fib(N) : IF N = 0 OR N = 1 THEN
RETURN N
ELSE
RETURN Fib(N-1) + Fib(N-2)
ENDIF

3. Recursive algorithm for Ackerman’s function

A(m , n) = n+1 , if m = 0
A(m-1 , 1) , if n = 0
A(m-1 , A(m, n-1)) , otherwise.

Visvodaya Technical Academy Page 5

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

4. Towers of Hanoi problem

In towers of Hanoi problem, three towers A, B and C are given. Consider tower A as source,
B as intermediate and C as destination towers. A finite number of N disks are arranged on the
source tower A in decreasing order of their size from bottom to top.
The main objective of this problem is to move these disks from source tower to
destination tower without violating the condition “decreasing order of their size from
bottom to top” and move only one disk at a time.

Ex: Let N = 2 then initial status of the problem statement is:

2
1

A B C
Source Intermediate Destination

After processing the instructions, tower status becomes:

2
1

A B C
Source Intermediate Destination
Let N = 1; then processing operations are : A→C

Let N = 2; then processing operations are : A→B


A→C
B→C

i.e., If the tower consists of N disks, then the problem statement can be solved by
performing 2N – 1 processing operations.

Algorithm Tower (N, BEG, AUX, END)

Step 1: IF N = 1 THEN
WRITE BEG → END
RETURN
ENDIF
Step 2: Call TOWER(N-1, BEG, END, AUX)
WRITE BEG → END
Call TOWER(N-1, AUX, BEG, END)
Step 3: RETURN

Visvodaya Technical Academy Page 6

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

For N = 3; recursion procedure works as:

Tower(1,A,B,C) = A→C
Tower(2,A,C,B) A→B
Tower(1,C,A,B) = C→B

Tower(3,A,B,C) A→C

Tower(1,B,C,A) = B→A
Tower(2,B,A,C) B→C
Tower(1,A,B,C) = A→C

PERFORMANCE ANALYSIS / ANALYSIS OF ALGORITHMS

Analysis of algorithms (or) performance analysis refers to the task of determining


how much computing time (time complexity) and storage (space complexity) of an algorithm
requires.

Algorithm efficiency describes the properties of an algorithm which relates to the


amount of resources used. An algorithm must be analyzed to determine its resources usage.

The time complexity of an algorithm is the amount of computer time it needs to run
for its completion. The space complexity of an algorithm is the amount of memory it needs
to run for its completion.

These complexities are calculated based on the size of the input. With this, analysis
can be divided into three cases as:

 Best case analysis


 Worst case analysis
 Average case analysis

Best case analysis: In best case analysis, problem statement takes minimum
number of computations for the given input parameters.

Worst case analysis: In worst case analysis, problem statement takes


maximum number of computations for the given input parameters.

Average case analysis: In average case analysis, problem statement takes


average number of computations for the given input parameters.

Based on the size of input requirements, complexities can be varied. Hence, exact
representation of time and space complexities is not possible. But they can be shown in some
approximate representation using mathematical notations known as asymptotic notations.

Visvodaya Technical Academy Page 7

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

SPACE COMPLEXITY

The process of estimating the amount of memory space to run for its completion is
known as space complexity.

Space complexity S(P) of any problem P is sum of fixed space requirements and
variable space requirements as:

Space requirement S(P) = Fixed Space Requirements + Variable Space Requirements

1. Fixed space that is independent of the characteristics (Ex: number, size) of the input
and outputs. It includes the instruction space, space for simple variables and fixed-
size component variables, space for constants and so on.

2. Variable space that consists of the space needed by component variables whose size is
dependent on the particular problem instance being solved, the space needed by the
referenced variables and the recursion stack space.

When analyzing the space complexity of any problem statement, concentrate solely
on estimating the variable space requirements. First determine which instance characteristics
to use to measure the space requirements. Hence, the total space requirement S(P) of any
program can be represented as:

S (P) = C + SP (I)

Where,
C is a constant representing the fixed space requirements and I refer to
instance characteristics.

Example 1: float sum(float a, float b, float c)


{
return a+b+c;
}

Here, the variables a,b and c are simple variables.


Therefore Ssum = 0.

Example 2: float sum(float list[25], int n)


{
float s = 0;
int i;
for( i = 0 ; i < n ; i++)
s = s + list[i];
return s;
}

Here, the instance characteristic is n. Variable terms are n, i and s. So that count
values are treated as n for the list array, one each for n, i, and s.
Therefore Ssum (n) = n + 3.

Visvodaya Technical Academy Page 8

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example 3: float sum(float list[25], int n)


{
if (n <= 0)
return 0.0;
else
return sum(list,n-1) + list[n];
}

Here, the instance characteristic is n. Recursion includes space for formal parameters,
local variables, and the return address. So that count values are treated as one for n, one for
return address and one for list[] array. Function works for n+1 times. For every call the three
word counts should be worked.

Therefore Ssum (n) = 3(n + 1).

TIME COMPLEXITY

The process of estimating the amount of computing time to run for its completion is
known as time complexity.

The time T(P) taken by a program P is the sum of its compile time and its run time.

i.e., Time complexity T(P) = Compile time + Run time

Here,
Compile time is a fixed component and does not depends on the instance
characteristics. Hence,

T(P) = C + TP (Instance characteristics)


Where, C is a fixed constant value

T(P) ≥ TP(I)
Where, I refer instance characteristic.

Time complexity of a program is calculated by determining the number of steps that a


program/function needs to solve known as step count and then express it in terms of
asymptotic notations.

Visvodaya Technical Academy Page 9

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

STEP COUNT METHOD

In Step count method, determine the number of steps that a program or a function
needs to solve a particular instance by creating a global variable count, which has an initial
value of ‘0’. Now increment count by number of program steps required by each executable
statement. Finally add the total number of times that the count variable is incremented.

Example 1: float sum(float a, float b, float c) count = 0


{
return a+b+c; count=count+1
count=count+1
}

Here, count variable is incremented by twice one for addition operation and one for
return statement.

Therefore Tsum = 2.

Example 2: float sum(float list[25], int n) count = 0


{
float s = 0; count=count+1 /* Assignment */
int i;
for( i = 0 ; i < n ; i++)
{ count=count+1 /* i Assignment */
s = s + list[i]; count=count+1 /* s Assignment */
}
count=count+1 /* i last assignment */
return s; count=count+1 /* return statement */
}

Here, inside the loop count variable is incremented by two times and the loop is
executed for n times so that it becomes 2n steps. Outside the loop count is incremented by 3
steps.

Therefore Tsum (n) = 2n + 3.

Example 3: float sum(float list[25], int n) count=0


{
if (n <= 0) count=count+1
return 0.0; count=count+1
else
return sum(list,n-1) + list[n]; count=count+1
}

Here, if n = 0 then count is incremented by 2 steps. Otherwise, it also incremented by


2 steps and increase additions of sum(list,n-1).

Visvodaya Technical Academy Page 10

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Therefore Tsum (n) = 2 ; if n = 0


= 2 + Tsum (n-1) ; if n > 0

These recursive formulas are referred to as recurrence relations. Recurrence


relations are solved to make repeated substitutions for each occurrence of recursive process
until all such occurrences disappear.

Tsum (n) = 2 + Tsum (n-1)


= 2 + 2 + Tsum (n-2)
= 2 + 2 + 2 + Tsum (n-3)
= 3 (2) + Tsum (n-3)
-
-
= n (2) + Tsum (n-n)
= 2n + 2

Therefore Tsum = 2n + 2.

STEP COUNT TABLE METHOD

Another way to obtain step count is tabular method. In this method,

 First determine the step count of each statement known as steps/execution simply s/e.
 Note down the number of times that each statement is executed known as frequency.
The frequency of a non-executable statement is 0.
 Multiply s/e with frequency, gives us the total steps for each statement.
 Finally, add these total steps, gives us the step count of the entire function.

Example 1:

STATEMENT s/e Frequency Total Steps


int ADD(int x, int y) 0 0 0
{ 0 0 0
int z; 0 0 0
z = x + y; 1 1 1
return z; 1 1 1
} 0 0 0
STEP COUNT VALUE 2

Visvodaya Technical Academy Page 11

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example 2:

STATEMENT s/e Frequency Total Steps


int ADD(int k[10], int n) 0 0 0
{ 0 0 0
int i,s; 0 0 0
s = 0; 1 1 1
for(i = 0; i < n; i++) 1 n+1 n+1
s = s + k[i]; 1 n n
return s; 1 1 1
} 0 0 0
STEP COUNT VALUE 2n+3

Example 3:

STATEMENT s/e Frequency Total Steps


int ADD(int k[10], int n) 0 0 0
{ 0 0 0
if (n = = 0) 1 n+1 n+1
return 0; 1 1 1
else 0 0 0
return (k[n] + ADD(k,n-1); 1 n n
} 0 0 0
STEP COUNT VALUE 2n+2

Examples:

 Design step count table for matrix addition.


 Design step count table for matrix multiplication.

Based on the size of input requirements, complexities can be varied. Hence, exact
representation of time and space complexities is not possible. But they can be shown in some
approximate representation using mathematical notations known as asymptotic notations.

ASYMPTOTIC NOTATIONS

Asymptotic notations are the mathematical representations of time and space


complexities in some approximate formats. The notations are:

 Big ‘Oh’ notation


 Omega notation
 Theta notation
 Little ‘Oh’ notation
 Little omega notation

Visvodaya Technical Academy Page 12

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Big ‘Oh’ notation (O):

The function f(n) = O(g(n)) iff there exist two positive constants c and n 0 such that
f(n) ≤ c * g(n) for all n, n ≥ n0.

The graphical representation between n values on X-axis and f(n) values on Y-axis is as
follows:
Y

c*g(n)
Here, the functional value f(n) is always below the
estimated functional value c*g(n). Thus, the function n0
g(n) acts as upper bound value for the function f(n).
f(n)
Hence, Big ‘Oh’ notation is treated as “Upper bounded
Function”. f(n)

n X

Example:

Consider f(n) = 3n+2

Assume that 3n+2 ≤ 4n

Let n=1 3(1) + 2 ≤ 4(1) → 5≤4 FALSE


n=2 3(2) + 2 ≤ 4(2) → 8≤8 TRUE
n=3 3(3) + 2 ≤ 4(3) → 11 ≤ 12 TRUE
.
.

From this,
c=4 g(n) = n and n0 = 2

Hence, the function 3n+2 = O(n) iff there exist two positive constants 4 and 2 such
that 3n+2 ≤ 4n for all n, n ≥ 2.

Example: The function n2 + n + 3 = O(n2) iff there exist two positive constants 2 and 3
such that n2 + n + 3 ≤ 2n2 for all n, n ≥ 3.

In these complexities,
O(1) means constant
O(n) means linear
O(log n) means logarithmic
O(n2) means quadratic
O(n3) means cubic
n
O(2 ) means exponential.

O(1) < O(log n) < O(n) < O(n logn) < O(n2) < O(n3) < - - - - - - - < O(2n) .

Visvodaya Technical Academy Page 13

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Omega notation (Ω):

The function f(n) = Ω(g(n)) iff there exist two positive constants c and n 0 such that
f(n) ≥ c * g(n) for all n, n ≥ n0.

The graphical representation between n values on X-axis and f(n) values on Y-axis is as
follows:
Y f(n)

Here, the functional value f(n) is always above the


estimated functional value c*g(n). Thus, the function c*g(n)
g(n) acts as lower bound value for the function f(n).
f(n) n0
Hence, Omega notation is treated as “Lower bounded
Function”.

Example: n X

Consider f(n) = 3n+2

Assume that 3n+2 ≥ 3n

Let n=1 3(1) + 2 ≥ 3(1) → 5≥3 TRUE


n=2 3(2) + 2 ≥ 3(2) → 8≥6 TRUE
.
.

From this,
c=3 g(n) = n and n0 = 1

Hence, the function 3n+2 = Ω (n) iff there exist two positive constants 3 and 1 such
that 3n+2 ≥ 3n for all n, n ≥ 1.

Theta notation (Ө):

The function f(n) = Ө(g(n)) iff there exist three positive constants c 1, c2 and n0 such
that c1 * g(n) ≤ f(n) ≤ c2 * g(n) for all n, n ≥ n0.

The graphical representation between n values on X-axis and f(n) values on Y-axis is as
follows:
Y
c2 * g(n)

Here, the functional value f(n) is always lies in f(n)


between the estimated functional values c1*g(n).
and c2*g(n). Thus, the function g(n) acts as lower f(n) c1 * g(n)
bound and upper bound value for the function f(n). n0
Hence, Theta notation is treated as “Bounded
Function”. n X

Visvodaya Technical Academy Page 14

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example:

Consider f(n) = 3n+2

Assume that 3n ≤ 3n+2 ≤ 4n

Let n=1 3(1) ≤ 3(1) + 2 ≤ 4(1) → 3≤5≤4 FALSE


n=2 3(2) ≤ 3(2) + 2 ≤ 4(2) → 6≤8≤8 TRUE
n=3 3(3) ≤ 3(3) + 2 ≤ 4(3) → 9 ≤ 11 ≤ 12 TRUE
.
.

From this,

c1 = 3 c2 = 4 g(n) = n and n0 = 2

Hence, the function 3n+2 = Ө(g(n)) iff there exist three positive constants 3,4 and 2
and n0 such that 3n ≤ 3n+2 ≤ 4n for all n, n ≥ 2.

Little ‘Oh’ notation (o):

The function f(n) = o(g(n)) iff there exist two positive constants c and n 0 such that
f(n) < c * g(n) for all n, n ≥ n0.

(OR)

The function f(n) = o(g(n)) iff Lim = 0

n→∞

Little Omega notation (ω):

The function f(n) = ω(g(n)) iff there exist two positive constants c and n 0 such that
f(n) > c * g(n) for all n, n ≥ n0.

OR

The function f(n) = ω(g(n)) iff Lim = 0

n→∞

Visvodaya Technical Academy Page 15

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

PERFORMANCE MEASUREMENT

Performance measurement also refers to as the process of estimating computing time


and memory space which are machine dependent. To calculate computing time in C
language, it supports two library functions known as clock() function and time() function.

clock() function:

clock() is a library function that gives the amount of processor time. The time is
returned as a built-in type, clock_t defined in time.h header file. The general format of
clock() library function is:

Syntax: clock_t clock();

To get processing time of specific events, apply the function before calling the event
as start time and after completion of the event as end time. Then subtract start time from the
end time. Here, the result is measured as internal processor time; divide it by the number of
clock ticks per second to obtain the result in seconds. In ANSI C, the ticks per second are
held in the built-in constant, CLOCKS_PER_SEC.

Example: clock_t start, end;


double k;
start = clock();
-----
-----
end = clock();
k = ((double) (end-start)) / CLOCKS_PER_SEC;

time() function:

time() is a library function that gives the amount of processor time measured in
seconds. The time is returned as a built-in type, time_t defined in time.h header file. The
general format of clock() library function is:

Syntax: time_t time(NULL);

To get processing time of specific events, apply the function before calling the event
as start time and after completion of the event as end time. Then subtract start time from the
end time.

Example: time_t start, end;


double k;
start = time(NULL);
-----
-----
end = time(NULL);
k = (double) (end-start);

Visvodaya Technical Academy Page 16

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

/* Example program for performance measurement */

#include<stdio.h>
#include<conio.h>
#include<time.h>
main()
{
int p[50],i,n;
double k;
time_t start,end;
clrscr();
printf("\nEnter How Many Values=");
scanf("%d",&n);
start=time(NULL);
printf("\nEnter %d Elements=",n);
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("\nArray Elements Are=\n");
for(i=1;i<=n;i++)
printf(" %d",p[i]);
end=time(NULL);
k=(double)(end-start);
printf("\n\nTime Duration = %lf Seconds",k);
}

ARRAYS

An array is a collection of homogeneous/similar/same data type elements those are


stored in successive memory locations. Based on the number of subscripts used with the
array, it can be classified into different categories such as:
a) Single/One dimensional arrays
b) Double/Two dimensional arrays
c) Multi-dimensional arrays.

a) Single/One dimensional arrays

Let ‘m’ is the size of an array, then a single dimensional array can be defined as –
“Single dimensional array is a collection of m homogeneous data elements those are stored in
m successive memory locations”.

Example: int K[5];

For this, the memory allocation will be:

Index Values 0 1 2 3 4
K

Visvodaya Technical Academy Page 17

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

1000 1002 1004 1006 1008 Addresses


Array Name

b) Double/Two dimensional arrays

Let ‘m’ is the row size and ‘n’ is the column size, then a double dimensional array can
be defined as – “Double dimensional array is a collection of m x n homogeneous data
elements those are stored in m x n successive memory locations”.

Example: int K[3][4];

For this, memory allocation will be:

0 1 2 3
K
0

For double dimensional arrays, memory is allocated in terms of table format


that contains collection of rows and columns. So, that double dimensional arrays are useful
for matrix representation of given elements.

c) Multi-dimensional arrays

Multidimensional array uses three or more dimensions. Let m1, m2, - - - , mn are the
sizes, then a multidimensional array can be defined as – “Multidimensional array is a
collection of m1 x m2 x - - - - x mn homogeneous data elements those are stored in m1xm2x-
- - - x mn successive memory locations”.

Example: int K[2][3][3];

The above example is a three dimensional array. For this, memory allocation will be:

0 1 2
K
0
0
1

0 1 2

0
1
1

2
Visvodaya Technical Academy Page 18

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

For multidimensional arrays, compiler allocates memory as in terms of collection of


tables.

Note:

Assume the given array dimension as: K[LB:UB]

Where, LB refers to lower bound


UB refers to upper bound of the array K.

Then, the address of particular location K[i] can be calculated as:

Address of K[i] = Base Address + (i – LB) * Size of Element

DYNAMICALLY ALLOCATED ARRAYS

In arrays concept, the process of specifying array size at the time of its declaration is
referred to as static allocation. In most of the cases, static allocation is not useful to save
memory. For proper utilization, it is better to allocate specific amount of memory at runtime
known as dynamic memory with the help of dynamic memory allocation functions.

C language supports dynamic memory allocation functions as malloc(), calloc() and


realloc() to allocate memory at run time.

1. malloc() function: malloc() function is used to allocate memory for the variables
at run time. The general form of malloc() function is:

Syntax: prtvariable = (casttype*)malloc(size);


Where,
ptrvariable is a pointer variable of type casttype.
size represents number of bytes of memory to be allocated.

Example: int *X;

X = (int*)malloc(10); (or) X = (int*)malloc(5*sizeof(int));

Here, malloc() function reserves a single block of memory with the specified size and
returns a pointer of type void. With this, we can assign it to any type of pointer variable. By
default memory location is filled with garbage values. For this, memory allocation will be:

X Garbage Values

Visvodaya Technical Academy Page 19

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

10 BYTES

2. calloc() function: calloc() function is another memory allocation function used


for dynamic memory allocation for storing derived data types such as arrays and structures
etc.,
The general form of calloc() function is:

Syntax: ptrvariable = (casttype*)calloc(n,elesize);

Where,
ptrvariable is a pointer variable of type casttype.
n represents number of blocks.
elesize represents block size.

Example: int *X;

X = (int*)calloc(5,sizeof(int));

Here, calloc() function allocates multiple blocks of storage space with each of same
size and by default all locations are initialized with ‘0’s. If there is not enough space to
allocate, then it returns a NULL pointer.

For this, memory allocation will be:

X 0 0 0 0 0
10 BYTES

A) Single / One dimensional array

Let ‘m’ is the size of an array, then a single dimensional array can be defined as – “It
is a collection of m homogeneous data elements those are stored in m successive memory
locations”.

Example: int *K,n;

K = (int*) calloc(n,sizeof(int));

/* Read and print a single dimensional array using dynamic memory allocation */

#include<stdio.h>
#include<conio.h>

main()
{
int *k,n,i;
clrscr();
printf("\nEnter How Many Elements =");

Visvodaya Technical Academy Page 20

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

scanf("%d",&n);
k=(int*)calloc(n,sizeof(int));
printf("\nEnter %d Elements = ",n);
for(i=0;i<n;i++)
scanf("%d",k+i);
printf("\nArray Elements Are =\n");
for(i=0;i<n;i++)
printf(" %d",*(k+i));
}

B) Double / Two dimensional array

Let ‘m’ is the row size and ‘n’ is the column size, then a double dimensional array can
be defined as – “It is a collection of m x n homogeneous data elements those are stored in m x
n successive memory locations”.

Example 1: int *K[5],n;

K[i]=(int*)calloc(n,sizeof(int));

Example 2: int **K,m,n;

K=(int**)calloc(m,sizeof(int*));
for(i=0 ; i<m ; i++)
K[i] = (int*)calloc(n,sizeof(int));

/* Reading and printing a double dimensional array */

#include<stdio.h>
#include<conio.h>

main()
{
int *k[3],n,i,j;
clrscr();
printf(“\nEnter How Many Columns =”);
scanf(“%d”,&n);
for(i=0;i<3;i++)
k[i]=(int*)calloc(n,sizeof(int));
printf("\nEnter Array Elements=");
for(i=0;i<3;i++)
{
for(j=0;j<n;j++)
scanf("%d",*(k+i)+j);
}
printf("\nArray Elements Are=\n\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<n;j++)

Visvodaya Technical Academy Page 21

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

printf(" %d",*(*(k+i)+j));
}
}
STRUCTURES

Structure is a user defined data type and can be defined as “It is a collection of non-
homogeneous / heterogeneous / different data elements that can be grouped together under a
common name”.

Structure Declaration and Definition

A structure declaration is specified with the keyword “struct” followed by a user


defined name surrounded by pair of braces, which describes the members of the structure.
The general format of a structure declaration is:

Syntax: struct Tag


{
Datatype Member1;
Datatype Member2;
---
Datatype Membern;
};

 struct is a keyword used to define structure declaration.


 Tag is name of the structure that follows same rules as a valid identifier.
 Members declared inside the structure declaration are called structure elements (or)
structure members.
 Structure declaration must be ended with a semicolon.
 Structure is a user-defined data type.

Example: struct Book


{
char BName[10];
float Price;
};

Declaration of the structure does not reserve any storage space. Memory is allocated
only at the time of defining a structure variable. The general format of defining a structure
variable is:

Syntax: struct Tag varname1, varname2, - - - - - , varnamep;


Example: struct Book B1;

Now, the compiler allocates memory for the structure variable B1 as

BName :
B1
Price :

14 Bytes

Visvodaya Technical Academy Page 22

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

i.e., Memory is allocated individually for each member of the structure.

Accessing members of the structure

‘.’ Dot operator is used to access members of the structure with its structure variable.
Here dot operator is also known as member operator (or) period operator. It forms link
between structure member and structure variable. The general format of accessing a structure
member with structure variable is:

Syntax: structurevariable.member;

Example: B1.Pages;

/* EXAMPLE PROGRAM FOR PRINTING BOOK DETAILS USING STRUCTURE */

#include<stdio.h>
#include<conio.h>

struct Book
{
char BName[50];
float Price;
};

main()
{
struct Book B1;
clrscr();
printf("\nEnter Titile of the Book:");
gets(B1.BName);
printf("\nEnter Cost of the Book:");
scanf("%f",&B1.Price);
printf("\nBOOK TITLE:%s",B1.BName);
printf("\nBOOK COST:%.2f RS",B1.Price);
}

Note:

1. A structure can be initialized with a list of values at the time of defining the structure
variable. But, individual member initialization inside the structure declaration is not possible.
The general format of initializing a structure is:

Syntax: struct Tag varname = { List of Values };

Example: struct Book B1 = {“Let Us C”,125.75};

Example: strcpy(B1.BName,”Let Us C”);

Visvodaya Technical Academy Page 23

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

B1.Price = 125.75;

2. Two variables of the same structure type can be copied in the same way as ordinary
variables using assignment operator.

Example: Let s1 and s2 are two structure variables of the same structure type,
and then the details of s1 are copied into s2 as:

s2 = s1;
Here,
Each individual member of s1 is copied into individual member of s2.

3. Direct comparison of one structure variable with another structure variable using
relational operators is not possible.

i.e., Let s1 and s2 are two structure variables of the same structure type, and then the
comparisons like
s1>s2, s1<s2, s1<=s2, s1>=s2, s1==s2 and s1!=s2 are invalid operations.

However, for comparing structure variables individual member comparisons is valid.

4. “typedef” keyword is allowed the user to change the name of the existing data types.
typedef keyword is very useful in the case of user-defined data types like structure. While
using typedef keyword with the structure creation, tag of the structure is optional.

Example: typedef struct


{
float real, imag;
}
Complex;

Complex k;

5. A structure can also be included within another structure. Such a representation is


known as nested structures.

Example: struct Employee


{
char name[50];
struct date
{
int day;
int month;
int year;
}join;
float salary;
}K;

Visvodaya Technical Academy Page 24

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

SELF-REFERENTIAL STRUCTURES

A structure in which, at least one member that points to same structure type is referred
to as a self-referential structure. Self-referential structures usually require dynamic storage
management functions to explicitly obtain and release memory. The general format of self-
referential structure is:

Syntax: struct Tag


{
Datatype Member1;
- - -
struct Tag *Member;
};

Example: typedef struct List


{
int info;
struct List *link;
}Node;

Node *New;
New = (Node*)malloc(sizeof(Node);
New -> info = 10;
New -> link = NULL;

UNION

Union is also a user-defined data type that is similar to structure. i.e., Union is a
collection of non-homogeneous / heterogeneous / different data type elements that can be
grouped together under a common name.

The general format of a union declaration is:

Syntax : union Tag


{
Datatype Member1;
Datatype Member2;
---
---
Datatype Membern;
};
Where,

 union is a keyword used to declare the union data type.


 Tag is name the union that follows same rules as a valid identifier.

Visvodaya Technical Academy Page 25

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

 Members declared inside the union are known as union members (or) union elements.
 Union declaration must be ended with a semicolon.

Example : union Account


{
int acno;
float balance;
char actype[10];
};

Memory is allocated for the union only at the time of creating union variable. The
general form of creating union variables is:

Syntax : union Tag Variable1, Variable2, - - - - - , Variablep;

Example : union Account X;

For this, the memory allocation will be:

10 Bytes

 In union, compiler selects the member which occupies highest memory, and that
memory is reserved only. So that, all the members of the union are shared that
common memory. It implies that, although a union may contain many members of
different types, it can handle only one member at a time.

 Dot operator is used to access member of the union with the union variable. The
general format of accessing union members with union variable is:

Syntax : UnionVariable . Member;

Example : X . balance;

/* PROGRAM TO CREATE ACCOUNT DATABASE USING UNION */

#include<stdio.h>
#include<conio.h>

union Account
{
int acno;
char actype[10];

Visvodaya Technical Academy Page 26

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

float acbalance;
};

main()
{
union Account x;
clrscr();
printf("\nEnter Account Number:");
scanf("%d",&x.acno);
printf("\nAccount Number:%d",x.acno);
fflush(stdin);
printf("\n\n\nEnter Account Type:");
gets(x.actype);
printf("\nAccount Type:%s",x.actype);
printf("\n\n\nEnter Balance Amount:");
scanf("%f",&x.acbalance);
printf("\nBalance Amount:%.2f RS",x.acbalance);
}

Note:

1. The main difference between a structure and union is in terms of their storage space.
In structure, each member has its own storage location. Whereas. In union all
members shared a common memory. Hence, the main advantage of union is to save
memory compared to the structure with same members.

2. A union can be included within another union. In addition to this, A union may be a
member of a structure and structure may by a member of union. These concepts are
referred as union of structures and structure of unions.

Example:
struct demo1 union demo1
{ {
char x; char x;
int y; int y;
}; };

union demo2 struct demo2


{ {
float a; float a;
struct demo1 p; union demo1 p;
double b; double b;

}K; }K;

Visvodaya Technical Academy Page 27

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

SEARCHING

Searching refers to the process of checking whether a given element is present in the
list of elements or not.
In this procedure, consider an array with ‘n’ elements. A specific element ‘key’ is
given to search. Now, we want to find whether the key element is available in the list of n
elements or not. If the search item key is exists, then it refers to successful search i.e.,
element found; otherwise, it refers to unsuccessful search i.e., element not found.
Most important techniques used for search operation are:

1. Linear search
2. Binary search
3. Fibonacci search
4. Interpolation search etc.

1. LINEAR SEARCH / SEQUENTIAL SEARCH

Consider an array K with n elements as K[1], K[2], . . . K[n]. Assume an element of


information is given in the variable Key to search.

In linear search technique, compare the key element with each element of K form
index 1 to index n. At any position i, if K[i] = Key, then return index i value refers to
successful search (Element Found). For the entire procedure K[i] ≠ Key and elements are not
available for further comparison, then return -1 refers to unsuccessful search (Element not
Found). For this, the functional procedure can be placed as:

int search(int K[50], int n, int Key)


{
int i;
for(i=1 ; i<=n ; i++)
{
if(K[i] = = Key)
return i;
}
return -1;
}

Example: Search an element 19 from the list of elements:


18 35 78 19 23 709

Solution: Given K[1:6] = 18 35 78 19 23 709


Search Element Key = 19

i=1 1≤6 TRUE K[1]=Key → 18 = 19 FALSE


i=2 2≤6 TRUE K[2]=Key → 35 = 19 FALSE
i=3 3≤6 TRUE K[3]=Key → 78 = 19 FALSE

Visvodaya Technical Academy Page 28

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

i=4 4≤6 TRUE K[4]=Key → 19 = 19 TRUE

→ SUCCESSFUL SEARCH i.e., ELEMENT FOUND AT 4th INDEX POSITION


Analysis of Linear Search

Analysis of search methods depends on the number of comparisons applied to search


the given element. In the linear search technique, if the function finds the search element at
the first position of the list, it does only one key comparison. If the target element is the
second element of the list, it requires two comparisons and so on. Hence, the number of key
comparisons is:

f(n) =

 f(n) =

= X

= O(n)

Therefore, the worst case and average case time complexity of linear search is O (n).

2. BINARY SEARCH

Binary search is another searching technique that takes less time complexity
compared with the linear search technique. Binary search can be applied only on the array
elements which is available in sorted order.

Consider K is an array consists of n elements in sorted order such that K[1] ≤ K[2]
≤ . . . . ≤ K[n]. Suppose an item of information is given to search in the variable Key. Then
binary search procedure works as:

In binary search technique, first compute

Mid

Where, Low refers to the first index and High refers to last index of the array at the
initial call. Now, the process falls into any one of the following three cases.

Visvodaya Technical Academy Page 29

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Case 1: If Key = K[Mid]; Then the search is successful search i.e., Element Found.

Case 2: If Key > K[Mid]; Then the Key element can appear only in the right half of
the array. So, we reset the Low value as Low = Mid+1 and begin search
again.

Case 3: If Key < K[Mid]; Then the Key element can appear only in the left half of the
array. So, we reset the High value as High = Mid-1 and begin search again.
The above procedure is repeated up to we reach Low > High. When we obtain this
condition, it indicates that the search is unsuccessful search i.e., Element not found.

For this, functional procedure can be placed as:

int search(int K[50], int Low, int High, int Key)


{
int Mid;
while(Low <= High)
{
Mid = (Low + High) / 2;
if (Key = = K[Mid])
return Mid;
else if (Key > K[Mid])
Low = Mid + 1;
else
High = Mid – 1;
}
return -1;
}

Example: Search an element 44 from the list of elements


11 22 30 33 41 44 55

Solution: Given K[1:7] = 11 22 30 33 41 44 55


Search Element Key = 44

Pass 1: Low = 1 High = 7 1<7 TRUE


Mid = (1+7) / 2 = 4
Key = K[Mid] 44 = 33 FALSE
Key > K[Mid] 44 > 33 TRUE
Hence, Reset Low = 4+1 = 5

Pass 2: Low = 5 High = 7 5<7 TRUE


Mid = (5+7)/2 = 6
Key = K[Mid] 44 = 44 TRUE

SUCCESSFUL SEARCH i.e., ELEMENT FOUND

Analysis of Binary search

Visvodaya Technical Academy Page 30

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

The complexity is measured by the number of comparisons to locate the search item
in the given array elements.

In binary search, each comparison reduces the size of the array into half. So that
number of comparisons is less compare to linear search. Hence, the worst case and average
case time complexity of binary search is O (log n).

3. INTERPOLATION SEARCH

Interpolation search is another searching technique can be used only when the list of
elements is ordered and uniformly distributed.

The interpolation search is an improvement over binary search for instances, where
the values in a sorted array are uniformly distributed. Binary search always goes to the
middle element to check. On the other hand, interpolation search may go to different
locations according to the value of key being searched.

Let K is an array that consists of n elements from index 0 to n-1. Search element is
given in the variable Key. Then interpolation search procedure works as:

Step 1:In a loop, calculate the value of “Pos” using the formula

Pos = Low + [ (Key – K[Low]) * (High – Low) / (K[High] – K[Low]) ]

Where, Low refers to starting index 0 and High refers to ending index n-1 at the first
call.

Step 2:Compare K[Pos] element with Key element. If it is a match, return Pos
position that refers to element found.

Step 3:If the Key element is less than K[Pos] element, calculate the next position at
the left sub-array by changing High value as High = Pos – 1.

Step 4: If the Key element is greater than K[Pos] element, calculate the next position
at the right sub-array by changing Low value as Low = Pos + 1.

Step 5:Repeat the above procedure until the search element is found or the sub-array
reduces to zero.

/* INTERPOLATION SEARCH PROGRAM */

#include<stdio.h>
#include<conio.h>
int search(int[],int,int);

main()
{
int K[5]={10,12,13,16,18},n,Key,Pos;
clrscr();
printf("\nEnter the Search Element=");

Visvodaya Technical Academy Page 31

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

scanf("%d",&Key);
Pos=search(K,5,Key);
if(Pos==-1)
printf("\nElement Not Found");
else
printf("\nElement Found");
}
int search(int K[5],int n,int Key)
{
int Pos;
int Low=0,High=n-1;
while(Low<=High)
{
if(Low==High)
{
if(K[Low]==Key)
return Low;
else
return -1;
}
Pos=Low+((Key-K[Low])*(High-Low)/(K[High]-K[Low]));
if(K[Pos]==Key)
return Pos;
else if(K[Pos]<Key)
Low=Pos+1;
else
High=Pos-1;
}
return -1;
}

SORTING

Sorting refers to the arrangement of data items either in the ascending (increasing)
order or in the descending (decreasing) order. Some of the most important sorting
techniques are:
a) Bubble Sort
b) Insertion Sort
c) Selection Sort
d) Quick Sort
e) Merge Sort etc.,

Sorting techniques are classified into two types as: Internal sorting techniques and
External sorting techniques.

Visvodaya Technical Academy Page 32

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Sorting that performed in main memory is called internal sorting i.e., in an internal
sort, all the data is held in primary memory during the sorting process. Internal sorting
techniques are used to handle small amount of data.

Examples: Bubble Sort, Insertion Sort, Selection Sort, Quick Sort etc.

Sorting that performed with the interaction of secondary storage devices like disks or
tapes is called external sorting i.e., in an external sort, some part of the data is in primary
memory during the sorting process and the remaining data is in secondary storage devices
that doesn’t fit in the primary memory. External sorting techniques are used to handle large
volume of data.

Examples: Merge Sort, Tape sort, Disk sort etc.

1) INSERTION SORT

In insertion sort, at each pass such number of data items is placed in sorted order. Let
K is an array that consists of ‘n’ elements from index 1 to index n. Sorting refers to the
process of rearranging the given elements of K in ascending order such that: K[1] ≤ K[2] ≤ . .
. . . . . . ≤ K[n]. For this, insertion sort procedure works as:

Step 1: Select the second data item and compare it with the first data item. If the second data
item is less than the first data item, then insert it before the first data item. Otherwise,
proceed with the next step.

Step 2: Select the third data item and compare it with the second data item. If it is less than
the second data item then compare it with the first data item. If it is less than the first
data item then insert it before the first data item. Otherwise, insert it in between the
first data item and the second data item.

Step 3: Repeat the above steps for n-1 times. At the end of the n-1 th pass, all elements of the
array are available in sorted order.

Functional procedure can be placed as:

int sort(int K[50], int n)


{
int i;
K[0] = -999;
for(i=2 ; i<=n ; i++)
{
temp = K[i];
j = i-1;
while( temp < K[j] )
{
K[j+1] = K[j];

Visvodaya Technical Academy Page 33

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

j = j – 1;

}
K[j+1] = temp;
}
return;
}
Example: Sort the following elements using insertion sort.
12 11 16 20 19

Solution: Given K[1:5] = 12 11 16 20 19

Consider K[0:5] = -999 12 11 16 20 19

Pass 1: temp = 11
11 < 12 TRUE
-999 12 12 16 20 19
11 < -999 FALSE
-999 11 12 16 20 19

Pass 2: temp = 16
16 < 12 FALSE
-999 11 12 16 20 19

Pass 3: temp = 20
20 < 16 FALSE
-999 11 12 16 20 19

Pass 4: temp = 19
19 < 20 TRUE
-999 11 12 16 20 20
19 < 16 FALSE
-999 11 12 16 19 20

Sorted Elements Are: 11 12 16 19 20

Analysis of Insertion Sort:

Insertion sort uses two loops. The outer loop executes n-1 times. For each outer loop,
inner loop executes from 0 to current time depending on the relationship between temp value
and the comparing value. On the average comparisons, the inner loop processes the data into
half of the sorted list. Hence, the function f(n) becomes

F(n) = n ((n+1)/2)

Visvodaya Technical Academy Page 34

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

= n2/2 + n/2
= O(n2)

The Worst case and Average case time complexity of insertion sort is O(n2).

VARIATIONS OF INSERTION SORT

Variations of insertion sort techniques are:


i) List insertion sort
ii) Binary insertion sort.

i) List / Linked Insertion Sort

List insertion sort procedure is a slight variation of insertion sort procedure. In this
procedure, elements are represented as a linked list rather than an array and must be in sorted
order. Initially procedure starts with an empty list or the elements with sorted order.

The basic function used for inserting a new element into the sorted list is:

struct list *P = START;


while( P != NULL )
{
if ( ( P→LINK == NULL ) || ( New→DATA < P→LINK→DATA)
{
New→LINK = P→LINK;
P→LINK = New;
break;
}
P = P→LINK;
}

Here, a new element can be inserted into its appropriate position with O(1) time and
uses insertion sort comparison procedure.

Example: Assume the initial list as:

1 2 3 4

10 2 27 3 49 4 78
START END

Insertion (36) : After insertion the list becomes

1 2 3 4

10 2 27 7 49 4 78
START END

Visvodaya Technical Academy Page 35

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

36 3
New

Hence, the list elements are : 10 27 36 49 78

ii) Binary Insertion Sort

Binary insertion sort procedure uses binary search procedure to find the proper
location to insert the new element in every pass. It reduces number of comparisons compared
to normal insertion sort. This procedure reduces to O(log i) time in each iteration sort
process. Program implementation of binary insertion sort procedure is as follows:

/* BINARY INSERTION SORT PROGRAM */

#include<stdio.h>
#include<conio.h>

void sort(int[],int);
int search(int[],int,int,int);

main()
{
int k[3]={6,2,10},i;
clrscr();
printf("\nBefore sorting elements are=\n");
for(i=0;i<3;i++)
printf("\t%d",k[i]);
sort(k,3);
printf("\nAfter sorting elements are=\n");
for(i=0;i<3;i++)
printf("\t%d",k[i]);
}
void sort(int k[3],int n)
{
int i,j,select,loc;
for(i=1;i<n;i++)
{
j=i-1;
select=k[i];
loc=search(k,0,j,select);
while(j>=loc)
{
k[j+1]=k[j];

Visvodaya Technical Academy Page 36

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

j=j-1;
}
k[j+1]=select;
}
}

int search(int k[3], int low, int high, int key)


{
int mid;
if(high<=low)
return (key>k[low]) ? (low+1) : low;
mid=(low+high)/2;
if(key == k[mid])
return mid+1;
else if(key > k[mid])
return search(k,mid+1,high,key);
else
return search(k,low,mid-1,key);
}

2) QUICK SORT (PARTITION EXCHANGE SORT)

Quick sort is a sorting technique based on the divide-and-conquer strategy. In this


sorting technique, array elements are divided into two sub arrays depending on a specialized
element called “pivot” element.

Let K is an array that consists of ‘n’ elements from index 1 to index n. Sorting refers
to the process of rearranging the given elements of K in ascending order such that: K[1] ≤
K[2] ≤ . . . . . . . . ≤ K[n]. For this quick sort procedure works as:

Step 1: Initialize the first element as pivot element.


Step 2: Initialize a variable i at the first index and another variable j at last index+1.
Step 3: Increment i value by 1 until K[i] ≥ pivot element.
Step 4: Decrement j value by 1 until K[j] ≤ pivot element.
If i < j Then
Interchange K[i] & K[j]
EndIf
Step 5: Repeat Step 3 and 4 until i ≥ j
Step 6: Interchange the values of K[j] and pivot element.

The above process refers to one pass. At the end of the pass, the pivot element is
positioned at its sorted position. At this stage, the elements before the pivot element are less

Visvodaya Technical Academy Page 37

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

than or equal to pivot element and after the pivot element are greater than or equal to the
pivot element.
Now, the same procedure is repeated on the elements before the pivot element as well
as on the elements after the pivot element.
When all passes are completed, then list of array elements are available in sorted
order.

Example: Sort the following elements using quick sort.


12 9 17 16 94
Pass 1:

K[1:5] 12 9 17 16 94

pivot = 12
i=1
j=6 1<6 TRUE

i=2 9 ≥ 12 FALSE
i=3 17 ≥ 12 TRUE
j=5 94 ≤ 12 FALSE
j=4 16 ≤ 12 FALSE
j=3 17 ≤ 12 FALSE
j=2

Here, i>j (3 > 2) TRUE


Interchange 9 & 12

K[1:5] = 9 12 17 16 94
K[1:1] K[3:5]

Pass 2:

K[3:5] = 17 16 94

pivot = 17
i =3
j=6 3<6 TRUE

i=4 16 ≥ 17 FALSE
i=5 94 ≥ 17 TRUE

j=5 94 ≤ 17 FALSE
j=4
Here, i>j (5 > 4) TRUE
Interchange 16 & 17

K[1:3] = 16 17 94

K[1:5] = 9 12 16 17 94

Visvodaya Technical Academy Page 38

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Sort list of elements are : 9 12 16 17 94

Example: Sort the following elements using quick sort.


76 92 11 24 49 33

ALGORITHM

QuickSort(K,LB,UB): Let K is an array that consists of ‘n’ elements from index 1 to


index n. Assume LB refers to the first index 1 and UB refers to the last index n at the
initial call. This procedure sorts the elements of K in ascending order..

Step 1: flag ← TRUE


Step 2: IF LB < UB THEN
i ← LB
j ← UB + 1
pivot ← K[LB]
Repeat WHILE flag
i ← i+1
Repeat WHILE K[i] < pivot
i ← i+1
EndRepeat
j ← j-1
Repeat WHILE K[j] > pivot
j ← j-1
EndRepeat
IF i < j THEN
Interchange K[i] and K[j] elements
ELSE
flag ← FALSE
ENDIF
EndRepeat
Interchange K[LB] and K[j] elements
Call QuickSort(K , LB , j-1)
Call QuickSort(K , j+1 , UB)
ENDIF
Step 3: RETURN

Analysis of Quick sort

1. The Worst case time complexity of quick sort is O (n2). It occurs when the list of
elements already in sorted order.

To calculate worst-case time complexity of quick sort, imagine the number of


comparisons to sort the array of n elements is f(n).

Visvodaya Technical Academy Page 39

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

An array of zero or one element is a sorted list.

Hence, f(0) = 0
f(1) = 0.

If n ≥ 2 then the recurrence relation is given by the function f(n) = f(n-1) + n

This equation can be solved recursively as:

f(n) = f(n-1) + n
f(n-1) = f(n-1-1) + n – 1 = f(n-2) + n – 1
f(n-2) = f(n-2-1) + n – 2 = f(n-3) + n – 2
.
.
.
f(1) = f(0) + 1
f(0) = 0

Therefore,
f(n) = n + (n-1) + (n-2) + - - - - - - - - + 1 + 0
= (n(n+1)) / 2
= (n2+n)/2
= (n2 / 2) + (n / 2)
= O(n2)
Hence,
Worst case time complexity of Quick sort is O(n2) time.

2. The Average case time complexity of quick sort is O(n logn), which is less
compare to worst case time complexity.

In this case, the pivot element is positioned at one appropriate location and splits the
array elements into two sub arrays. From this, the recurrence relation is given by the function

f(n) = (2/n) [ f(0) + f(1) + - - - - - + f(n-1) ] + n

Multiply both sides of this equation by n


n f(n) = 2 [ f(0) + f(1) + - - - - - + f(n-1) ] + n2

Replace n with n-1, then


(n-1) f(n-1) = 2 [ f(0) + f(1) + - - - - - + f(n-2) ] + (n-1)2

n f(n) – (n-1)f(n-1) = 2 f(n-1) + n2 – (n-1)2


nf(n) – nf(n-1) + f(n-1) = 2f(n-1) + n2 – ( n2 – 2n + 1)
nf(n) = 2f(n-1) – f(n-1) + nf(n-1) + n2 – n2 + 2n - 1
nf(n) = (n+1) f(n-1) + 2n – 1

Divide both sides by n(n+1), then

Visvodaya Technical Academy Page 40

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

(1/(n+1)) f(n) = (1/n) f(n-1) + (2/(n+1)) – ((1/(n(n+1)))


Solve this equation recursively with n-1, n-2, - - - - - we get
f(n) = (n+1) log n
f(n) = n log n + log n
= O(n logn)
Therefore,

The Average case time complexity of quick sort is O(n log n) time.
EXTERNAL SORTING

In external sorting process, some part of the data is held in primary memory during
the sorting process and the remaining data is in secondary storage devices that do not fit in
the primary memory. In general, external sorting techniques are used to handle large volume
of data.

Examples: Merge sort, Tape sort, Disk sort etc.

One of the most important external sorting techniques is merge sort technique.

MERGING ORDERED FILES

Merging is the process of combining two sorted files data into a single sorted file.
While performing merging operation, the two sub lists must be in sorted order.

Example:

1
File-1 1
3
2
5
3
File-3
MERGE 4
2 5
4 6
File-2
6 8
8

Here, File-1 and File-2 are merged into File-3. For this, compare the first value of
File-1 with the first value of File-2. Here, 1<2 then the value 1 of File-1 write into the output
file File-3. Now, compare the next value of File-1 with File-2 value and so on. Repeat the
same procedure until File-1 and File-2 data are empty. At this stage, entire data is available
in File-3 in sorted order.

MERGING UN-ORDERED FILES

Visvodaya Technical Academy Page 41

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

For unordered files data, apply Merge sort technique for sorting data that internally
uses merge process. Different variations of merge sort are:

 Iterative merge sort


 Recursive merge sort
 Natural merge sort

RECURSIVE MERGE SORT

Merge sort is also a sorting technique designed based on divide-and-conquer strategy.


Let K is an array that consists of ‘n’ elements from index 1 to index n. Sorting refers to the
process of rearranging the given elements of K in ascending order such that: K[1] ≤ K[2] ≤ . .
. . . . . . ≤ K[n].

In this sorting technique,

First divide the array elements into two sub arrays based on

Mid = (Low + High) / 2

Where,
Low is the first index of the array and High is the last index of the array.

Once, the sub arrays are formed, each set is individually sorted and the resulting sub
sequences are merged to produce a single sorted sequence of data elements.

Divide-and-Conquer strategy is applicable as splitting the array into sub arrays; and
combining operation is merging the sub arrays into a single sorted array.

Merging is the process of combining two sorted lists into a single sorted list. While
performing merging operation, the two sub lists must be in sorted order.

Example: Sort the following elements using merge sort.


12 9 17 16 94

Given k[1:5] = 12 9 17 16 94

Low = 1 High = 5 1<5 TRUE


Mid = (1+5)/2 = 3

Then, k[1:5] spitted into two sub arrays as: k[1:3] and k[4:5]

Consider k[1:3] = 12 9 17

Low = 1 High = 3 1<3 TRUE


Mid=(1+3)/2 = 2

Visvodaya Technical Academy Page 42

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Then, k[1:3] spitted into two sub array as: k[1:2] and k[3:3]

Consider k[1:2] = 12 9

Low = 1 High = 2 1<2 TRUE


Mid=(1+2)/2 = 1

Then, k[1:2] spitted into two sub array as: k[1:1] and k[2:2]

Apply Merge operation on k[1:1] and k[2:2], it produces a sorted list k[1:2] as
K[1:2] = 9 12

Apply Merge operation on k[1:2] and k[3:3], it produces a sorted list k[1:3] as
K[1:3] = 9 12 17

Consider k[4:5] = 16 94

Low = 4 High = 5 4<5 TRUE


Mid=(4+5)/2 = 4

Then, k[4:5] spitted into two sub array as: k[4:4] and k[5:5]

Apply Merge operation on k[4:4] and k[5:5], it produces a sorted list k[4:5] as
k[4:5] = 16 94

Apply Merge operation on k[1:3] and k[4:5], it produces a sorted list k[1:5] as
K[1:5] = 9 12 16 17 94

Sorted Elements Are: 9 12 16 17 94

ALGORITHM

MSort(Low, High): Let K is an array that consists of ‘n’ elements from index 1 to
index n. Low refers to the first index 1 and High refers to the last index n at the initial call.
This procedure sorts elements of K in ascending order.

Step 1: IF Low < High THEN


Mid ← (Low+High) / 2
Call MSort(Low,Mid)
Call MSort(Mid+1,High)
Call Merge(Low,Mid,High)
ENDIF
Step 2: RETURN

Visvodaya Technical Academy Page 43

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Merge(Low, Mid, High): This procedure merges the two sub sorted arrays into a single
sorted array.

Step 1: h ← Low
i ← Low
j ← Mid+1
Step 2: Repeat WHILE h ≤ Mid AND j ≤ High
IF K[h] ≤ K[j] THEN
S[i]←K[h]
h ← h+1
ELSE
S[i]←K[j]
j ← j+1
ENDIF
i ← i+1
EndRepeat
Step 3: IF h > Mid THEN
Repeat FOR p ← j TO High DO STEPS BY 1
S[i]←K[p]
i ←i+1
EndRepeat
ELSE
Repeat FOR p ← h TO Mid DO STEPS BY 1
S[i]←K[p]
i ←i+1
EndRepeat
ENDIF
Step 4: Repeat FOR p ← Low TO High DO STEPS BY 1
K[p] ← S[p]
EndRepeat

Analysis of Merge Sort

Merge sort consists of several passes over the input. The first pass merges segments
of size1, second pass merges segments of size2, and the i th pass merges segments of size 2i-1.
Thus, the total number of passes is ┌ log 2n ┐.

At each pass, merge process required O(n) time. For ┌ log 2n ┐ passes, the total
computing time becomes O(n log n) time.

The Worst case and Average case time complexity of merge sort is O(n logn) time.

Visvodaya Technical Academy Page 44

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Note:

The main disadvantage of merge sort is its storage representation. In merge sort
technique, merge process required an auxiliary (temporary) array which has same size as the
original array. Hence, it requires more space compared to other sorting techniques.

ITERATIVE MERGE SORT

Iterative merge sort begins by interpreting the input list as comprised of n sorted sub-
lists, each of size 1. In the first merge pass, these sub-lists are merged by pairs to obtain n/2
sub lists, each of size 2. In the second merge pass, these n/2 sub-lists are merged by pairs to
obtain n/4 sub lists. Each merge process reduces the number of sub-lists by half. Merge
passes are continued until the process reached to only one list.

Example: Assume the input list is:

26 , 5 , 77 , 1 , 61 , 11 , 59 , 15 , 48 , 19

For this,

The merge process diagrammatic view can be shown as:

26 5 77 1 61 11 59 15 48 19

5 26 1 77 11 61 15 59 19 48

1 5 26 77 11 15 59 61 19 48

1 5 11 15 26 59 61 77 19 48

1 5 11 15 19 26 48 59 61 77

Visvodaya Technical Academy Page 45

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

3) HEAP TREE

Suppose H is a complete binary tree. Then it will be termed as a heap tree / max
heap if it satisfies the property as:

 For each node N in H, the value at N is greater than or equal to the value of each of
the children of N.

Example:
9
5
8 4
4 8
5
7 2
6 3
In addition to this a min heap is possible, where the value at N is less than or equal to the
value of each of the children of N.

Example:
2
5
4 7
5 8
5
8 7
3 1

REPRESENTATION OF A HEAP TREE

Heap tree is a complete binary tree, it is better to represent with a single dimensional
array. In this case, there is no wastage of memory space between two non-null entries.

Example:
9
5
Visvodaya Technical8Academy 4 Page 46
4 8
7 5 Downloaded by Karan Kamble ([email protected])
lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Array Representation

1 2 3 4 5 6

95 84 48 76 23
2
3
OPERATIONS ON HEAP TREE

Basic operations on a heap tree are: insertion, deletion, merging etc.

Insertion into a heap tree: This operation is used to insert a new element into a
heap tree. Let K is an array that stores n elements of a heap tree. Assume an element of
information is given in the variable ‘key’ for insertion. Then insertion procedure works as:

 First adjoin key value at the end of K so that still the tree is a complete binary tree, but
not necessarily a heap.
 Then raise the key value to its appropriate position so that finally it is a heap tree.

The basic principle is that first add the data element into the complete binary tree.
Then compare it with the data in its parent node; if the value is greater than then the parent
node then interchange these two values. This procedure will continue between every two
nodes on the path from the newly inserted node to the root node till we get a parent whose
value is greater than its child or we reached at the root node.

Example: Insert a value 124 into the existing heap tree

14
0

85 45

7 2 3 1
5 5 5 5

55 6
5
Algorithm InHeap (Key): Let K is an array that stores a heap tree with ‘n’
elements. This procedure is used to store a new element Key into the heap tree.

Step 1: n ← n+1
Step 2: K[n] ← Key
Step 3: i←n
p ← i/2
Step 4: Repeat WHILE p > 0 AND K[p] < K[i]

Visvodaya Technical Academy Page 47

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Temp ← K[i]
K[i] ← K[p]
K[p] ← Temp
i←p
p ← p/2
End Repeat
Step 5: Return

Deletion of a node from heap tree: This operation is used to delete an


element from the existing heap tree. Deletion procedure works as:

 Assign the root node into a temporary variable as key.


 Replace the root node location by the last node in the heap tree. Then re-heap the tree
as:
o Let newly modified root node be the current node. Compare its value with its
two children node values. Let X is the child whose value is the largest value.
Then interchange the value of X with the value of the current node.
o Make X as the current node.
o Continue re-heap process, if the current node is not an empty node.

Example: Delete an element from the following heap tree

14
0

85 45

7 2 3 1
5 5 5 5

55 6
5

Algorithm Deletion (): This procedure removes root element of the heap tree
and rearranges the elements into heap tree format.

Step 1: IF n = 0 THEN
WRITE ‘ HEAP TREE EMPTY’
RETURN
ENDIF

Visvodaya Technical Academy Page 48

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Step 2: Temp ← K[1]


K[1] ← K[n]
Step 3: n ← n-1
i←1
Flag ← FALSE
Step 4: Repeat WHILE Flag = FALSE AND i < n
lchild ← 2 * i
rchild ← 2 * i + 1
IF lchild ≤ n THEN
X ← K[lchild]
ELSE
X ← -999
ENDIF
IF rchild ≤ n THEN
Y ← K[rchild]
ELSE
Y ← -999
ENDIF
IF K[i] > X AND K[i] > Y THEN
Flag ← TRUE
ELSEIF X > Y AND K[i] < X THEN
SWAP(K[i], K[lchild])
i ← lchild
ELSEIF Y > X AND K[i] < Y THEN
SWAP(K[i], K[rchild])
i ← rchild
ENDIF
ENDREPEAT
Step 5: RETURN

APPLICATION OF HEAP TREES

Two important applications of heap trees are: Heap sort and Priority queue
implementations.

Heap Sort:

Heap sort is also known as Tree sort. The procedure of heap sort is as follows:

Step 1: Build a heap tree with the given set of data elements.
Step 2: a) Delete the root node from the heap and place it in last location.
b) Rebuild the heap with the remaining elements.
Step 3: Continue Step-2 until the heap tree is empty.

Example: Sort the elements 33, 14, 65, 2 and 99 using heap sort.

Visvodaya Technical Academy Page 49

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Note: The worst case time complexity of heap sort is O(n logn) time.

END

UNIT-II

Stack, Queue and Linked Lists


Stacks, Stacks using Dynamic Arrays, Queues, Circular Queues using Dynamic Arrays,
Evaluation of Expressions, Multiple Stacks and Queues. Linked lists: Singly Linked Lists
and Chains, Representing Chains in C, Linked Stacks and Queues, Additional List operations,
Doubly Linked Lists.

DATA STRUCTURES

The logical or mathematical model of a particular organization of data is called as a


data structure.
(OR)
The class of data that can be characterized by its organization is known as a data
structure.

Data structures are classified into two categories such as:

a) Linear data structures


b) Non-linear data structures

a) Linear data structures: A data structure is said to be linear if the values are
arranged in a sequence order i.e., in linear fashion. Here, there is a relationship between the
adjacent elements of the list.
Examples: Arrays, Stack, Queues, Linked list etc.

b) Non-linear data structures: A data structure is said to be non-linear if the values are
arranged without any sequence order i.e., in non-linear fashion. Here, there is no relationship
between the adjacent elements of the list.
Examples: Trees, Graphs, Sets, Tables etc.

APPLICATIONS OF DATA STRUCTURES:

Data structures are extensively used in different application areas such as:
 Compiler design
 Operating system
 Numerical analysis
 Graphics
 Artificial intelligence
 Simulation etc.

Visvodaya Technical Academy Page 50

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

ABSTRACT DATA TYPE

Abstract Data Type (ADT) is a specification for the type of values that a data type can
store and the operations that can be performed on those values. It is just a specification
without any details on the implementation part.

Examples: Array ADT, Stack ADT, Queue ADT etc.

ARRAYS

An array is a collection of homogeneous/similar/same data type elements those are


stored in successive memory locations. Based on the number of subscripts used with the
array, it can be classified into different categories such as: Single/One dimensional arrays,
Double/Two dimensional arrays and Multi-dimensional arrays.

ADT Array
{
Data members: Collection of same type of data elements stored in successive
memory locations.

Operations: Insertion (Item,Pos) : Process of inserting an element Item


into the array at a specified position Pos.
Deletion (Pos): Process of deleting an element from the
specified position Pos of the array.
Display(): Process of printing elements of the array.
}

OPERATIONS ON ARRAYS

Various operations that can be performed on arrays are: Insertion, Deletion,


Traversing, Sorting, Searching etc.

Consider a single dimensional array K that consists of n elements from index 0 to n-1.
Thus, these operations are:

Insertion Operation:

Insertion operation is used to insert a new element into the array at a specified
position. At this stage, first a specified number of elements are shift towards right hand side
location to obtain the specified position location as empty. Then insert the new element at the
specified position.

Algorithm Insertion (Item , Pos): This procedure inserts a new element Item into the
specified position Pos of the array K.

Step 1: REPEAT FOR i ← n-1 TO Pos-1 DO STEPS BY -1


K[i+1] ← K[i]
ENDREPEAT

Visvodaya Technical Academy Page 51

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Step 2: K[Pos-1] ← Item


Step 3: RETURN

Example: Assume the initial status of the array K as:

0 1 2 3 4 5 6 7
K 10 47 28 32 70

Insertion (36, 3):

0 1 2 3 4 5 6 7
K 10 47 36 28 32 70

Deletion Operation:

Deletion operation is used to delete a particular element from the specified position of
the given array elements. At this stage, first remove the element from the specified position
and then shift the elements towards left hand side locations of the array.

Algorithm Deletion (Pos): This procedure deletes a specified position Pos element from
the array K.

Step 1: Key ← K[Pos-1]

Step 2: REPEAT FOR i ← Pos-1 TO n-2 DO STEPS BY 1


K[i] ← K[i+1]
ENDREPEAT

Step 3: RETURN Key

Example: Assume the initial status of the array K as:

0 1 2 3 4 5 6 7
K 10 47 28 32 70

Deletion (3):

0 1 2 3 4 5 6 7
K 10 47 32 70

Visvodaya Technical Academy Page 52

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Deleted Element = 28

Display Operation:

Display operation is used to print elements of the array.

Algorithm Display (): This procedure prints elements of the array K from index 0 to
index n-1.

Step 1: REPEAT FOR i ← 0 TO n-1 DO STEPS BY 1


WRITE K[i]
ENDREPEAT

Step 2: RETURN

Example: Assume the initial status of the array K as:

0 1 2 3 4 5 6 7
K 10 47 28 32 70

Display() :

Array Elements are: 10 47 28 32 70

APPLICATIONS OF ARRAYS

 Arrays are used in different application areas where same type of elements is
necessary to use its computations.
Examples: Student Roll Numbers, Names, Marks etc.

 Arrays are useful to represent matrix form of the given homogeneous data elements.

ARRAY DISADVANTAGES

 Arrays concept is a static allocation of memory i.e., memory size of the array must be
declared in the declaration statement and can’t be expanded. So that it leads to space
complexity problems.

Visvodaya Technical Academy Page 53

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

 Insertion and deletion operations are quite time consuming process. Since, in these
two cases elements must be shift to other locations before processing the actual
operations.

STACKS

A stack is a linear data structure in which an element may be inserted or deleted only
at one end, called the TOP of the stack. The diagrammatic representation of a stack is as
follows:

TOP

Insertion 2 Deletion

Consider the elements as: 10 20 30 40

When these elements are inserted into a stack of size 5, then


Insertion order is: 10 20 30 40

TOP 40 3
Status of Stack: 30
2 SIZE = 5
20
1
10
0

Now, same elements are deleted from the stack, then


Deletion order is: 40 30 20 10

Visvodaya Technical Academy Page 54

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Here, the order of deleted elements from the stack is reverse order in which they were
inserted into the stack. Hence, a stack is also known as a LIFO (Last-In-First-Out) list.
Since, the last inserted element is the first out coming element of the list.

Examples: Stack of trays, Stack of plates, Letter basket etc.,

REPRESENTATION OF STACK

Stacks may be represented in the memory in two ways. Those are:

a) Array representation (Static representation)


b) Linked representation (Dynamic representation)

a) Array Representation

Static representation of a stack uses array concept. In this case, assume S is an array
used to represent a stack with a maximum size as ‘N’. Initially no elements are available in
the stack S. As per C language array index starts from 0 th location. At this stage, a variable
TOP is set at -1 position. Then, the status of the stack is as follows:

N-1

|
Status of Stack:
|

1 TOP = -1

Example: Assume N = 4 and two elements 10 and 20 are inserted into the stack,
and then the status of stack is:

2
20
TOP 1
10
0
S
b) Linked Representation

Dynamic representation of a stack uses linked list concept. In general, single linked
list structure is sufficient to represent any stack.

Visvodaya Technical Academy Page 55

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

In linked representation of a stack, each node consists of two parts namely DATA field
and LINK field. DATA field contains information part of the element and LINK field
contains address of the next node of the list. Then the linked representation of a stack is as
follows:
1 2 3 4
59 2 38 3 64 4 26

START TOP

ADT Stack
{
Data members: Linear collection of zero or more elements.
Operations: void push(Item): Process of inserting an element Item into
the stack.
int pop(): Process of deleting top element of the stack.
int peek(): Process of printing top element of the stack.
int isEmpty(): Process of checking whether the stack is empty
or not.
int isFull(): Process of checking whether the stack is full or
not.
void display(): Process of printing elements of the stack.
}

OPERATIONS ON STACKS

Basic operations on the stack are: push, pop, peek, empty, full, display etc.

For implementing these operations, consider array representation of the stack.


Assume S is an array used to perform stack operations with a maximum size as ‘N’. Initially
elements are not available in the stack S. At this stage, set the TOP variable at -1 position of
S.

Example: Let N = 3

Status of Stack:
2

1 TOP = -1

Push Operation:

The process of inserting an element into the stack is known as push operation.

At every push operation, first the variable TOP is incremented by 1 and then the new
element is inserted into top position of the stack.

Visvodaya Technical Academy Page 56

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

While performing push operation, if empty locations are not available to insert the
element then the status of the stack is called as “STACK OVERFLOW” or “STACK FULL”.

Algorithm push(x): This procedure inserts a new element x into top position of the stack S.

Step 1: IF TOP = N-1 THEN


WRITE ‘STACK OVERFLOW’
RETURN
ENDIF
Step 2: TOP ← TOP + 1
Step 3: S[TOP] ← x
Step 4: RETURN

Pop Operation:

The process of deleting an element from the stack is known as pop operation.

At every pop operation, first delete the element from top position of the stack and then
the variable TOP is decremented 1.

While performing pop operation, if elements are not available to delete from the stack
then the status of the stack is called as “STACK UNDERFLOW” or “STACK EMPTY”.

Algorithm pop(): Function is used to delete the topmost element from the stack S.

Step 1: IF TOP = -1 THEN


WRITE ‘STACK UNDERFLOW’
RETURN -1
ENDIF
Step 2: K ← S[TOP]
Step 3: TOP ← TOP - 1
Step 4: RETURN K

Peek Operation:

The process of printing the topmost element of the stack is known as peek operation.

Algorithm peek(): Function returns the top element from the stack S if exists;
otherwise, it returns -1.

Step 1: IF TOP = -1 THEN


RETURN -1
ELSE
RETURN S[TOP]
ENDIF

Empty Operation:

Visvodaya Technical Academy Page 57

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Empty operation is used to check whether the stack is empty or not.

Algorithm empty(): Function returns 1 if the stack is empty; otherwise, it returns 0.

Step 1: IF TOP = -1 THEN


RETURN 1
ELSE
RETURN 0
ENDIF
Full Operation:

Full operation is used to check whether the stack is full with elements or not.

Algorithm full(): Function returns 1 if the stack is full; otherwise, it returns 0.

Step 1: IF TOP = N-1 THEN


RETURN 1
ELSE
RETURN 0
ENDIF

Display Operation:

Display operation is used to print elements of the stack S.

Algorithm display(): Function prints elements of the stack S.

Step 1: IF TOP ≠ -1 THEN


REPEAT FOR i ← 0 TO TOP DO STEPS BY 1
WRITE S[i]
ENDREPEAT
ELSE
WRITE ‘STACK EMPTY’
ENDIF
Step 2: RETURN

Visvodaya Technical Academy Page 58

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

APPLICATIONS OF STACK

Stack operations are utilized by the computer system to do various tasks. Some of the
important applications areas are:

1) Evaluation of arithmetic expressions


2) Code generation for stack machines
3) Implementation of recursion
4) Record management etc.

1) Evaluation of Arithmetic Expressions

An arithmetic expression is formed with the combination of arithmetic operators and


operands.

Example: 2x + 3y = 74

Any arithmetic expression can be represented in three ways. Those are:

 Infix notation
 Prefix notation
 Postfix notation.

Infix Notation: In infix notation, operators are placed in between the operands of the
mathematical expression. In general ordinary mathematical expressions are represented in
terms of infix expressions.

Example: A+B
2-7+4/3
(14+25) / (9+2-4) etc.

Prefix Notation: In prefix notation, operators are placed before the operands of the
mathematical expression. Prefix notation is also known as polish notation.

Example: +AB
- + 2 14 9 etc.

Postfix Notation: In postfix notation, operators are placed after the operands of the
mathematical expression. Postfix notation is also known as reverse polish notation.

Visvodaya Technical Academy Page 59

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example: AB+
5 6 2 + * 12 4 / - etc.

Computer system evaluates the given arithmetic expression in two steps.

First it converts the given infix expression into postfix expression and then evaluates
the postfix expression. In both stages, stack is the important tool used to perform the task.

A) Conversion of Infix Expression into Postfix Expression

Let Q is an arithmetic expression written in infix notation consists of the operators +


(Addition), - (Subtraction), * (Multiplication), / (Division), ^ (Power), left and right
parentheses. For this assume the priority order as:

OPERATOR PRIORITY

^ 3
* / 2
+ - 1

Then the following procedure converts the given infix expression Q into its equivalent
postfix expression P.

Step 1: Push “(“ onto the STACK, and add “)” to the end of Q.

Step 2: Scan Q from left to right and repeat Steps 3 to 6 for each element of Q
until the STACK is empty:

Step 3: If an operand is encountered, add it to P.

Step 4: If a left parenthesis is encountered, push it onto the STACK.

Step 5: If an operator © is encountered, then:


a) Repeatedly pop elements from the STACK and add to P each
operator which has the same precedence as or higher precedence
than ©.
b) Add © to the STACK.

Step 6: If a right parenthesis is encountered, then:


a) Repeatedly pop elements from the STACK and add to P each
operator until a left parenthesis is encountered.
b) Remove the left parenthesis.
Step 7: EXIT

Visvodaya Technical Academy Page 60

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

The above procedure transforms the infix expression Q into its equivalent postfix
expression P. It uses a stack to temporarily hold operators and left parenthesis.

The postfix expression P will be constructed from left to right using the operands
from Q and the operators which are removed from the stack. Procedure starts by pushing a
left parenthesis onto stack and adding a right parenthesis at the end of Q. The procedure is
completed when the stack is empty.

Example: Convert the expression A + B into postfix expression.

Solution: Given infix expression Q=A+B)

Assume priority of operators as : ^ = 3


* / = 2
+ - = 1

Scanned STACK Postfix Notation Remarks


Element P
- ( - -
A ( A -
+ (+ A +=1
(=0
0>=1 FALSE
B (+ AB -
) EMPTY AB+ -

Resultant Postfix Expression = A B +

Example: Convert the following infix expressions into postfix expressions.


a) ( A + B ) * ( C – D ) Ans: A B + C D - *
b) 5 * ( 6 + 2 ) – 12 / 4 Ans: 5 6 2 + * 12 4 / -
c) A + ( B * C ) / D Ans: A B C * D / +

B) Evaluation of Postfix Expression

Suppose P is an arithmetic expression written in postfix notation. The following


procedure evaluates the expression P and uses a STACK to hold operands.

Procedure

Step 1: Add a right parenthesis “)” at the end of P.

Step 2: Scan P from left to right and repeat Steps 3 and 4 for each element of P
until the right parenthesis is encountered.

Step 3: If an operand is encountered, put it on the STACK.

Visvodaya Technical Academy Page 61

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Step 4: If an operator © is encountered, then:


a) Remove the two top elements of the STACK, where A is the top
element and B is the next-to-top element.
b) Evaluate B © A.
c) Place the result of (b) back on the STACK.

Step 5: Set result VALUE equal to the top element of the STACK.

Step 6: EXIT.
Example: Evaluate the postfix expression
5 6 2 + * 12 4 / -

Solution: Given Postfix Expression P = 5 6 2 + * 12 4 / - )

Scanned STACK Operation


Element
5 5 -
6 5 6 -
2 5 6 2 -
+ 5 8 A=2
B=6
6+2 = 8
* 40 A=8
B=5
5*8 = 40
12 40 12 -
4 40 12 4 -
/ 40 3 A=4
B = 12
12/4 = 3
- 37 A=3
B = 40
40-3 = 37
) 37 = Result Value -

Result Value = 37

Examples: Evaluate the following postfix expressions:


a) 3 9 2 3 ^ * + Ans: 75
b) 7 4 + 14 3 - * Ans: 121

2) Implementation of Recursion

A function calls itself is called recursion and the function is called as recursion
function. In case of recursive procedures stack is the important tool used by the compiler for
storing addresses temporarily while moving the control from one place to another place.

Visvodaya Technical Academy Page 62

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

In recursion procedure, statements are expanded in forward direction and values are
substituted in backward direction.

Example: Consider recursive code for factorial of a given number.


int Fact(int N)
{
if(N = = 0)
return 1;
else
return N * Fact(N-1);
}
Assume N = 4 ; then evaluation procedure works as:

Step 1: 4! = 4 * Fact (4-1)


2: 3! = 3 * Fact (3-1)
3: 2! = 2 * Fact (2-1)
4: 1! = 1 * Fact (1-1)
5: 0! = 1 : return 1
6: 1! = 1*1=1: return 1
7: 2! = 2*1=2: return 2
8: 3! = 3*2=6: return 6
9: 4! = 4 * 6 = 24 : return 24

Here, Steps 1 to 4 are implemented in stack as push operations. Then subsequent pop
operations will evaluate up to step 9 and produces result value.

Stack Advantages:

 Insertion and deletion operations can be made easily compared to array insertion and
deletion operations.
 Stack operations can be performed very easily and efficiently.
 Time complexity of push operation is O(1) time.
 Time complexity of pop operation is O(1) time.

Stack Disadvantages:

Consider the status of the stack is as:

2 30

1 20

10
0

STACK

Visvodaya Technical Academy Page 63

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Now, if we want to delete the first inserted element 10 direct deletion is not possible.
First delete the element 30, then 20 and then the actual required element 10. After deletion
the actual element 10 again inserts the previous deleted elements 20 and 30 into the stack.
It’s a time consuming process.

***
QUEUES

A queue is a linear data structure in which elements can be inserted only at one end,
called REAR end and elements can be deleted from the other end, called FRONT end of the
list. The diagrammatic representation of a queue is as follows:

FRONT REAR

Deletions QUEUE Insertions

Consider the elements as: 10 20 30 40

When these elements are inserted into a queue of size 5, then


Insertion order is: 10 20 30 40

0 1 2 3 4
Status of Queue:
10 20 30 40 SIZE = 5

FRONT REAR

Now, same elements are deleted from the queue, then


Deletion order is: 10 20 30 40

Here, the order of deleted elements from the queue is the same order in which they
were inserted into the queue. Hence, a queue is also known as a FIFO (First-In-First-Out)
list. Since, the first inserted element is the first out coming element of the list.

Visvodaya Technical Academy Page 64

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Examples: Ticket issuing counter process


Vehicles entry system in a single line road
Printing multiple programs on a printer etc.

REPRESENTATION OF QUEUES

Queues may be represented in the memory in two ways. Those are:

a) Array representation (Static representation)

b) Linked representation (Dynamic representation)

a) ARRAY REPRESENTATION

Static representation of a queue uses array concept. In this case, assume Q is an array
used to represent a queue with a maximum size as ‘N’. Initially no elements are available in
the queue Q. At this stage, two variables FRONT and REAR set at -1 position. Then, the
status of the queue is as follows:

0 1 - - N-1
FRONT = -1
REAR = -1
Q

Example: Assume N = 4 and two elements 10 and 20 are inserted into the queue,
and then the status of queue is:

0 1 2 3
Status of Queue:
10 20 Q SIZE = 4

FRONT REAR

b) LINKED REPRESENTATION

Dynamic representation of a queue uses linked list concept. In general, single linked
list structure is sufficient to represent any queue.
In linked representation of a queue, each node consists of two parts namely DATA
field and LINK field. DATA field contains information part of the element and LINK field

Visvodaya Technical Academy Page 65

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

contains address of the next node of the list. Then the linked representation of a queue is as
follows:

1 2 3 4
59 2 38 3 64 4 26

FRONT REAR

ADT Queue
{
Data members: Linear collection of zero or more elements.

Operations: void enqueue(Item): Process of inserting an element Item


into the queue.
int dequeue(): Process of deleting an element from the queue.
int frontelement(): Process of printing front element of the
queue.
int rearelement(): Process of printing rear element of the
queue.
int isEmpty(): Process of checking whether the queue is
empty or not.
int isFull(): Process of checking whether the queue is full or
not.
void display(): Process of printing elements of the queue.
}

TYPES OF QUEUES

Depending on the way of performing operations on the queue data structure, queues
can be classified into four types as:

a) Linear Queue
b) Circular Queue
c) Deque
d) Priority Queue

a) LINEAR QUEUE

A linear queue is a linear data structure in which elements can be inserted only at one end,
called REAR end and elements can be deleted from the other end, called FRONT end of the
list. The diagrammatic representation of a linear queue is as follows:

Visvodaya Technical Academy Page 66

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

FRONT REAR

Deletions QUEUE Insertions

OPERATIONS ON LINEAR QUEUE

Basic operations on the queue are: enqueue, dequeue, front element, rear element,
empty, full, display etc.

For implementing these operations, consider array representation of the queue.


Assume Q is an array used to perform queue operations with a maximum size as ‘N’.
Initially elements are not available in the queue Q. At this stage, set two variables F and R at
-1 that represents FRONT and REAR ends of the list.

Example: Let N = 3
0 1 2
Status of Queue: F = -1
R = -1

Q
Enqueue Operation:

The process of inserting an element into the queue is known as enqueue operation.

For every enqueue operation, first the variable REAR is incremented by 1 and then
the new element is inserted into REAR position of the queue.

While performing enqueue operation, if empty locations are not available to insert the
element then the status of the queue is called as “QUEUE OVERFLOW” or “QUEUE FULL”.

Algorithm enqueue(x): This procedure inserts a new element x into REAR position of
the queue Q.

Step 1: IF R = N-1 THEN


WRITE ‘LINEAR QUEUE OVERFLOW’
RETURN
ENDIF
Step 2: R←R+1
Q[R] ← x
Step 3: IF F = -1 THEN
F←0
ENDIF
Step 4: RETURN

Visvodaya Technical Academy Page 67

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Dequeue Operation:

The process of deleting an element from the queue is known as dequeue operation.

For every dequeue operation, first delete an element from FRONT position of the
queue and then the variable FRONT is incremented 1.

While performing dequeue operation, if elements are not available to delete from the
queue then the status of the queue is called as “QUEUE UNDERFLOW” or “QUEUE
EMPTY”.
Algorithm dequeue(): Function is used to delete the FRONT position element
from the queue Q.

Step 1: IF F = -1 THEN
WRITE ‘LINEAR QUEUE UNDERFLOW’
RETURN -1
ENDIF
Step 2: K ← Q[F]
Step 3: IF F = R THEN
F←R←-1
ELSE
F←F+1
ENDIF
Step 4: RETURN K

Front Element Operation: This operation is used to print FRONT position element
of the Queue Q.

Algorithm felement(): Function returns the FRONT position element of


the queue Q if exists; otherwise, it returns -1.

Step 1: IF F = -1 THEN
RETURN -1
ELSE
RETURN Q[F]
ENDIF

Rear Element Operation: This operation is used to print REAR position element
of the Queue Q.

Algorithm relement(): Function returns the REAR position element of


the queue Q if exists; otherwise, it returns -1.

Step 1: IF R = -1 THEN
RETURN -1
ELSE
RETURN Q[R]
ENDIF

Visvodaya Technical Academy Page 68

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Empty Operation: Empty operation is used to check whether the queue is empty
or not.

Algorithm empty(): Function returns 1 if the queue is empty; otherwise, it


returns 0.

Step 1: IF F = -1 THEN
RETURN 1
ELSE
RETURN 0
ENDIF
Full Operation:

Full operation is used to check whether the queue is full with elements or not.

Algorithm full(): Function returns 1 if the queue is full; otherwise, it returns 0.

Step 1: IF R = N-1 THEN


RETURN 1
ELSE
RETURN 0
ENDIF

Display Operation:

Display operation is used to print elements of the Queue Q.

Algorithm display(): Function prints elements of the queue Q.

Step 1: IF F = -1 THEN
WRITE ‘LINEAR QUEUE EMPTY’
ELSE
REPEAT FOR i ← F TO R DO STEPS BY 1
WRITE Q[i]
ENDREPEAT
ENDIF
Step 2: RETURN

Linear Queue Disadvantages:

Consider the following status of the linear queue as:

Example: Let N = 5
0 1 2 3 4
Status of Queue Q: 30 40 50

Visvodaya Technical Academy Page 69

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

F R

At this stage, if we tried to insert an element into the queue, it prints a message as
“Queue Overflow” that refers to empty locations is not available. But still empty locations
are available at the FRONT end of the list.

From this, Even though empty locations are available at FRONT end and REAR
reached to N-1th locations then further insertion is not possible in linear queues.

b) CIRCULAR QUEUE

In circular queue, elements are arranged in circular fashion. Here, when REAR end
reaches to N-1th location and still empty locations are available at the FRONT end of the list
then REAR variable is set back to starting location. The logical view of a circular queue can
be represented as:

Consider the basic operations on circular queue as Insertion, Deletion and Display
operations.

Algorithm Insertion(x): This procedure inserts a new element x into REAR position of
the circular queue CQ.

Step 1: IF (F = 0 AND R = N-1) OR (F = R+1) THEN


WRITE ‘CIRCULAR QUEUE OVERFLOW’
RETURN
ENDIF
Step 2: IF R = N-1 THEN
R←0
ELSE
R←R+1
ENDIF
Step 3: CQ[R] ← x
Step 4: IF F = -1 THEN
F←0
ENDIF
Step 5: RETURN

Visvodaya Technical Academy Page 70

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Algorithm Deletion(): Function is used to delete the FRONT position element


from the circular queue CQ.

Step 1: IF F = -1 THEN
WRITE ‘CIRCULAR QUEUE UNDERFLOW’
RETURN -1
ENDIF
Step 2: K ← CQ[F]
Step 3: IF F = R THEN
F←R←-1
ELSEIF F = N-1 THEN
F←0
ELSE
F←F+1
ENDIF
Step 4: RETURN K

Algorithm display(): Function prints elements of the circular queue CQ.

Step 1: IF F = -1 THEN
WRITE ‘CIRCULAR QUEUE EMPTY’
ELSE
IF F ≤ R THEN
REPEAT FOR i ← F TO R DO STEPS BY 1
WRITE CQ[i]
ENDREPEAT
ELSE
REPEAT FOR i ← F TO N-1 DO STEPS BY 1
WRITE CQ[i]
ENDREPEAT
REPEAT FOR i ← 0 TO R DO STEPS BY 1
WRITE CQ[i]
ENDREPEAT
ENDIF
ENDIF
Step 2: RETURN

c) DEQUE

A deque (Double Ended Queue) is a linear data structure in which elements may be
inserted and deleted at either ends of the list. The diagrammatic representation of a deque is
as follows:

Insertions Insertions

Visvodaya Technical Academy Page 71

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

FRONT REAR

Deletions Deletions

Deque can be classified into two types as:


i) Input-Restricted Deque
ii) Output-Restricted Deque

i) Input-Restricted Deque: An input-restricted deque is a deque which allows


insertions only at one end of the list but allows deletions at both ends of the list. The
diagrammatic representation of an input-restricted deque is as follows:

Insertions

FRONT REAR

Deletions Deletions

ii) Output-Restricted Deque: An output-restricted deque is a deque which allows


deletions only at one end of the list but allows insertions at both ends of the list. The
diagrammatic representation of an output-restricted deque is as follows:

Insertions Insertions

FRONT REAR

Deletions

d) PRIORITY QUEUE

A priority queue is a collection of elements such that each element has been assigned
a priority and such that the order in which elements are deleted and processed comes from the
following rules:

 An element of higher priority is processed before any element of lower


priority.
 Two elements with the same priority are processed according to the order in
which they were added to the queue.

Example: Consider the following jobs/elements and their properties as:

JOB/ELEMENT PRIORITY

Visvodaya Technical Academy Page 72

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

10 3
20 2
30 1
40 2

Then the status of the priority queue after processing these elements is:

0 1 2 3 4
30 20 40 10
FRONT REAR

APPLICATION OF QUEUES

Queues are used in different application areas such as: simulation, multiprogramming
environments, job scheduling applications etc.

CPU Scheduling in Multiprogramming Environments

In multiprogramming environment a single CPU has to serve more than one program
simultaneously. In this case, scheduling is to classify the work load according to its
characteristics and to maintain separate process queues. Process will assign to their
respective queues. Then CPU will service the processes as per the priority of the queues.

Example:

High Priority Queue


C
Multiprogramming
Medium Priority Queue P
Jobs
U
Low Priority Queue

Here, high priority programs are processed first before the medium and low priority
queue jobs. After completing high priority queue program, then CPU serve its service to
medium priority queue programs. Finally it serves to lowest priority programs.

Round Robin Algorithm

Round Robin (RR) algorithm is a scheduling algorithm designed for time sharing
systems. The problem statement can be stated as:

Visvodaya Technical Academy Page 73

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Assume there are n processes P1, P2, - - - - , Pn served by the CPU. Different
processes require different execution time. Suppose sequence of processes are arrivals
according to their subscripts as P1 comes first than P2 and P2 comes first than P3 and so on.

Consider the following table consists of different processes and their burst time to
complete the execution.

PROCESS BURST TIME


P1 7
P2 18
P3 5

Assume the time quantum is 4 units. For this the total execution procedure with RR
algorithm can be shown as:

P1 finished P3 finished P2 finished

P1 P2 P3 P1 P2 P3 P2 P2 P2

0 4 8 12 15 19 20 24 28 30

To implement such execution, all the programs are currently under execution are
maintained in a circular queue. When a process finished its execution then this process is
deleted from the queue and whenever a new process arrives it is inserted into tail of the
queue.

Advantages of Queues:

 As similar to stacks, insertion and deletion operations can be performed easily in


queues.
 Time complexity of enqueue operation is O(1) time.
 Time complexity of dequeue operation is O(1) time.

***

LINKED LIST

“A linked list is a linear collection of elements called NODES and the order of the
elements is given by means of LINKS / POINTERS”. The diagrammatic representation of a
linked list is as follows:

1 2 3
10 2 20 3 30

Visvodaya Technical Academy Page 74

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

START END

In array implementation of data structures, elements are organized in sequentially.


For this, the size of the array must be specified at the beginning of the list and
implementation can be done only on limited number of elements. Whereas, linked list is a
dynamic implementation and can be capable to implement on large number of elements. In
linked list, nodes are created with the concept of self-referential structure.

Representation of a Linked list in Memory

Linked list can be represented in memory in two ways as: Static representation and
Dynamic representation.

Static Representation:

Static representation of a linked list uses array concept. Here, the list maintains two
arrays – one array as DATA array and other array as LINK array.

Example: DATA LINK

4 4

3 30 3

2 20 3 2

START 1 10 2 1

0 0

Dynamic Representation:

Dynamic representation of a linked list uses dynamic memory allocation concept.


Here, nodes are created in run time based on their requirements and establishes links in
between the nodes.

Example:

1 2 3
10 2 20 3 30

START END

Visvodaya Technical Academy Page 75

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

TYPES OF LINKED LISTS

Depending on the number of parts of a node and the way of establishing links in
between the nodes, linked list can be classified into different categories as:

1) Single linked list


2) Double linked list
3) Circular linked list
4) Header linked list

1. SINGLE LINKED LIST

In single linked list, each node is divided into two parts as INFO/DATA part and
LINK part.

DATA LINK

NODE

Where, the first part DATA field consist information part of the element and the
second part LINK field consist address of the next node in the list.

Example:
1 2 3
59 2 38 3 64

START END

Here, START and END are two pointer variables that points to beginning and ending
nods of the list. The LINK field of the last node filled with a special pointer called NULL
pointer.

Basic operations performed on the single linked list are:

i) Creating a list
ii) Traversing the list
iii) Insertion of a node into the list
iv) Deletion of a node from the list
v) Counting number of elements
vi) Searching an element etc.,

i) Creating a list

Visvodaya Technical Academy Page 76

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Creating a list refers to the process of creating nodes of the list and arranges links in
between the nodes of the list.

In single linked list, nodes are created using self-referential structure as:

typedef struct list


{
int DATA;
struct list *LINK;
}NODE;

Now, create new nodes with the format as: NODE *NEW;

Initially no elements are available in the list. At this stage, set two pointer variables
START and END to NULL pointer as:

NODE *START = NULL, *END = NULL;

Algorithm creation (): This procedure creates a single liked list with the
specified number of elements.

Step 1: Repeat WHILE TRUE


READ an element as x
IF x = -999 THEN
RETURN
ELSE
Allocate memory for a NEW node
DATA(NEW) ← x
LINK(NEW) ← NULL
IF START = NULL THEN
START ← END ← NEW
ELSE
LINK(END) ← NEW
END ← NEW
ENDIF
ENDIF
EndRepeat

ii) Traversing the list

Traversing the list refers to the process of visiting every node of the list exactly once
from the first node to the last node of the list. To display the elements of the single linked
follow the procedure as:

Step 1 - Check whether list is Empty or not.

Step 2 - If it is Empty then, display 'List is Empty' and terminate the function.

Visvodaya Technical Academy Page 77

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize
with start. Keep displaying temp → data and then shift temp to next location until
temp is not equal to NULL pointer.

Algorithm display(): This procedure is used to display the elements of the single
linked list from the first node to the last node.

Step 1: TEMP ← START


Step 2: IF START ≠ NULL THEN
Repeat WHILE TEMP ≠ NULL
WRITE DATA(TEMP)
TEMP ← LINK(TEMP)
EndRepeat
ELSE
WRITE ‘LIST EMPTY’
ENDIF
Step 3: RETURN

Example: Assume initial status of the list as:

1 2 3
59 2 38 3 64

START END

Display(): Single linked list elements are: 59 38 64

iii) Insertion of a node into the list

The process of inserting a node into the single linked list falls into three cases as:

 Front insertion
 Rear insertion
 Any position insertion

Case 1: Front Insertion: In this case, a new node is inserted at front position of
the single linked list. For this, follow the procedure as:

Step 1 - Create a NEW Node with given value.

Step 2 - Check whether list is Empty or not.

Step 3 - If it is Empty then, set START and END pointers to NEW node.

Step 4 - If it is Not Empty then, set LINK(NEW) with START pointer and move
START pointer to NEW node.

Visvodaya Technical Academy Page 78

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Algorithm Finsertion(x): This procedure inserts an element x at front end of the list.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← x
LINK(NEW) ← NULL
Step 2: IF START = NULL THEN
START ← END ← NEW
ELSE
LINK(NEW) ← START
START ← NEW
ENDIF
Step 3: RETURN

Example: Assume initial status of the list as:

1 2 3
59 2 38 3 64

START END

Finsertion (26):

0 1 2 3
26 1 59 2 38 3 64

START END
NEW

Case 2: Rear Insertion: In this case, a new node is inserted at rear position of
the single linked list. For this, follow the procedure as:

Step 1 - Create a NEW Node with given value.

Step 2 - Check whether list is Empty or not. If it is Empty then, set START and
END pointers to NEW node.

Step 3 - If it is Not Empty then, set LINK(END) with NEW pointer and move END
pointer to NEW node.

Algorithm Rinsertion(x): This procedure inserts an element x at rear end of the list.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← x
LINK(NEW) ← NULL
Step 2: IF END = NULL THEN
START ← END ← NEW

Visvodaya Technical Academy Page 79

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

ELSE
LINK(END) ← NEW
END ← NEW
ENDIF
Step 3: RETURN

Example: Assume initial status of the list as:

1 2 3
59 2 38 3 64

START END

Rinsertion (26):

1 2 3 4
59 2 38 3 64 4 26

START END
NEW

Case 3: Any Position Insertion: In this case, a new node is inserted at a specified
position of the single linked list. For this, follow the procedure as:

Step 1 - Create a NEW Node with given value.

Step 2 – Set a temporary variable PTR points to START and move the PTR variable
up to the specified location of the list along with another temporary variable TEMP.
When the specified location achieved, establish links in between NEW, PTR and
TEMP variables.

Algorithm Anyinsertion(x, pos): This procedure inserts an element x at the


specified position pos of the list.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← x
LINK(NEW) ← NULL
Step 2: k←1
PTR ← START
Step 3: Repeat WHILE k < pos
TEMP ← PTR
PTR ← LINK(PTR)
k ← k+1
EndRepeat
Step 4: LINK(TEMP) ← NEW
LINK(NEW) ← PTR
Step 5: RETURN

Example: Assume the initial status of the list as:


Visvodaya Technical Academy Page 80

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

1 2 3 4
59 2 38 3 64 4 26

START END

Anyinsertion (14 , 3):

1 2 6 3 4
59 2 38 6 14 3 64 4 26

START NEW END

iv) Deletion of a node from the list

Deleting an element from the single linked list falls into three categories as:

 Front deletion
 Rear deletion
 Any position deletion

Case 1: Front Deletion: In this case, front node information is deleted from the
single linked list. For this, follow the procedure as:

Step 1 - Check whether list is Empty or not.

Step 2 - If it is Empty then, display 'List is Empty, Deletion is not possible' and
terminate the function.

Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize
with START.

Step 4 - Check whether list is having only one node or not. If it is TRUE then set
START and END to NULL pointer; otherwise, change START to LINK(START) and
LINK(TEMP) if filled with NULL pointer.

Algorithm Fdeletion( ): This function deletes the front element of the list.

Step 1: IF START = NULL THEN


RETURN -1
ELSE
k← DATA(START)
IF START = END THEN
START ← END ← NULL
ELSE
TEMP ←START
START ← LINK(START)
LINK(TEMP) ← NULL
Visvodaya Technical Academy Page 81

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Release memory of TEMP


ENDIF
RETURN k
ENDIF

Example: Assume the initial status of the list as:

1 2 3 4
59 2 38 3 64 4 26

START END

Fdeletion( ): Front Deleted Element = 59

2 3 4
38 3 64 4 26

START END

Case 2: Rear Deletion: In this case, rear node information is deleted from the
single linked list.

Step 1 - Check whether list is Empty or not.

Step 2 - If it is Empty then, display 'List is Empty, Deletion is not possible' and
terminate the function.

Step 3 - If it is Not Empty then, define a Node pointer ‘ptr’ at START and 'temp' at
END pointer.

Step 4 - Check whether list is having only one node or not. If it is TRUE then set
START and END to NULL pointer; otherwise, move ptr link to the node which is
before the temp and make the appropriate links between END and ptr pointers.

Algorithm Rdeletion( ): This function deletes the rear element of the list.

Step 1: IF END = NULL THEN


RETURN -1
ELSE
k← DATA(END)
IF START = END THEN
START ← END ← NULL
ELSE
PTR ← START

Visvodaya Technical Academy Page 82

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

TEMP ← END
Repeat WHILE LINK(PTR) ≠ END
PTR ← LINK(PTR)
EndRepeat
END ← PTR
LINK(END) ← NULL
Release memory of TEMP
ENDIF
RETURN k
ENDIF

Example: Assume the initial status of the list as:

1 2 3 4
59 2 38 3 64 4 26

START END

Rdeletion( ): Rear Deleted Element = 26

1 2 3
59 2 38 3 64

START END

Case 3: Any Position Deletion: In this case, a specified position element is


deleted from the single linked list.

Step 1 - Check whether list is Empty or not.

Step 2 - If it is Empty then, display 'List is Empty, Deletion is not possible' and
terminate the function.

Step 3 - If it is Not Empty then, set a temporary variable PTR points to START and
move the PTR variable up to the specified location of the list along with another
temporary variable TEMP. When the specified location achieved, change links in
between NEW, PTR and TEMP variables.

Algorithm Anydeletion(pos): This function deletes the element of the list from the
specified position pos.

Step 1: IF START = NULL Then


RETURN -1
ELSE

Visvodaya Technical Academy Page 83

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

PTR ← START
p←1
Repeat WHILE p < pos
TEMP ← PTR
PTR ← LINK(PTR)
p ← p+1
EndRepeat
k ← DATA(PTR)
LINK(TEMP) ← LINK(PTR)
LINK(PTR) ← NULL
Release memory of PTR
RETURN k
ENDIF
Example: Assume the initial status of the list as:

1 2 3 4 5
59 2 38 3 14 4 64 5 26

START END

Anydeletion(3): Deleted Element = 14

1 2 4 5
59 2 38 4 64 5 26

START END

v) Counting number of nodes of the list

In this case, function counts number of nodes exists in the list.

Algorithm count(): This function is used to count number of elements of the list.

Step 1: IF START = NULL THEN


RETURN 0
ELSE
k←0
PTR ← START
Repeat WHILE PTR ≠ NULL
k ← k+1
PTR ← LINK(PTR)
EndRepeat
RETURN k
ENDIF

Visvodaya Technical Academy Page 84

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example: Assume the initial status of the list as:

1 2 3 4 5
59 2 38 3 14 4 64 5 26

START END

count(): Number of nodes = 5

vi) Searching an element

In this case, function checks whether a key element is present in the list of elements or
not. If the search element is found it refers to successful search; otherwise, it refers to
unsuccessful search.

Algorithm search (key): This function checks whether an element ‘key’ present
in the list of elements or not. It returns 1 if the search element key is found; otherwise, it
returns 0.

Step 1: IF START = NULL THEN


RETURN 0
ELSE
PTR ← START
Repeat WHILE PTR ≠ NULL
IF DATA(PTR) = key THEN
RETURN 1
ELSE
PTR ← LINK(PTR)
ENDIF
EndRepeat
RETURN 0
ENDIF

Example: Assume the initial status of the list as:

1 2 3 4
59 2 38 3 64 4 26

START END

search(64): Element Found

Visvodaya Technical Academy Page 85

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

search(12): Element Not Found

Disadvantages:

In singly linked list, it can be traversed only in forward direction. Backward


traversing is not possible.

2) DOUBLE LINKED LIST

In double linked list, each node is divided into three parts as FLINK/LLINK,
INFO/DATA and RLINK.

LLINK DATA RLINK

NODE
Here,
The first part FLINK/LLINK consist address of the left node of the list
The second part DATA/INFO consist information part of the element
The third part RLINK field consist address of the right node of the list.

Example:

1 2 3
10 2 1 29 3 2 32

START END

Here, START and END are two pointer variables that points to beginning and ending
nods of the list. The LLINK field of the first node and RLINK field of the last node filled
with a special pointer called NULL pointer.

Basic operations performed on the double linked list are:

i) Creating a list
ii) Traversing the list
iii) Insertion of a node into the list
iv) Deletion of a node from the list
v) Counting number of elements
vi) Searching an element etc.,

Visvodaya Technical Academy Page 86

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

i) Creating a list

Creating a list refers to the process of creating nodes of the list and arranges links in
between the nodes of the list.

In double linked list, nodes are created using self-referential structure as:

typedef struct list


{
int DATA;
struct list *LLINK,*RLINK;
}NODE;

Now, create new nodes with the format as: NODE *NEW;

Initially no elements are available in the list. At this stage, set two pointer variables
START and END to NULL pointer as:

NODE *START = NULL, *END = NULL;

Algorithm creation (): This procedure creates a double liked list with the
specified number of elements.

Step 1: Repeat WHILE TRUE


READ an element as x
IF x = -999 THEN
RETURN
ELSE
Allocate memory for a NEW node
DATA(NEW) ← x
LLINK(NEW) ← NULL
RLINK(NEW) ← NULL
IF START = NULL THEN
START ← END ← NEW
ELSE
RLINK(END) ← NEW
LLINK(NEW) ← END
END ← NEW
ENDIF
ENDIF
EndRepeat

ii) Traversing the list

Traversing the list refers to the process of visiting every node of the list exactly once
from the first node to the last node of the list.

Visvodaya Technical Academy Page 87

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Algorithm display(): This procedure is used to display the elements of the double
linked list from the first node to the last node.

Step 1: TEMP ← START


Step 2: IF START ≠ NULL THEN
Repeat WHILE TEMP ≠ NULL
WRITE DATA(TEMP)
TEMP ← RLINK(TEMP)
EndRepeat
ELSE
WRITE ‘LIST EMPTY’
ENDIF
Step 3: RETURN

Example:
1 2 3
10 2 1 29 3 2 32

START END

Display(): Double linked list elements are: 10 29 32

iii) Insertion of a node into the list

The process of inserting a node into the double linked list falls into three cases as:
 Front insertion
 Rear insertion
 Any position insertion

Case 1: Front Insertion: In this case, a new node is inserted at front position of
the double linked list.

Algorithm Finsertion(x): This procedure inserts an element x at front end of the list.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← x
LLINK(NEW) ← NULL
RLINK(NEW) ← NULL
Step 2: IF START = NULL THEN
START ← END ← NEW
ELSE
RLINK(NEW) ← START
LLINK(START) ← NEW
START ← NEW
ENDIF
Step 3: RETURN

Example: Assume initial status of the list as:

Visvodaya Technical Academy Page 88

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

1 2 3
59 2 1 24 3 2 64

START END

Finsertion (26):
0 1 2 3
26 1 0 59 2 1 24 3 2 64

NEW START END

Case 2: Rear Insertion: In this case, a new node is inserted at rear position of
the double linked list.

Algorithm Rinsertion(x): This procedure inserts an element x at rear end of the list.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← x
LLINK(NEW) ← NULL
RLINK(NEW) ← NULL
Step 2: IF END = NULL THEN
START ← END ← NEW
ELSE
RLINK(END) ← NEW
LLINK(NEW) ← END
END ← NEW
ENDIF
Step 3: RETURN

Example: Assume initial status of the list as:

1 2 3
59 2 1 24 3 2 64

START END

Rinsertion (26):

1 2 3 4
59 2 1 24 3 2 64 4 3 26

START NEW
END

Visvodaya Technical Academy Page 89

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Case 3: Any Position Insertion: In this case, a new node is inserted at a specified
position of the double linked list.

Algorithm Anyinsertion(x, pos): This procedure inserts an element x at the


specified position pos of the list.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← x
LLINK(NEW) ← NULL
RLINK(NEW) ← NULL
Step 2: k←1
PTR ← START

Step 3: Repeat WHILE k < pos


TEMP ← PTR
PTR ← RLINK(PTR)
k ← k+1
EndRepeat
Step 4: RLINK(TEMP) ← NEW
LLINK(NEW) ← TEMP
RLINK(NEW) ← PTR
LLINK(PTR) ← NEW
Step 5: RETURN

Example: Assume initial status of the list as:

1 2 3
59 2 1 24 3 2 64

START END

Anyinsertion (26,3):

1 2 5 3
59 2 1 24 5 2 26 3 5 64

START NEW END

iv) Deletion of a node from the list

The process of deleting an element from the double linked list falls into three
categories as:
 Front deletion

Visvodaya Technical Academy Page 90

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

 Rear deletion
 Any position deletion

Case 1: Front Deletion: In this case, front node information is deleted from the
double linked list.

Algorithm Fdeletion( ): This function deletes the front element of the list.

Step 1: IF START = NULL THEN


RETURN -1
ELSE
k← DATA(START)
IF START = END THEN
START ← END ← NULL
ELSE
TEMP ←START
START ← RLINK(START)
LLINK(START) ← NULL
RLINK(TEMP) ← NULL
Release memory of TEMP
ENDIF
RETURN k
ENDIF

Example: Assume initial status of the list as:

1 2 3 4
59 2 1 24 3 2 26 4 3 64

START END

Fdeletion( ): Front Deleted Element = 59

2 3 4
24 3 2 26 4 3 64

START END

Case 2: Rear Deletion: In this case, rear node information is deleted from the
double linked list.

Algorithm Rdeletion( ): This function deletes the rear element of the list.

Step 1: IF END = NULL THEN


RETURN -1

Visvodaya Technical Academy Page 91

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

ELSE
k← DATA(END)
IF START = END THEN
START ← END ← NULL
ELSE
PTR ← START
TEMP ← END
Repeat WHILE RLINK(PTR) ≠ END
PTR ← RLINK(PTR)
EndRepeat
END ← PTR
LLINK(END) ← NULL
RLINK(PTR) ← NULL
END ← PTR
Release memory of TEMP
ENDIF
RETURN k
ENDIF

Example: Assume initial status of the list as:

1 2 3 4
59 2 1 24 3 2 26 4 3 64

START END

Rdeletion( ): Rear Deleted Element = 64

1 2 3
59 2 1 24 3 2 26

START END

Case 3: Any Position Deletion: In this case, a specified position element is


deleted from the double linked list.

Algorithm Anydeletion(pos): This function deletes a specified position ‘pos’ element


of the list.

Step 1: IF START = NULL Then


RETURN -1
ELSE
PTR ← START
p←1

Visvodaya Technical Academy Page 92

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Repeat WHILE p < pos


TEMP ← PTR
PTR ← RLINK(PTR)
p ← p+1
EndRepeat
k ← DATA(PTR)
RLINK(TEMP) ← RLINK(PTR)
LLINK(RLINK(PTR)) ← LLINK(PTR)
LLINK(PTR) ← NULL
RLINK(PTR) ← NULL
Release memory of PTR
RETURN k
ENDIF

Example: Assume initial status of the list as:

1 2 3 4
59 2 1 24 3 2 26 4 3 64

START END

Anydeletion( 3): Deleted Element = 26

1 2 4
59 2 1 24 4 2 64

START END

v) Counting number of nodes of the list

In this case, function counts number of nodes exists in the list.

Algorithm count(): This function is used to count number of elements of the list.

Step 1: IF START = NULL THEN


RETURN 0
ELSE
k←0
PTR ← START
Repeat WHILE PTR ≠ NULL
k ← k+1
PTR ← RLINK(PTR)
EndRepeat
RETURN k
ENDIF

Visvodaya Technical Academy Page 93

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example: Assume initial status of the list as:

1 2 3 4
59 2 1 24 3 2 26 4 3 64

START END

count(): Number of nodes = 4

vi) Searching an element

In this case, function checks whether a key element is present in the list of elements or
not. If the search element is found it refers to successful search; otherwise, it refers to
unsuccessful search.

Algorithm search (key): This function checks whether an element ‘key’ present
in the list of elements or not. It returns 1 if the search element key is found; otherwise, it
returns 0.

Step 1: IF START = NULL THEN


RETURN 0
ELSE
PTR ← START
Repeat WHILE PTR ≠ NULL
IF DATA(PTR) = key THEN
RETURN 1
ELSE
PTR ← RLINK(PTR)
ENDIF
EndRepeat
RETURN 0
ENDIF

Example: Assume initial status of the list as:

1 2 3 4
59 2 1 24 3 2 26 4 3 64

START END

search(64): Element Found

Visvodaya Technical Academy Page 94

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

search(12): Element Not Found

Disadvantages:

In doubly linked list, forward and backward directions traversing are possible. But
traversing from a specific location is not possible.

3) CIRCULAR LINKED LIST

In circular linked list, elements are organized in circular fashion. Circular linked list
can be classified into two types as: Circular single linked list and Circular double linked list.

a) Circular Single Linked List:

In circular single linked list, the LINK part of the last node contains address of the
starting node. The diagrammatic representation of a circular single linked list is as follows:

1 2 3
10 2 20 3 30 1

START END

Operations:

Algorithm creation(): This procedure creates a circular single linked with the
specified number of elements.

Step 1: Repeat WHILE TRUE


READ an element as x
IF x = -999 THEN
RETURN
ELSE
Allocate memory for a NEW node
DATA(NEW) ← x
LINK(NEW) ← NULL
IF START = NULL THEN
START ← END ← NEW
ELSE
LINK(END) ← NEW
END ← NEW

Visvodaya Technical Academy Page 95

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

ENDIF
LINK(NEW) ← START
ENDIF
EndRepeat

Algorithm display(): This procedure is used to display the elements of the circular
single linked list from the first node to the last node.

Step 1: TEMP ← START


Step 2: IF START ≠ NULL THEN
Repeat WHILE LINK(TEMP) ≠ START
WRITE DATA(TEMP)
TEMP ← LINK(TEMP)
EndRepeat
WRITE DATA(TEMP)

ELSE
WRITE ‘LIST EMPTY’
ENDIF
Step 3: RETURN

Example: Assume initial status of the list as:

1 2 3
10 2 20 3 30 1

START END

Display(): Circular single linked list elements are: 10 20 30

b) Circular Double Linked List:

In circular double linked list, the RLINK part of the last node contains address of the
starting node and LLINK part of the first node contains address of the last node respectively.
The diagrammatic representation of a circular double linked list is as follows:

1 2 3
3 10 2 1 20 3 2 30 1

START END

Operations:

Visvodaya Technical Academy Page 96

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Algorithm creation(): This procedure creates a circular double linked with the
specified number of elements.

Step 1: Repeat WHILE TRUE


READ an element as x
IF x = -999 THEN
RETURN
ELSE
Allocate memory for a NEW node
DATA(NEW) ← x
LLINK(NEW) ← NULL
RLINK(NEW) ← NULL
IF START = NULL THEN
START ← END ← NEW
ELSE
RLINK(END) ← NEW
LLINK(NEW) ← END
END ← NEW
ENDIF
RLINK(END) ← START
LLINK(START) ← END

ENDIF
EndRepeat

Algorithm display(): This procedure is used to display the elements of the circular
double linked list from the first node to the last node.

Step 1: TEMP ← START


Step 2: IF START ≠ NULL THEN
Repeat WHILE RLINK(TEMP) ≠ START
WRITE DATA(TEMP)
TEMP ← RLINK(TEMP)
EndRepeat
WRITE DATA(TEMP)
ELSE
WRITE ‘LIST EMPTY’
ENDIF
Step 3: RETURN

4) HEADER LINKED LIST

A header linked list is a linked list which always contains a special node called as the
“header node” at beginning of the list that holds address of the starting node.

Commonly used header linked lists are:

 Grounded header linked list


 Circular header linked list

Visvodaya Technical Academy Page 97

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

A grounded header list is a header linked list where the link part of the last node
consists of NULL pointer.

Example:

Header node

1
1 2 3
10 2 20 3 30

START END

A circular header list is a header linked list where the link part of the last node
contains address of the header node i.e., it points back to the header node.

Example:

Header node
1

1 2 3
10 2 20 3 30

START END

APPLICATION OF LINKED LISTS

Linked lists are used in different application areas such as sparse matrix
manipulations, polynomial representations, stack implementations, queue implementations
etc.

i) LINKED STACK: Stack Implementation using Linked list

Stack data structure can also be implemented using linked list (Dynamic Storage
Management). The diagrammatic representation of a linked stack can be shown as:

1 2 3 4
10 2 20 3 30 4 40

START TOP

Visvodaya Technical Academy Page 98

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Algorithm push (ITEM): This procedure inserts a new element ITEM at TOP position of
the stack.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← ITEM
LINK(NEW) ← NULL
Step 2: IF TOP = NULL THEN
START ← TOP ← NEW
ELSE
LINK(TOP) ← NEW
TOP ← NEW
ENDIF
Step 3: RETURN

Example: Assume initial status of the linked stack as:

1 2 3
59 2 38 3 64

START TOP

push (26):

1 2 3 4
59 2 38 3 64 4 26

START TOP
NEW

Algorithm pop (): This function deletes the topmost element from the stack.

Step 1: IF TOP = NULL THEN


WRITE ‘STACK UNDERFLOW’
RETURN -1
ELSE
K ← DATA(TOP)
IF START = TOP THEN
START ← TOP ← NULL
RETURN K
ELSE
PTR ← START
TEMP ← TOP
Repeat WHILE LINK(PTR) ≠ TOP
PTR ← LINK(PTR)
EndRepeat
TOP ← PTR
LINK(TOP) ← NULL

Visvodaya Technical Academy Page 99

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Release memory of temp


Return K
ENDIF
ENDIF

Example: Assume initial status of the linked stack as:

1 2 3 4
59 2 38 3 64 4 26

START TOP

pop(): Deleted Element = 26

1 2 3
59 2 38 3 64

START TOP

Algorithm display(): This procedure is used to print elements of the stack.

Step 1: TEMP ← START


Step 2: IF START ≠ NULL THEN
REPEAT WHILE TEMP ≠ NULL
WRITE DATA (TEMP)
TEMP ← LINK(TEMP)
ENDREPEAT
ELSE
WRITE ‘STACK EMPTY’
ENDIF
Step 3: RETURN

Example: Assume initial status of the linked stack as:

1 2 3 4
59 2 38 3 64 4 26

START TOP

display(): Linked Stack Elements Are = 59 38 64 26

Visvodaya Technical Academy Page 100

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

ii) LINKED QUEUE: Queue Implementation using Linked list

Queue data structure can also be implemented using linked list (Dynamic Storage
Management). The diagrammatic representation of a linked queue can be shown as:

1 2 3 4
10 2 20 3 30 4 40

FRONT REAR

Algorithm insertion (ITEM): This procedure inserts a new element ITEM at REAR
position of the queue.

Step 1: Allocate memory for a NEW node


DATA(NEW) ← x
LINK(NEW) ← NULL
Step 2: IF FRONT = NULL THEN
FRONT ← REAR ← NEW
ELSE
LINK(REAR) ← NEW
REAR ← NEW
ENDIF
Step 3: RETURN

Example: Assume initial status of the linked queue as:

1 2 3
59 2 38 3 64

FRONT REAR

insertion (26):

1 2 3 4
59 2 38 3 64 4 26

FRONT REAR
NEW

Algorithm deletion(): This function deletes the front position element of the linked
queue.

Step 1: IF FRONT = NULL THEN


RETURN -1

Visvodaya Technical Academy Page 101

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

ELSE
k← DATA(FRONT)
IF FRONT = REAR THEN
FRONT ← REAR ← NULL
ELSE
TEMP ← FRONT
FRONT ← LINK(FRONT)
LINK(TEMP) ← NULL
Release memory of TEMP
ENDIF
RETURN k
ENDIF

Example: Assume initial status of the linked queue as:

1 2 3 4
59 2 38 3 64 4 26

FRONT REAR

deletion(): Front Deleted Element = 59

2 3 4
38 3 64 4 26

FRONT REAR

Algorithm display (): This procedure is used to display the elements of the linked
queue.
Step 1: TEMP ← FRONT
Step 2: IF FRONT ≠ NULL THEN
REPEAT WHILE TEMP ≠ NULL
WRITE DATA (TEMP)
TEMP ← LINK(TEMP)
ENDREPEAT
ELSE
WRITE ‘QUEUE EMPTY’
ENDIF
Step 3: RETURN

Example: Assume initial status of the linked queue as:

1 2 3 4
59 2 38 3 64 4 26

FRONT REAR

Visvodaya Technical Academy Page 102

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

display(): Linked Queue Elements Are = 59 38 64 26

Advantages of Linked list:

 Linked list used as dynamic implementation of the elements to save memory and can
be applied on huge amount of elements.
 Memory utilization can be effective. Since memory for the variables is allocated at
run time based on the user requirements.
 It provides flexibility to rearrange elements by changing links.
 Insertion and deletion operations are easier compared to array implementation.
 Linear data structures such as stack and queues can also be implemented with the help
of linked list.
Disadvantages of Linked list:

 Pointer requires extra memory space for storing elements.


 In arrays, we can access nth element easily using K[n]. But in case of linked list such
type of direct accessing is not possible. Here, we have traverse the elements in
sequential order to the particular node.
 In arrays accessing time of individual element is O(1), whereas in case of linked list is
O(n). Since, it doesn’t support random accessing.
 Reverse traversing is also difficult process.
 Since linked list is a dynamic implementation, if memory space is not available to
allocate then it won’t create any memory.

END

Visvodaya Technical Academy Page 103

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Unit-III

Trees
Introduction, Binary Trees, Binary Tree Traversals, Additional Binary Tree Operations,
Binary Search Trees, Counting Binary Trees, Optimal Binary Search Trees, AVL Trees, B-
Trees: B-Trees, B+ Trees.

TREE

A tree T is a finite set of one or more nodes such that:

 There is a specially designated node called the root,


 The remaining nodes are partitioned into n > 0 disjoint sets T 1 , T2 , - - - Tn are called
subtrees of the root.

Example:
A

B C D

E F G

H I

BASIC TERMINOLOGIES

Visvodaya Technical Academy Page 104

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Node: In tree structure each element is represented as a node. The concept of node is
same as used in linked list. Node of a tree stores the actual data and links to the other node.
Edge: The connecting link between any two nodes is called as an edge. If a tree
consists of ‘N’ number of nodes then the number of edges is N-1.
Path: The sequence of nodes and edges from one node to another node is called as a
path between the two nodes.
Parent Node: Parent of a node is the immediate predecessor of a node.

Example: A is the parent of B, C and D


F is the parent of H and I etc.

Child Node: In a tree data structure, if the immediate predecessor of a node is the parent
node then all immediate successor of a node are known as child nodes.

Example: B, C and D are the child nodes of A


H and I are the child nodes of F

Root Node: It is a specially designated node which has no parent node.

Example: A is the root node.

Leaf Node: The node which does not have any child is called a leaf node. Leaf nodes are
also known as terminal nodes.

Example: E, H, I, G and D are leaf nodes.

Non-Leaf Node: The node which has children nodes is called a non-leaf node. Non-leaf
nodes are also known as non-terminal nodes.

Example: A, B, C and F are non-leaf nodes.

Level: Level refers to the rank of the hierarchy. In general, root node is at level 1, its
children are at level 2, their children are at level 3 and so on. Hence, if a node is at level ‘K’
then its children are at level ‘K+1’.

Example: Level 1 : A
Level 2 : B, C, D
Level 3 : E, F, G
Level 4 : H, I

Degree of a Node: The number of subtrees of a node is called its degree. Degree of a leaf
node is 0.

Example: Degree (A) : 3


Degree (C) : 1
Degree (I) : 0

Degree of a Tree: The degree of a tree is the maximum of the degree of nodes in the tree.

Visvodaya Technical Academy Page 105

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example: Degree (Tree) : 3

Height of a Tree: Maximum number of nodes that is possible in a path starting from root
node to a leaf node is called the height of a tree. Height of a tree is also known as depth of a
tree.
Example: Height of the above tree : 4

Siblings: The nodes which have the same parent are called siblings.

Example: E and F are siblings.


B, C and D are siblings etc.

REPRESENTATION OF TREES

Trees can be represented in different ways such as:

A) List representation
B) Left child – Right sibling representation
C) Degree – Two representation

A) List Representation

In list representation of a tree, the information in the root node comes first, followed
by a list of subtrees of that node.

Example:
A

B C D

E F G

H I

List representation: (A(B(E,F(H,I)),C(G),D)

B) Left Child – Right Sibling Representation

In left child – right sibling representation, the node structure format can be shown as:

Visvodaya Technical Academy Page 106

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

DATA
Left child Right sibling

Here, every node has at most one leftmost child and at most one closest right sibling.
The left child field of each node points to its leftmost child, and the right sibling field points
to its closest right sibling. In this format, order of children in a tree is not important, any of
the children of a node could be the leftmost child, and any of its siblings could be the closest
right sibling.

Example:
A

B C D

E F G

H I

Left child – right sibling representation:

B C D

E F G

H I

C) Degree – Two Representations


Degree two representation of a tree is obtained by rotating the right-sibling pointers in
a left child – right sibling representation clockwise by 45 degrees. In this representation,
every node has maximum degree as 2. So that it can refer as left and right children.

Visvodaya Technical Academy Page 107

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example:
A

B C D

E F G

H I

Degree – Two representation:

E C

F G D

BINARY TREES

A binary tree T is a finite set of nodes, such that:

 T is empty (called empty binary tree), or


 T contains a specially designated node called the root of T, and remaining nodes of T
form two disjoint binary trees T1 and T2 which are called left subtree and right subtree
respectively.

Example:
A

B D

Visvodaya Technical Academy Page 108

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

E F

H I

Differences between tree and binary tree are:


 A tree can never be empty but binary tree may be empty.
 In binary tree, a node may have at most two children, whereas in a tree
a node may have any number of children nodes.
Note: Two special situations of binary tree are possible as:
a) Full binary tree
b) Complete binary tree

a) Full binary tree:


A binary tree is a full binary tree if it contains maximum possible number of nodes in
all levels.
Example: Full binary tree of height 3.

B C

D E F G

b) Complete binary tree:


A binary tree is said to be a complete binary tree if all its levels except the last level
have maximum number of possible nodes and all the nodes are appear as far left as possible.
Example: Complete binary tree of height 3.

Visvodaya Technical Academy Page 109

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

B C

D E F

Properties of a Binary Tree

i. In any binary tree, maximum number of nodes on a level K is 2K-1, where K≥1.

ii. Maximum number of nodes possible in a binary tree of height h is 2h – 1.

iii. Minimum number of nodes possible in a binary tree of height h is h.

These trees are called skew binary trees.

iv. For any non-empty binary tree, if n is the number of nodes and e is the number of
edges, then n=e+1.

v. A full binary tree of depth k is a binary tree of depth k having 2k – 1 nodes, k≥0.

vi. Height of a complete binary tree with n number of nodes is ┌log (n+1) ┐.
2

REPRESENTATION OF BINARY TREE

A binary tree can be represented in memory in two ways. Those are:

a) Sequential representation
b) Linked representation

a) Sequential Representation

Visvodaya Technical Academy Page 110

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Sequential representation of a binary tree is static and uses array concept. In this
representation, the nodes are stored level by level, starting from the first level. Root node is
stored in the first memory allocation.

Let X is an array used to store binary tree elements. Assume the root node is stored in
index 1 of the array. Then the remaining nodes follow the properties as:

- The root node is stored in X[1] location.


- If a node N occupies X[i]th location, then its
Left child is stored in X[2*i]th position and
Right child is stored in X[2*i+1]th position.

Example: Consider the binary tree as:

1
2

7 3
2 4

9 2
9 1

For this, sequential representation is:

0 1 2 3 4 5 6 7
12 72 34 99 21

Advantages:

 Any node can be accessed from any other node by calculating its index value.
 Data stored easily without help of pointers.
 BASIC, FORTRAN programming languages doesn’t support dynamic memory
allocation concept. In such cases, array representation is an efficient way to store tree
structures.

Disadvantages

 It allows only static representation. It is not possible to enhance the tree structure if
the array size is limited.
 Other than full binary trees, majority of the array entries may be empty. Hence, the
structure leads to space complexities problems.

b) Linked Representation

Visvodaya Technical Academy Page 111

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Linked representation of a binary tree is dynamic implementation and elements are


stored in terms of nodes. Here, each node is divided into three parts as: LCHILD, DATA and
RCHILD.

LCHILD DATA RCHILD

NODE
Where,

 DATA field contains information part of the element.


 LCHILD is a link field that stores the address of the left child.
 RCHILD is a link field that stores the address of the right child respectively.

Example:

For this, create node structure format as:

typedef struct tree


{
int DATA;
struct tree *LCHILD,*RCHILD;
}NODE;

BINARY TREE TRAVERSALS

Traversing operation refers to the process of visiting elements of the tree exactly once.
Any tree can be traversed in three ways as:

a) Inorder Traversal
b) Preorder Traversal
c) Postorder Traversal

a) Inorder Traversal

The inorder traversal of a binary tree can be stated as:

 Traverse the left subtree of the root node R in Inorder.

Visvodaya Technical Academy Page 112

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

 Visit the root node R.


 Traverse the right subtree of the root node R in Inorder.

Algorithm Inorder (ROOT): This procedure is used to visit the binary tree in
inorder recursively.

Step 1: PTR ← ROOT


Step 2: IF PTR ≠ NULL THEN
Inorder (LCHILD (PTR))
Visit (DATA (PTR))
Inorder (RCHILD (PTR))
ENDIF
Step 3: STOP

Example:
+
Inorder Traversal Output:
* C
A * B + C

A B

b) Preorder Traversal

The preorder traversal of a binary tree can be stated as:

 Visit the root node R.


 Traverse the left subtree of the root node R in Preorder.
 Traverse the right subtree of the root node R in Preorder.

Algorithm Preorder (ROOT): This procedure is used to visit the binary tree in
preorder recursively.

Step 1: PTR ← ROOT


Step 2: IF PTR ≠ NULL THEN
Visit (DATA (PTR))
Preorder (LCHILD (PTR))
Preorder (RCHILD (PTR))
ENDIF
Step 3: STOP

Example:
+
Preorder Traversal Output:
* C
Visvodaya Technical Academy Page 113

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

+ * A B C

A B

c) Postorder Traversal

The postorder traversal of a binary tree can be stated as:

 Traverse the left subtree of the root node R in Postorder.


 Traverse the right subtree of the root node R in Postorder.
 Visit the root node R.
Algorithm Postorder (ROOT): This procedure is used to visit the binary tree in
postorder recursively.

Step 1: PTR ← ROOT


Step 2: IF PTR ≠ NULL THEN
Postorder (LCHILD (PTR))
Postorder (RCHILD (PTR))
Visit (DATA (PTR))
ENDIF
Step 3: STOP

Example:
+
Postorder Traversal Output:
* C
A B * C +

A B

ADDITIONAL BINARY TREE OPERATIONS

1. Level-Order Traversal

Inorder, Preorder and Postorder traversals operations uses stack data structure,
whereas level-order traversal uses queue data structure.

Visvodaya Technical Academy Page 114

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

In this process, first visit the root node, then root’s left child followed by the root’s
right child. Continue the same process, visiting the nodes at each new level from the leftmost
node to the rightmost node.

The functional implementation is as follows:

void levelorder( BTREE *ptr)


{
int front = rear = 0;
if(!ptr)
return;
addqueue(ptr);
for( ; ; )
{
ptr = deletequeue();
if(ptr)
{
printf(“%d”,ptr →DATA);
if(ptr → LCHILD)
addqueue(ptr → LCHILD);
if(ptr → RCHILD)
addqueue(ptr → RCHILD);
}
else
break;
}
}

Example:
+

Level-order Traversal Output:


* C
+ * C A B

A B

2. Copying Binary Trees

Copy operation is used to copy the existing binary tree into another binary tree.
Functional implementation is as follows:

BTree copy( BTree Original)

Visvodaya Technical Academy Page 115

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

{
BTree *Temp;
if(Original)
{
Temp = (BTree*)malloc(sizeof(BTree));
Temp → LCHILD = copy(Original → LCHILD);
Temp → RCHILD = copy(Original → RCHILD);
Temp → DATA = Original → DATA;
return Temp;
}
return NULL;
}

3. Merging Operation

Suppose T1 and T2 are two binary trees. Merging operation refers to the process of
combining the entire tree T2 (or T1) as a sub tree of T1 (or T2). For this observe that in either
(or both) tree there must be at least one null sub tree.

Example:

V V
1 1

V V V
V + =
5 2 5
2

V V V V V V
V V 6 7 3 4 6 7
3 4
Let T1 consists of n1 nodes and T2 consists of n2 nodes, then the resultant tree T
consists n1+n2 nodes.
i.e., T1 (n1) + T2 (n2) = T (n1 + n2)

Algorithm Merge (ROOT1, ROOT2, ROOT): This procedure is used to merge


two subtrees T1 and T2 with root nodes ROOT1, ROOT2 and forms as a single tree T
with a root node ROOT.

Step 1: IF ROOT1 = NULL THEN


ROOT ← ROOT2
EXIT
ELSEIF ROOT2 = NULL THEN
ROOT ← ROOT1
EXIT
ELSEIF LC(ROOT1) = NULL THEN
LC(ROOT1) ← ROOT2 /* Merge T2 as left child of T1 */

Visvodaya Technical Academy Page 116

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

ROOT ← ROOT1
ELSEIF RC(ROOT1) = NULL THEN
RC(ROOT1) ← ROOT2 /* Merge T2 as right child of T1 */
ROOT ← ROOT1
ELSEIF LC(ROOT2) = NULL THEN
LC(ROOT2) ← ROOT1 /* Merge T1 as left child of T2 */
ROOT ← ROOT2
ELSEIF RC(ROOT2) = NULL THEN
RC(ROOT2) ← ROOT1 /* Merge T1 as right child of T2 */
ROOT ← ROOT2
ELSE
WRITE ‘MERGE OPERATION NOT POSSIBLE’
EXIT
ENDIF
Step 2: STOP
4. Formation of a binary tree from its traversal techniques

A binary tree can be constructed with two traversal values in which one should be
inorder traversal and other should be either preorder or postorder traversal. Basic principle
for the formation can be stated as:

 If the preorder traversal is given, then the first node is the ROOT node. If the
postorder traversal is given, then the last node is the ROOT node.
 Once the ROOT node is identified, all the nodes in the left subtree and right subtree of
the ROOT node can be gathered.
 Same procedure is applied repeatedly on the left and right subtrees.

Example: Construct a binary tree with the following tree traversals.

Inorder Traversal : D B H E A I F J C G
Preorder Traversal : A B D E H C F I J G

Step 1:
A

In: D B H E In: I F J C G
Pre: B D E H Pre: C F I J G

A
Visvodaya Technical Academy Page 117

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Step 2:

C
B

D
In : H E In : I F J G
Pre : E H
Pre: F I J

Step 3:
A

C
B

D E
F
G

H
I J

TYPES OF BINARY TREES

Different types of binary trees are:

1) Expression tree
2) Binary search tree
3) Heap tree
4) Threaded binary tree
5) Huffman tree
6) Height balanced tree
7) Decision tree etc.

1) EXPRESSION TREE

Visvodaya Technical Academy Page 118

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

An expression tree is a binary tree which stores an arithmetic expression. The leaves
of an expression tree are operands, such as constants/variable names and all internal nodes
are the operators.

Example: A+B*C

+ C

A B

2) BINARY SEARCH TREE

A binary search tree is a binary tree. It may be empty. If it is not empty then it
satisfies the following properties as:

 Each node has exactly one value and the values in the tree are distinct.
 The values in the left subtree are smaller than the value in the root node.
 The values in the right subtree are larger than the value in the root node.
 The left and right subtrees are also binary search trees.

Example:
7
5

2 9
7 2

8 9
3 9

Operations on Binary search tree

Basic operations on a binary search tree are searching, insertion, deletion, merging,
traversing etc.

i) Search Operation:

Suppose T is a binary search tree and is represented using linked structure. Assume
search element is given in the variable Key. Then search procedure works as:

 Search procedure starts from the root node R.

Visvodaya Technical Academy Page 119

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

 If Key value is less than the root node R, then proceed to its left child; If Key value is
greater than the root node R, then proceed to its right child.
 The above process will be continued till the Key is found or NULL pointer occurred.
If search procedure reached to NULL pointer, it refers to Key value not found.

Algorithm Search (Key): This procedure is used to search whether the


element Key is exist in the binary search tree or not.

Step 1: PTR ← ROOT


Flag ← FALSE
Step 2: REPEAT WHILE PTR ≠ NULL AND Flag = FALSE
IF Key < DATA(PTR) THEN
PTR ← LCHILD(PTR)
ELSEIF Key > DATA(PTR) THEN
PTR ← RCHILD(PTR)
ELSE
Flag ← TRUE
ENDIF
ENDREPEAT
Step 3: IF Flag = TRUE THEN
WRITE ‘KEY VALUE FOUND’
ELSE
WRITE ‘KEY VALUE NOT FOUND’
ENDIF
Step 4: STOP

Example:
7
5 Search (92): Key Found

2 9
7 2 Search (77): Key Not Found

8 9
3 9

ii) Inserting an element into a binary search tree:

Suppose T is a binary search tree. Inserting element is given in the variable Key.
Then insertion procedure works as:

 To insert a node with the given value, the tree T is to be searched from the ROOT
node.
 If the same Key value is found at any stage, then print a message as ‘Key already
exists – Insertion not possible’; otherwise, a new node is inserted at the dead node
where the search procedure terminates.

Visvodaya Technical Academy Page 120

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example:

7 7
5 5
Insertion (39)
2 9 2 9
7 2 7 2

3
8 9 8 9
9
3 9 3 9

Algorithm Insertion (Key): This procedure is used to insert a new node with the
data as Key into the binary search tree.

Step 1: PTR ← ROOT


Step 2: Repeat WHILE PTR ≠ NULL
IF Key < DATA (PTR) THEN
P1 ← PTR
PTR ← LCHILD(PTR)
ELSEIF Key > DATA (PTR) THEN
P1 ← PTR
PTR ← RCHILD(PTR)
ELSE
WRITE ‘KEY ALREADY EXISTS’
EXIT
ENDIF
EndRepeat
Step 3: IF PTR = NULL THEN
Allocate memory for a NEW node
DATA(NEW) ← Item
LCHILD(NEW) ← NULL
RCHILD(NEW) ← NULLL
IF DATA (P1) < Key THEN
RCHILD(P1) ← NEW
ELSE
LCHILD(P1) ← NEW
ENDIF
ENDIF
Step 4: STOP

Example: Create binary search trees with the following keys

Visvodaya Technical Academy Page 121

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

1) 15, 25, 20, 10, 2, 30,13


2) 35, 45, 20, 16, 29, 24, 33, 58

iii) Deleing an element from a binary search tree:

Suppose T is a binary search tree and an element of information is given in the variable
Key to delete from T.

Consider N be the node which contains the information element as Key. Assume
PARENT (N) denotes the parent node of N and SUCC (N) denotes the inorder successor of
the node N.

Then the deletion of the node N depends on any one of the three following cases
based on its children nodes as:

Case 1: N has no children.


Case 2: N has only one child node.
Case 3: N has two children nodes.

Case 1: N has no children:

In this case, N is deleted from the tree T by simply setting the pointer of N in the
parent node PARENT (N) by NULL pointer.

Example:
7
7
5
5
Deletion (39) 9
9 2
2
7 2
7 2

3 8 9
8 9
9 3 9
3 9

Case 2: N has only one child node:

In this case, N is deleted from the tree T by replacing address of its children pointer in
the parent node PARENT (N).

Example:
7
7 5
5 3 9
9 9 2
Visvodaya
2 Technical Academy Page 122
7 2
3 8 9 8 9
Downloaded by Karan Kamble ([email protected])
3 9
lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Deletion (27)

Case 3: N has two children:

In this case, N is deleted from the tree T by first deleting succ(N) from T (by using
case 1 or case 2) and then replace the data content in node N by the data content of node
succ(N).

Example: Inorder Traversal : 27 39 75 83 92 94 99

7
5 7
Deletion (92)
9 5
2
7 2 2 9
7 4
3
8 9
9
3 9 8 9
3 9

9
4

Algorithm deletion(Key): This function is used to delete a specified key element from the
binary search tree.

Step 1: PTR ← ROOT


Flag ← FALSE
Step 2: Repeat WHILE PTR ≠ NULL AND Flag = FALSE
IF Key < DATA (PTR) THEN
Parent ← PTR
PTR ← LCHILD(PTR)
ELSEIF Key > DATA (PTR) THEN
Parent ← PTR
PTR ← RCHILD(PTR)
ELSE

Visvodaya Technical Academy Page 123

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Flag ← TRUE
ENDIF
EndRepeat
Step 3: IF Flag = FLASE THEN
WRITE ‘ KEY DOES NOT EXIST’
EXIT
ENDIF
Step 4: IF LCHILD (PTR) = NULL AND RCHILD (PTR) = NULL THEN
IF LCHILD(Parent) = PTR THEN
LCHILD(Parent) = NULL
ELSE
RCHILD(Parent) = NULL
ENDIF

ELSEIF LCHILD(PTR) ≠ NULL AND RCHILD(PTR) ≠ NULL THEN


PTR1 ← SUCC(PTR)
K ← DATA(PTR1)
deletion(K)
DATA(PTR) ← K
ELSE
IF LCHILD(Parent) = PTR THEN
IF LCHILD(PTR) = NULL THEN
LCHILD(Parent) ← RCHILD(PTR)
ELSE
LCHILD(Parent) ← LCHILD(PTR)
ENDIF
ELSEIF RCHILD(Parent) = PTR THEN
IF LCHILD(PTR) = NULL THEN
RCHILD(Parent) ← RCHILD(PTR)
ELSE
RCHILD(Parent) ← LCHILD(PTR)
ENDIF
ENDIF
ENDIF
Step 5: STOP

Algorithm SUCC(PTR): This procedure is used to locate inorder successor of


PTR node.

Step 1: P ← RCHILD(PTR)

Step 2: IF P ≠ NULL THEN

Visvodaya Technical Academy Page 124

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

REPEAT WHILE LCHILD(P) ≠ NULL


P ← LCHILD(P)
ENDREPEAT
ENDIF

Step 3: RETURN P

iv) Traversals on binary search tree:

A binary search tree can be traversed in three ways such as: Inorder traversal,
Preorder traversal and Postorder traversal techniques.

Example:
6
5
1 7
9 4
1 2 8
5 8 8

2 5
5 7

Inorder Traveral : 15 19 25 28 57 65 74 88

Preorder Traversal : 65 19 15 28 25 57 74 88

Postorder Traversal : 15 25 57 28 19 88 74 65

Note:

 Inorder traversal on a binary search tree will give the sorted order of data in
ascending order.
 To sort the given set of data, a binary search tree can be built and then inorder
traversal can be applied. This method of sorting is known as binary sort and such
binary search tree can be treated as binary sorted tree.

Visvodaya Technical Academy Page 125

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

3) OPTIMAL BINARY SEARCH TREE

Assume there is a fixed set of keys. When we wish to create a binary search tree, it
leads to different performance characteristics.

Example: Consider the keys as: 10 5 25 20 15

For this, different possible binary search trees are:

1 1
0 0
etc.,
5 2 5 2
5 0

2 1 2
0 5 5

1
5
 When we apply search operations on those keys:
o First tree takes 1, 2, 2, 3, 4 comparisons to find the keys. Thus, the average
number of comparisons = (1+2+2+3+4) / 5 = 12/5.
o Second tree takes 1, 2, 2, 3, 3 comparisons to find the keys. Thus, the average
number of comparisons = (1+2+2+3+3) / 5 = 11/5.
o Hence, the second binary search tree has a better performance than the first
binary search tree.
 Suppose each of these keys 5, 10, 15, 20 and 25 is searched with a probability of 0.3,
0.3, 0.05, 0.05 and 0.3 respectively. Then the average number of comparisons for the

Visvodaya Technical Academy Page 126

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

first tree is 1.85 and second tree is 2.05. In this case, the first binary search tree has a
better performance than the second binary search tree.
 Based on the different factors, it leads different binary search trees and gives different
performances.

To generate unique representation, the number of comparisons at different levels to


search a particular node is considered as cost. Then a binary search tree construction with a
given set of keys with minimum cost is known as optimal binary search tree.

Cost construction for a binary search tree is obtained by considering cost of successful
search and unsuccessful search. In evaluating binary search trees, it is useful to add a special
‘square’ node at every null link. These nodes are known as external nodes. Other nodes are
treated as internal nodes. A binary tree with external nodes is known as an extended binary
tree. If it satisfies the properties of binary search tree, then it is termed as extended binary
search tree.
Example: Extended binary search tree:

1
0

5 2
0

1 2
5 5

 Here, successful search terminates at internal nodes and unsuccessful search


terminates at external nodes.
 If the tree consists of n internal nodes, then it consists of n+1 external nodes.

Construction of OBST:

Let a1 , a2 , - - - , a n with a1 < a2 < - - - - < an be the element keys. To design optimal
binary search tree, apply dynamic programming approach. Suppose that the probability of
searching for ai is pi and probability of unsuccessful search is qi. Then, calculate w (Weight
value), c (Cost value) and r (Root node) at each stage. For this, computation equations are
given below.

Initially wi,i = qi
ci,i = 0
ri,i = 0

From the next iterations,

Visvodaya Technical Academy Page 127

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

wi,j = pj + qj + wi,j-1
ci,j = Min [ ci,k-1 + ck,j ] + wi,j
i<k≤j
ri,j = k

For a node position ri,j = k, the left child position is ri,k-1 and the right child position is rk,j.

Example: Construct an optimal binary search tree with n=3, (a 1 , a2 , a3) = (10, 15,
20), (p1 , p2 , p3) = (3, 3, 1) and (q0 , q1 , q2 , q3) = (2, 3, 1, 1).

4) HEIGHT BALANCED TREES

Trees with a worst case height of O (log n) are called height balanced trees.

Examples: AVL tree, Red-Black tree, B-tree, etc.,

Here, AVL tree and Red-Black trees are useful for internal memory applications
whereas B-tree is useful for external memory applications.

i) AVL Tree

AVL tree is a height balanced tree introduced in 1962 by Adelson-Velskii and Landis.

“An empty binary tree T is an AVL tree. If T is non-empty binary tree with T L and TR
as its left and right subtrees, then T is an AVL tree if:

 TL and TR are also AVL trees.


 | hL – hR | ≤ 1 ; Where, hL and hR are the heights of TL and TR respectively.”

In an AVL tree every node is associated with a value called balance factor and it is
denoted for the node x as:

bf (x) = Height of left subtree – Height of right subtree

From the definition of AVL tree, the allowable balance factors are 0, 1 and -1.

Visvodaya Technical Academy Page 128

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example:
0
2
0
0 -1
1 4
5 0

0 0 1 0
9 7
0 7 6

Note: If the AVL tree satisfies the properties of binary search tree, then it is referred to as an
AVL search tree.

OPERATIONS ON AVL SEARCH TREE

Insertion Operation:

Inserting an element into an AVL search tree follows the same procedure as the
insertion of an element into a binary search tree. But, the insertion may leads to a situation
where the balance factors of any of the nodes may be other than -1, 0, 1 and the tree is
unbalanced.

If the insertion makes an AVL search tree unbalanced, the height of the subtree must
be adjusted by the operations called rotations.

For this, consider N is the newly inserted node and A is the nearest ancestor which
has balance factor as 2 or -2. Then, the imbalance rotations are classified into four types as:

 LL Rotation: New node N is left subtree of left subtree of A.


 RR Rotation: New node N is right subtree of right subtree of A.
 LR Rotation: New node N is right subtree of left subtree of A.
 RL Rotation: New node N is left subtree of right subtree of A.

Here, the transformations done to LL and RR imbalances are often called single
rotations, while those done for LR and RL imbalances are called double rotations.

LL Rotation: In LL rotation, every node moves one position from the current position.

A
B
LL Rotation
B
N A
N

Visvodaya Technical Academy Page 129

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

RR Rotation: In RR rotation, every node moves one position from the current position.

A
B
RR Rotation
B
A N
N

LR Rotation: The LR Rotation is a sequence of single left rotation followed by a single right
rotation. In LR Rotation, at first, every node moves one position to the left and one position
to right from the current position.

A A
N

B
N

N B
A
B

RL Rotation: The RL Rotation is sequence of single right rotation followed by single left
rotation. In RL Rotation, at first every node moves one position to right and one position to
left from the current position.

A A
N

B N

B A
N B

Example: Create an AVL search tree with the keys: 70 25 47 98 101.

Visvodaya Technical Academy Page 130

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Deletion Operation:

Deletion of an element from an AVL search tree follows the same procedure as the
deletion of binary search tree operations. Due to deletion of the node, some or all of the
nodes of balance factors on the path might be changed and tree becomes unbalanced. To
make it balanced format, it also requires rotations.

In this case, the deleting node is not available after performing the deletion operation.
Hence based on the balanced factor of siblings of the deleted node, rotations are classified
into six types as L0 , L1 , L-1 and R0 , R1 , R-1 rotations.

R0 Rotation: Assume a node is deleted from the right subtree of a specific position C. After
deletion operation, the sibling node B has a balance factor as 0, and then R 0 rotation is used to
rebalance the tree as:

C
B

R0 Rotation
0 B C1R BL C
BL BR BR C1R

R1 Rotation: Assume a node is deleted from the right subtree of a specific position C. After
deletion operation, the sibling node B has a balance factor as 1, and then R 1 rotation is used to
rebalance the tree as:

C
B

R1 Rotation
B 1
1 CR BL C
BL BR BR C1R

R-1 Rotation: Assume a node is deleted from the right subtree of a specific position C. After
deletion operation, the sibling node B has a balance factor as -1, and then R -1 rotation is used
to rebalance the tree as:

C B

R-1 Rotation
-1 A C1R A C

B
Visvodaya Technical Academy Page 131

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

AL
AL BL BR C1R
BL BR

L0 Rotation: Assume a node is deleted from the left subtree of a specific position B. After
deletion operation, the sibling node C has a balance factor as 0, and then L 0 rotation is used to
rebalance the tree as:

B
C

L0 Rotation
B1L 0 C CR
B
CL CR B1L CL

L1 Rotation: Assume a node is deleted from the left subtree of a specific position B. After
deletion operation, the sibling node D has a balance factor as 1, and then L 1 rotation is used to
rebalance the tree as:

B
C

L1 Rotation
BL 1 D
D
B
C
DR B1L CL CR DR

CL CR
L-1 Rotation: Assume a node is deleted from the left subtree of a specific position B. After
deletion operation, the sibling node C has a balance factor as -1, and then L -1 rotation is used
to rebalance the tree as:

B
C

L-1 Rotation
B1L -1 C CR
B
CL CR B1L CL

Example: Delete 40 from the tree


7
0

4 8
0 5

Visvodaya Technical Academy Page 132

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

7 9
5 9

ii) B-TREE

m-way Search Tree:

An m-way search tree T may be an empty tree. If T is non-empty then it satisfies the
properties as:

 Every node can have a maximum of m sub trees.


 A node with k sub trees will have k-1 elements where k<m.
 All the elements of a node will be in ascending order i.e., if it consists elements as k1,
k2, - - - kn such that k1<k2<- - - <kn. Assume its children nodes as c1, c2, - - - , cn.
 The elements in c1 are less than k1 and elements in c2 are greater than k1 and so on.
 All sub trees are m-way search trees.

Example: 3-way search tree

20 40
0

10 15 25 30 45 50
0 0

28

B-TREE Definition

A B-tree of order m is an m-way search tree and it may be empty. If it is non-empty


then the following properties are satisfied in terms of extended trees.

 The root node should have a minimum of two children and a maximum of m children.
 All the internal nodes except the root node should have a minimum of ┌ m/2 ┐ non-
empty children and a maximum of m non-empty children.
 All the external nodes are at the same level.

Example: B-tree of order 3 (2-3 tree)

43 75

6 24 52 64 87

Visvodaya Technical Academy Page 133

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Note:

1. B-tree of order 3 is also referred to as 2-3 trees. Since, its internal nodes can have
only two or three children.
2. B-tree of order 4 is also referred to as 2-3-4 or 2-4 trees. Since, its internal nodes can
have either two, three or four children.

OPERATIONS ON B-TREE

Insertion Operation:

Inserting a new element into the B-tree of order m is followed by the search operation
for the proper location in a node. When the search terminates at a particular node, then
insertion falls into the either of the cases as:

Case-1: In node X contains space for insertion, then inserts the element in proper
position and child pointers are adjusted accordingly.

Example: B-tree of order 5

40 82

11 25 38 58 74 86 89 93 97

Insertion (64):

40 82

11 25 38 58 64 74 86 89 93 97

Visvodaya Technical Academy Page 134

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Case-2: If node X contains full of elements, then first insert the element into its list of
elements. Then split the node into two sub nodes at the median value. The elements that are
less than the median becomes the left node and that are greater than the median becomes the
right node. Then the median element is shifted up into the parent node of X. Sometimes the
process may propagate up to root level also.

Example: B-tree of order 5

40 82

11 25 38 58 74 86 89 93 97

Insertion (99):
40 82 93

11 25 38 58 74 86 89 97 99

Deletion Operation:

Deleting an element from a B-tree of an order m may be performed according to one


of the four cases.

Case-1: When the key exists in the leaf node and deletion may not effect of the
properties, then simply delete the key from the node and child pointers are adjusted.

Example: B-tree of order 5


40 82

11 25 38 58 74 86 89 93 97

Deletion (89):
40 82

Visvodaya Technical Academy Page 135

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

11 25 38 58 74 86 93 97

Case-2: When the key exists in the non-leaf node, replace key with the largest element
from its left sub-tree or the smallest element from its right sub-tree.

Example: B-tree of order 5


40 82

11 25 38 58 74 86 89 93 97

Deletion (40):

82

11 25 38 58 74 86 89 93 97

38 82

11 25 58 74 86 89 93 97

Case-3: If deleting an element k from a node leaves it with less than its minimum
number of elements, then elements can be borrowed from either of its sibling nodes. If the
left sub tree node is capable to spare the element then its largest element is shifted into the
parent node. If the right sub tree node is capable to space the element then its smallest
element is shifted into the parent node. From the parent node the intervening element is
shifted down to fill the vacancy created by the deleted element.

Example: B-tree of order 5


40 82

Visvodaya Technical Academy Page 136

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

11 25 38 58 74 86 89 93 97

Deletion (58):

40 82

11 25 38 74 86 89 93 97

38 82

11 25 40 74 86 89 93 97

Case-4: If deletion of an element is making the elements of the node to be less than its
minimum number and either of the sibling nodes have no chance of sparing an element, then
this node is merged with either of the sibling nodes including the intervening element from
the parent node.

Example: B-tree of order 5

38 82

11 25 40 74 86 89 93 97

Deletion (25):

82

11 38 40 74 86 89 93 97
Visvodaya Technical Academy Page 137

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

iii) B+ - TREE

B+ - Tree can be viewed as B-Tree in which it has two types of nodes – index nodes
and data nodes.

 The index nodes store keys (not elements) and pointers and the data nodes store
elements.
 The data nodes are linked together, in left to right order, to form a doubly linked list.

Definition:

A B+ - Tree of order m is a tree that either is empty or satisfies the following


properties:

1. All data nodes are the same level and are leaves. Data nodes contain elements only.
2. The index nodes define a B-tree of order m; each index node has keys but not
elements.
3. Let n, A0 , (K1 , A1) , (K2 , A2) , - - - , (Kn , An)

Where, Ai are pointers to subtrees and the Ki are the keys be the format of
some index node.
All elements in the subtree Ai have keys less than Ki+1 and greater than or
equal to Ki.

Example:

20 40

10 30 70 80

2 12 20 32 40 71 80
4 16 25 36 50 72 82
6 18 60 84

OPERATIONS ON B+ - TREE

Search Operation

Visvodaya Technical Academy Page 138

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

In B+ - Tree search procedure begin at the root node. From the definition of B + - tree,
search element is less than the specific key index value then control moves to left supporting
node; otherwise, moves to right supporting node until the data node reaches. Then compare
the search element with data key values. If element found, then it refers to successful search;
otherwise, it refers to unsuccessful search.
Insertion Operation

To implement insertion operations, first apply search operation to locate proper node
for insertion. Then it falls into different cases such as:

Case – 1: The data node is not full and has space for insertion then; simply insert the key
into the node.

Example:

20 40

10 30 70 80

2 12 20 32 40 71 80
4 16 25 36 50 72 82
6 18 60 84

Insertion (27) :

20 40

10 30 70 80

2 12 20 32 40 71 80
4 16 25 36 50 72 82
6 18 27 60 84

Visvodaya Technical Academy Page 139

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Case – 2: The data node is full, and then the overall node is split into two by moving the
largest half of the elements into a new node, which is then inserted into the doubly linked list
of data nodes. The smallest key in the new node is placed as a pointer in the parent node.

Example:

20 40

10 30 70 80

2 12 20 32 40 71 80
4 16 25 36 50 72 82
6 18 60 84

Insertion (14) :

20 40

10 16 30 70 80

2 12 16 20 32 40 71 80
4 14 18 25 36 50 72 82
6 60 84

Visvodaya Technical Academy Page 140

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Case – 3: The data node is full, and then the overall node is split into two by moving the
largest half of the elements into a new node, which is then inserted into the doubly linked list
of data nodes. The smallest key in the new node is placed as a pointer in the parent node.
But the parent index node is full and locations are not empty, then split the index node into
two are more it to upper level if necessary.

Example:

20 40

10 16 30 70 80

2 12 16 20 32 40 71 80
4 14 18 25 36 50 72 82
6 60 84

Insertion (86) :

40

20 80

10 16 30 70 84

2 12 16 20 32 40 71 80 84
4 14 18 25 36 50 72 82 86
6
Visvodaya Technical Academy 60 Page 141

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Deletion Operation

In B+ tree, keys are available in leaves. Hence, elements are deleted from the leaf
node. Assume the data node consists at least ┌ c / 2 ┐ elements, where c is the capacity of B +
tree.

Example: Consider a B+ tree of order 3 (i.e., c = 3)

20 40

10 30 70 80

2 12 20 32 40 71 80
4 16 25 36 50 72 82
6 18 60 84

To delete any element, first search the node in which the key element exists. Then,
deletion process falls into different cases as:

Case – 1: If the deleting element doesn’t effects the properties of the B + tree, simply
delete the key element from the data node.

Deletion (40):

20 40

10 30 70 80

2 12 20 32 50 71 80
4 16 25 36 60 72 82
6 18 84

Visvodaya Technical Academy Page 142

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Case – 2: If the deleting element deficient the data node, then check either its nearest
right or left sibling and determine whether the checked sibling has more than the required
minimum number of ┌ c / 2 ┐ of elements.

If left node has an excess element, borrow the largest element and if the right node has
an excess element, borrow the smallest element and update the in-between key in the parent
node.

Deletion (71):

20 40

10 30 70 82

2 12 20 32 40 72 82
4 16 25 36 50 80 84
6 18 60

Case – 3: If the deleting element deficient the data node, then check either its nearest
right or left sibling and determine whether the checked sibling has more than the required
minimum number of ┌ c / 2 ┐ of elements. If the sibling does not have minimum number of
keys then combines the two data nodes into single node and update the parent node according
to the new node values.

Example: Consider a B+ tree of order 3 (i.e., c = 3)

20 40

10 30 70 82

2 12 20 32 50 72 82
4 16 25 36 60 80 84
6 18
Visvodaya Technical Academy Page 143

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Deletion (80):

20 40

10 30 70

2 12 20 32 50 72
4 16 25 36 60 82
6 18 84

END

Visvodaya Technical Academy Page 144

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Unit-IV

Graphs and Hashing

The Graph Abstract Data Type, Elementary Graph Operations, Minimum Cost Spanning
Trees, Shortest Paths and Transitive Closure.
Hashing: Introduction to Hash Table, Static Hashing, Dynamic Hashing.
***

GRAPHS

A graph G=(V,E) consists of a finite non-empty set of vertices V also called as nodes
and a finite set of edges E also called as arcs.

Example:

e1
a b
e2 e3

e5

c d

e4

Where, V = {a,b,c,d} and E = {e1 , e2 , e3 , e4 , e5 }


e1 = (a,b) e2 = (a,c) e3 = (b,c) e4 = (c,d) e5 = (b,d)

GRAPH TERMINOLOGIES

Digraph: A graph in which every edge is directed is called a digraph. A digraph is also
known as a directed graph.

Example:

e1
a b
e2 e3

c d

Visvodaya Technical Academy Page 145

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

e4

Where, V = {a,b,c,d} and E = {e1 , e2 , e3 , e4 }


e1 = <a,b> e2 = <a,c> e3 = <c,b> e4 = <c,d>

Undirected Graph: A graph in which every edge is undirected is called an undirected


graph. Here, an edge (Vi , Vj) is equivalent to (Vj , Vi).

Example: e1
a b
e3
e2 c
Where, V = {a,b,c} and E = {e1 , e2 , e3}
e1 = (a,b) e2 = (a,c) e3 = (b,c)

Mixed Graph: A graph in which some edges are directed and some edges are
undirected is called a mixed graph.

Example: e1
a b
e2 e4
c d
e3
Where, V = {a,b,c,d} and E = {e1 , e2 , e3 , e4}
e1 = (a,b) e2 = <a,c> e3 = <c,d> e4 = (a,d)

Weighted Graph: A graph is termed as a weighted graph if all the edges in it are labeled
with some weight values.
Example: 5
7 a b
3
c d
9
Adjacent Vertices: A vertex Vi is adjacent / neighbor of another vertex Vj, if there is an
edge from Vi to Vj.

Self Loop: If there is an edge whose starting and ending vertices are same is called
as a loop or self loop.

Example:
a Self loop at vertex c
e2 e4
e1
b c
e3
In-degree of a Vertex: The number of edges coming into the vertex Vi is called the in-
degree of vertex Vi.

Out-degree of a Vertex: The number of edges going out from a vertex Vi is called the
out-degree of vertex Vi.

Visvodaya Technical Academy Page 146

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Degree of a Vertex: Sum of out-degree and in-degree of a node V is called the total
degree of the node V and is denoted by degree (V).

Example:
a In-degree (a) = 1
Out-degree (a) = 2

b d

Path: A path from vertex vi to vj is a sequence of vertices vi , vi+1 , - - - , vj such that


(vi , vi+1) , (vi+1 , vi+2) , - - - are the edges in the graph. The length of the path is the number of
edges on it.
Example:
V
2
V
V 4
1
V
3
Path from V1 to V4 is:
P1 = {V1, V2, V4} P2 = {V1, V3, V4}

Complete Graph:

A Graph G is said to be a complete graph if each vertex v i is adjacent to every other


vertex vj in G.
Example:
V
2
V
V 4
1
V
3

Note: An n-vertex, undirected graph with exactly n(n-1) / 2 edges is said to be complete
graph.
Connected Graph:

In a graph G, two vertices v i and vj are said to be connected if there is a path in G


from vi to vj.

Visvodaya Technical Academy Page 147

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

 An undirected graph is said to be a connected graph is every pair of distinct vertices


vi and vj are connected.

Example: V
2
V V
1 4
V
3

 A digraph is said to be strongly connected graph if for every pair of distinct vertices
vi , vj in G, there is a directed path from vi to vj and also from vj to vi.

Example: V
1
V V
4 2
V
3

Acyclic Graph: If there is a path containing one or more edges which starts from a
vertex vi and terminates into the same vertex then the path is known as a cycle. If a graph
does not have any cycle then it is called as acyclic graph.

Example: V
1
V V
4 2
V
3
Sub Graph: A sub graph of G is a graph G1 such that V(G1) is a subset of V(G) and
1
E(G ) is a sub set of E(G).

1 2

For this, some of the sub graphs are:

0
0 0

Visvodaya Technical Academy 1 2 Page 148

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

1 2

GRAPH REPRESENTATIONS

A graph can be represented in many ways. Some of the important representations are:
Set representation, Adjacency matrix representation, Adjacency list representations
etc.,
Set representation:

One of the straight forward methods of representing any graph is set representation.
In this method two sets are maintained: V as the set of vertices and E as the set of edges
which is a subset of V x V.
In case of weighted graph, V as the set of vertices and E as the set of edges which is a subset
of W x V x V.
V
Example:
1
V(G) = {V1,V2,V3,V4,V5,V6,V7} V
V
E(G) = { (V1,V2), (V1,V3), (V2,V4), 3
(V2,V5), (V3,V4), (V3,V6), 2
V
(V4,V7), (V5,V7), (V6,V7) }
4

V
V
5 6
V
7
Example:

5 V(G) = {A, B, C, D}
A B E(G) = { (3,A,C), (5,B,A),
(1,B,C), (7,B,D), (2,C,A),
2 3 1 6 7 (4,C,D), (6,D,B), (8,D,C)}

4
C D
8
Linked representation:

Linked representation is a space saving way of graph representation. Linked


representation is also known as adjacency list representation. In this representation, two
types of node structures assumed as:

Visvodaya Technical Academy Page 149

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Node Adjacency
Node structure of Non-weighted graph:
Information List

Node structure of Weighted graph: Weight Node Adjacency


Value Information List

In linked representation, the number of lists depends on the number of vertices in the
graph.

Example:

V1 V2 V3
V
1 V2 V1 V4 V5
V
V
3 V3 V1 V4 V6
2
V
4 V4 V2 V3 V7

V V5 V2 V7
V
6
5 V6 V7
V3
V
7 V7 V4 V5 V6

Example:

5
A B

2 3 1 6 7

4
C D
8

A 3 C

B 1 C 7 D 5 A

Visvodaya Technical Academy Page 150

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

C 2 A 4 D

D 6 B 8 C

Sequential representation:

Sequential (Matrix) representation is the most useful way for representing any graphs.
For this, different matrices are allowed as Adjacency matrix, Incidence matrix, Circuit matrix,
Cut set matrix, Path matrix etc.

Adjacency Matrix Representation

The adjacency matrix representation of a graph G with n vertices is an nXn matrix


such that each element is defined as:

Aij = 1 ; If there is an edge between vi and vj.


= 0 ; Otherwise.

Example: V1 V2 V3 V4
V 0 1 1 0
3 1 0 1 0
V A =
V 4 1 1 0 1
1 0 0 1 0
V
2

Example:
V 0 1 1 1
3 0 0 0 1
V A =
V 4 0 1 0 1
1 0 0 0 0
V
2

ABSTRACT DATA TYPE GRAPH

ADT Graph
{
Data Members: A non empty set of vertices and a set of undirected edges.

Visvodaya Technical Academy Page 151

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Functions:

Graph create(): Creation of a graph


Graph Insertion(Graph, v): Insertion of a new vertex v into the graph
Graph Deletion(Graph, v): Deletion of a vertex v from the graph
Boolean IsEmpty(Graph): Process of checking whether the graph is empty
Or not.
}

OPERATIONS ON GRAPHS

Basic operations on graphs are: Insertion, Deletion, Merging, Traversing etc.

Insertion operation:

Insertion of a vertex into the graph involves the way of insertion and establishes
connectivity with other vertices in the existing graph.

 In case of un-directed graph, if Vx is inserted and Vi be its adjacent vertex then


entries of Vx and Vi are incorporated in the adjacency list.

 In case of directed graph, if Vx is inserted and Vi be its adjacent vertex then based on
the directions necessary entries are incorporated in the adjacency list.

Example: Insertion(V5)
V
V1 V2 V3
3
V
V 4 V2 V1 V3 V5
1
V
V3 V1 V2 V4
2 V V4 V3 V5
5
V5 V2 V4

Deletion operation:

For deleting a vertex from a graph, identify all the vertices that are adjacent to the
vertex and break the edges.

Example:

Visvodaya Technical Academy Page 152

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

V
V1 V2 V3
3
V
V 4 V2 V1 V3 V4
1
V
V3 V1 V2 V4
2
V4 V2 V3
Deletion (V4):

V
V1 V2 V3
3

V V2 V1 V3
1
V
V3 V1 V2
2
Merging operation:

Consider two graphs G1 and G2. Merging operation refers to the process of
combining these two graphs as a single graph G. This is accomplished by establishing one or
more edges between the graphs G1 and G2.

Example:

V W
V 1 Merge((V4,W1), (V3,W2)
1
4 + W
V 3
2 V W
3 2

V W
1 V 1
4 W
V 3
2 V W
3 2

GRAPH TRAVERSAL TECHNIQUES

Traversing a graph means visiting all the vertices in the graph exactly once. Graph
traversal techniques are:
a) Breadth First Search (BFS) Traversal
b) Depth First Search (DFS) Traversal

Visvodaya Technical Academy Page 153

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

a) Breadth First Search (BFS) Traversal

The traversal starts from a vertex u which is said to be visited. Now all nodes V i
adjacent to u are visited. The unvisited vertices Wij adjacent to each of Vi are visited next and
so on. This process continues till all the vertices of the graph are visited.

BFS uses a queue data structure that keep a track of order of nodes whose adjacent
nodes are not to be visited.

Algorithm BFS (u): This procedure visits all the vertices of the graph starting from the
vertex u.
Step 1: Initialize a queue as Q
Visited(µ) ← 1
Enqueue(Q, u)
Step 2: Repeat WHILE NOT EMPTY(Q)
Dequeue(Q,u)
WRITE (u)
For all vertices V adjacent to u
IF Visited (V) = 0 THEN
Enqueue(Q,V)
Visited (V) ← 1
ENDIF
EndFor
EndRepeat
Step 3: RETURN

Example: Apply breadth first search traversal for the graph

5 4

2
1
9
7

3
6
1
0
8

Breadth First Search Traversal Output : 1 5 6 7 4 9 10 3 2 8


Analysis: For each vertex is placed on the queue exactly once, the while loop is iterated
at most n times. For the adjacency list representation, this loop has a total cost of O(e). For
adjacency matrix representation, the while loop takes O(n) time for each vertex visited.
Therefore, the total time is O(n2).

Visvodaya Technical Academy Page 154

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

b) Depth First Search (DFS) Traversal

The traversal starts from a vertex u which is said to be visited. Now all nodes V i
adjacent to u are collected and the first adjacent vertex V j is visited. The nodes adjacent to V j
namely Wk are collected and the first adjacent vertex is visited. The traversal progresses until
there are no more visits possible.

DFS uses a stack data structure that keep a track of order of nodes whose adjacent
nodes are not to be visited.

Algorithm DFS (u): This procedure visits all the vertices of the graph starting from the
vertex u.

Step 1: Visited(u) ← 1
WRITE (u)
Step 2: For each vertex V adjacent to u
IF Visited (V) = 0 THEN
Call DFS(V)
ENDIF
EndFor
Step 3: RETURN

Example: Apply depth first search traversal for the graph

A C E

Depth First Search Traversal Output : A B E C D

Analysis: For the adjacency list representation, each node determines the links search
operation is O(e) time. For adjacency matrix representation, the determining all vertices
adjacent to the vertex requires O(n) time. Therefore, the total time is O(n2).

CONNECTED COMPONENTS

A connected component of an undirected graph G is a maximal connected subgraph of


G.

Example:

Visvodaya Technical Academy Page 155

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

For this, connected component is:

1 1

2 3 2 3

4 4

If the graph is connected undirected graph, then we can visit all the vertices of the
graph by using either breadth first search or depth first search. The subgraph which has been
obtained after traversing the graph using either BFS or DFS represents the connected
component of the graph.

Example:
0

1 2

3 5 6
4

Connected component by DFS Connected component by BFS

0
0

1 2
1 2
4
3 5 6
3 5 6 4

7
7

Functional implementation of connected components is as follows:

void components(G, n)
{
int i;
for(i =0 ; i<n; i++)
Visited[i] = 0;
Visvodaya Technical Academy Page 156

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

for(i=0 ; i<n; i++)


{
if(Visited[i] = = 0)
dfs(i);
}
}

Analysis:

1. If the graph G is represented by its adjacency lists, then the total time needed to
generate all the connected components is O(n+e) time.
2. If the graph G is represented by its adjacency matrix, then the total time needed to
generate all the connected components is O(n2) time.

SPANNING TREES

A spanning tree T of an undirected graph G is a sub graph that is a tree which


includes all of the vertices of G, with minimum possible number of edges.

Example:

B
A

C
D

For this some of the possible spanning trees are:


B
B
B
A A A

C C C
D D D

Note:

1. If the graph contains n vertices, then spanning tree contains exactly n-1 edges to
connect the n vertices.
2. When breadth first search traversal applied on a graph, the resultant spanning tree is
known as a breadth first spanning tree.

Visvodaya Technical Academy Page 157

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

3. When depth first search traversal applied on a graph, the resultant spanning tree is
known as a depth first spanning tree.

MINIMUM COST SPANNING TREES

Let G = (V, E) be a connected undirected graph. A sub graph T=(V,E1) of G is a


spanning tree iff T is a tree. If the graph contains n vertices, then spanning tree contains
exactly n-1 edges to connect the n vertices.

A Spanning Tree for G is a sub-graph of G that it is a free tree connecting all vertices
in V. The cost of a spanning tree is the sum of costs on its edges.

A Minimum cost spanning tree of G is a spanning tree of G having a minimum cost.

The most important techniques used for finding minimum cost spanning trees are:
Prim’s algorithm, Kruskal’s algorithm and Sollin’s algorithm.

Prim’s Algorithm

Prim’s algorithm to find minimum cost spanning tree uses greedy method. In this
method, edge by edge is selected based on optimization criteria.

The procedure for the prim’s algorithm is as follows:

Step 1: Randomly choose any vertex. The vertex connecting to the edge having least
weight is usually selected.

Step 2: Find all the edges that connect the tree to new vertices. Select the least weight
edge among those edges and include it in the existing tree. If including that edge creates a
cycle, then reject that edge and look for the next least weight edge.

Step 3: Keep repeating step-02 until all the vertices are included and Minimum
Spanning Tree (MST) is obtained.

Function implementation is as follows:

T = { };
TV = {0};
while( T contains less than n-1 edges )
{
Visvodaya Technical Academy Page 158

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Let (u,v) be a least cost edge such that u € TV and v not belongs to TV;
if(there is no such edge)
break;
add v to TV;
add (u,v) to T;
}
if(T contains fewer than n-1 edges)
printf(“ No Spanning Tree”);

Example: Design minimum cost spanning tree for the following graph

Kruskal’s Algorithm

In kruskal’s algorithm the edges of the graph are considered in non decreasing order
of cost.

In this method, place all vertices as in forest. First lowest cost edge is selected. Then
the next lowest cost edge is selected and so on. The set T of edges so far selected for the
spanning tree be such that it is possible to complete T into a tree. Thus T may not be a tree at
all stages in the algorithm.

Function implementation is as follows:

T = { };
while( T contains less than n-1 edges && E is not empty)
{
Choose a least cost edge (v,w) from E
Delete (v,w) from E
if((v,w) does not create a cycle in T)
add (v,w) to T
else
discard (v,w)
}
if(T contains fewer than n-1 edges)
printf(“ No Spanning Tree”);
Example: Design minimum cost spanning trees for the following graphs

Visvodaya Technical Academy Page 159

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Sollin’s Algorithm

Sollin’s algorithm is also known as Boruvka’s algorithm. It was developed by


Boruvkas in 1926 and is the first algorithm to find the minimum cost spanning tree for any
graphs. Sollin’s algorithm is also follows the greedy method and similar to prim’s and
kruskal’s procedure.

Steps of the sollin’s algorithm are as follows:

Step 1: Select all vertices and place in the tree as a forest.


Step 2: Select all edges with lowest cost with at least one new vertex.
Step 3: Select the smallest edge and make it as tree connections.
Step 4: Repeat step-3 with the existing edges. If the edge forms a cycle then
reject; otherwise include it until all vertices are included and forms
minimum cost spanning tree.

Example: Design minimum cost spanning tree for the following graph

BICONNECTED COMPONENTS

Visvodaya Technical Academy Page 160

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Articulation Point: A vertex in an undirected connected graph is an articulation


point (or cut vertex) if removing the vertex (and edges through it) disconnects the graph into
two or more sub graphs.

Examples:

Biconnected Graph: A biconnected graph is a connected graph that has no


articulation points.

Example:
1
5

4
3

Biconnected Components: A biconnected component of a connected undirected


graph is a maximal biconnected subgraph.

Key observations regarding biconnected components are:

 The biconnected component is a maximal biconnected subgraph.


 Two different biconnected components should not have any common edges.
 Two different biconnected components can have common vertex.
 The common vertex which is attaching two or more biconnected components must
have an articulation point.

Example:
1 0 3

2 4

For this, biconnected components are:

Visvodaya Technical Academy Page 161

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

1 0 0 3 3

2 4

Note: The computing time of biconnected component is O(n+e) time.

APPLICATION OF GRAPHS

Graph are used to represent networks, road maps, facebook etc. In addition to this,
graphs are used in different application areas which include:

 Shortest path problem


 Topological sorting
 Spanning trees
 Hamilton path etc.

SHORTEST PATH PROBLEM

In a graph, the vertices represent cities and the edges represent sections of the
highway. Each edge has a weight representing the distance between the two cities connected
by the edge. Aim of the problem is to find a path between two vertices in such a way that the
path will satisfy optimization criteria. The starting vertex of the path will be referred to as the
source and the last vertex be the destination.

 For non-weighted graph, optimization is number of edges will be minimum.


 For weighted graph, the sum of weights on all edges in the path will be minimum.

Single source shortest path

Graphs are used to represent highway structure with vertices represent cities and the
edges represent sections of the highway. Each edge has a weight representing the distance
between the two cities connected by the edge. Everyone is interested to move from one city
to another city with minimum distance.

Let G=(V,E) be a weighted graph. The aim of the problem statement is to determine
the shortest paths from vertex V0 to all remaining vertices of G.

The Greedy strategy (Dijkstra’s algorithm) generates shortest paths from vertex V0
to the remaining vertices in non-decreasing order of their path length. Let S denotes the set
of vertices including V0 to which the shortest path have already been generated.
Procedure:

Visvodaya Technical Academy Page 162

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Step 1: Create a set S that keeps track of vertices included in shortest path tree.
Initially the set is empty.
Step 2: Assign a distance value to all vertices in the input graph. Initialize all
distance values as INFINITE. Assign distance value as 0 for the
source vertex so that it is picked first.
Step 3: While S doesn’t include all vertices, then
i. Pick a vertex u which is not there in S and has minimum
distance value.
ii. Include u to S.
iii. Update distance value of all adjacent vertices of u. To update
the distance values, iterate through all adjacent vertices. For
every adjacent vertex v, if sum of distance value of u and
weight of edge u-v, is less than the distance value of v, then
update the distance of v.

Algorithm shortestpath (V, Cost, Dist, n)


{
Repeat FOR i ← 1 TO n DO STEPS BY 1
S[i] ← FALSE
Dist[i] ← Cost[V,i]
ENDRepeat

S[v] ← TRUE
Dist[v] ← 0

Repeat FOR NUM ← 1 to n-1 DO STEPS BY 1


Choose u from among those vertices not in S such that Dist[u] is minimum
S[u] ← TRUE

FOR ( Each w adjacent to u with S[w] = FALSE ) DO


{
If (Dist[w] > Dist[u] + Cost[u,w]) THEN
Dist[w] ← Dist[u] + Cost[u,w];
}
}

Example: Obtain shortest path for the following graph

Analysis: The time complexity of single shortest path problem is O(n2) time.
All Pairs Shortest Path

Visvodaya Technical Academy Page 163

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Let G=(V,E) be a directed graph with n vertices. Let Cost be an adjacency matrix of
G such that cost(i,i) = 0; cost(i,j) is the length of the edge <i,j> if <i,j> belongs to E(G) and
cost(i,j) = α if <i,j> not belongs to E(G).

All pairs shortest path problem is to determine a matrix A such that A(i,j) is length of
the shortest path from i to j. The matrix A can be obtained by solving n-single source shortest
path problems.

By applying dynamic programming strategy, it can be solved as:

Step 1: Initially design A0 matrix with the values as:

Cij = cost(e) ; If there is an edge from Vi to Vj


= α ; Otherwise.

Step 2: Ak(i,j) be the length of the shortest path from node i to node j such that
every intermediate node will be <= k. Now compute Ak = 1, 2, - - - -, n
nodes.
When intermediate vertex k arises, the two possible cases are:
i. Path going from i to j via k.
ii. Path not going via k, then principle of optimality holds.

Step 3: Shortest path can be computed using the formula:

Ak (i,j) = Min [ Ak-1(i,j) , Ak-1(i,k) + Ak-1(k,j) ] Where k ≥ 1.

If k is selected then,
Ak (i,j) = Ak-1(i,k) + Ak-1(k,j)
Otherwise,
Ak (i,j) = Ak-1(i,j)

Algorithm shortestpath(): This procedure is used to generate path matrices that shows
shortest path between every pair of vertices of graph G.

Step 1: Repeat FOR i ← 1 to N DO STEPS BY 1


Repeat FOR j ← 1 to N DO STEPS BY 1
IF A[i][j] = 0 THEN
A[i][j] ← α
ELSE
A[i][j] ← C[i][j]
ENDIF
EndRepeat
EndRepeat
Step 2: Repeat FOR k ← 1 to N DO STEPS BY 1
Repeat FOR i ← 1 to N DO STEPS BY 1
Repeat FOR j ← 1 to N DO STEPS BY 1
A[i][j] ← MIN ( A[i][j] , A[i][k] + A[k][j] )
EndRepeat
EndRepeat

Visvodaya Technical Academy Page 164

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

EndRepeat
Step 3: RETURN

Example: Calculate shortest path for the weighted graph

1
2 1

2 3 3

1 2
4

Analysis: The first nested for loop takes O(n2) time. The second loop nested for loop
takes O(n3) time. Therefore, the overall time complexity of above procedure is O(n3) time.

TRANSITIVE CLOSURE

Consider a directed graph G with unweighted edges. To determine shortest path from
i to j for all values of i and j, two cases are possible. The first case requires positive path
lengths, while the second requires only nonnegative path lengths. These cases are known as
the transitive closure and reflexive transitive closure of a graph respectively.

Transitive Closure: The transitive closure matrix, denoted A+, of a graph, G, is a matrix
such that

A+[i][j] = 1 if there is a path of length > 0 from i to j;


otherwise, A+[i][j] = 0.

Reflexive Transitive Closure: The reflexive transitive closure matrix, denoted A*, of a
graph, G, is a matrix such that

A*[i][j] = 1 if there is a path of length ≥ 0 from i to j;


otherwise, A0[i][j] = 0. (Similar to transitive closure except that A*[i][i] = 1)

Example: Consider the digraph G as:

Visvodaya Technical Academy Page 165

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

0 1 2 3 4

For this,

Adjacency matrix A = 0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
0 0 1 0 0

Transitive closure matrix A+ = 0 1 1 1 1


0 0 1 1 1
0 0 1 1 1
0 0 1 1 1
0 0 1 1 1

Reflexive Transitive closure matrix A* = 1 1 1 1 1


0 1 1 1 1
0 0 1 1 1
0 0 1 1 1
0 0 1 1 1

***

TABLES

Visvodaya Technical Academy Page 166

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

A table is a non-linear data structure that organizes the given data into rows and
columns. It can be used to store and display data in a structure format. The diagrammatic
representation of a table data structure is as follows:

Examples: Rectangular tables, Jagged tables, Inverted tables, Hash tables etc.

Rectangular Tables

Rectangular tables are also known as matrices. In matrix format data is organized in
terms of rows and columns.

Example:
* * * *
* * * *
* * * *

Jagged Tables

Jagged tables are the special kind of sparse matrices such as triangular matrices; band
matrices etc. with an additional constraint such that in a row (or in a column) if elements are
present then they are contiguous.

Example:

* * *
* * * * *
* * *
* * *
* * *
* * * *
* *
Inverted Tables

Visvodaya Technical Academy Page 167

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Inverted table is kinds of table that can be avoid multiple set of records while
organizing in a structured format based on the specified constraints.

Example: Assume a company maintains employee data records as:

Index Name of the Employee Address Phone Number


1 Mahesh Kumar Kavali 257696
2 Anil Kumar Chennai 257459
3 Varun Nellore 257483
4 Bharath Chennai 257429
5 Sravan Kumar Hyderabad 257590

Specified constraints are:


 Alphabetical ordering of name of employees
 Alphabetical ordering of addresses
 Ascending order of phone numbers.

For this, Inverted table can be created as:

Name Address Phone


2 2 4
4 4 2
1 5 3
5 1 5
3 3 1

HASH TABLES

Collections of records are possible to store in a particular table format called Hash
table by using the function known as Hash function. This procedure is known as Hashing.

Example:
4 84
Hash Table Size = 5
3
13
27
2

1 36

0 40

Here,

Visvodaya Technical Academy Page 168

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Assume ‘K’ is the key element. Then key K is stored in position f(K) of the
hash table. Where, f is the hash function. Each position of the hash table is a bucket and
f(K) is the home bucket for the key element K. Each key is mapped into some numbered
cell of the hash table using hash function.

Hashing: The process of mapping a collection of records into appropriate positions of


the hash table is known as a hashing process.

Hash Table: Table used for storing a collection of records into appropriate positions
by using hash function is known as a hash table.

Hash Function: Function used for storing a collection of records into appropriate
positions of the hash table is known as a hash function.

HASHING TECHNIQUES / HASH FUNCTIONS

The given set of values is stored into appropriate a position of the hash table with the
help of hash function is known as hashing. Methods used for creating hash functions are:

1. Truncation method
2. Folding method
3. Mid square method
4. Division hash functions etc.

1. Truncation Method

In truncation method, the hash function H of the key ‘K’ is obtained by ignoring a set
of digits and rest of the combination is used as the array index position of the hash table.

Example: Assume the key K = 9 2 8 3 1 4 2 7 5

 H(K) = H(928314275) = 37

Therefore, the given key 928314275 is stored in 37th index position of the hash table.

2. Folding Method

In folding method, partition the key ‘K’ into a number of parts as K 1 , K2 , - - - Kn.
Each of these individual parts is added together and applies truncation method to obtain the
final index position to the key element ‘K’.

Example: Assume the key K = 9 2 8 3 1 4 2 7 5

 H(K) = H(928314275) = 928 + 314 + 275 = 1 5 1 7 = 11

Therefore, the given key 928314275 is stored in 11th index position of the hash table.

3. Mid Square Method

Visvodaya Technical Academy Page 169

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

In mid square method, the hash function H of the key ‘K’ is obtained by selecting an
appropriate number of digits from the middle of the square of the key value ‘K’.

Example: Assume the key K = 486

 H(K) = H(486) = K2 = 236196 = 269

Therefore, the given key 486 is stored in 269th index position of the hash table.

4. Division Hash Function Method

One of the fast and most widely used hash function method is division hash function.
In this method, the hash function H of the key ‘K’ is defined by

H (K) = K%D

Where, K is the key value and D is the length/size of the hash table.
Ideal choice is D is taken as a prime number.

Example: Assume the key K = 486 and size of the hash table is 5.

 H(K) = H(486) = 486 % 5 = 1

Therefore, the given key 486 is stored in 1st index position of the hash table

COLLISION AND OVERFLOW

Overflow: While inserting keys into the hash table, if empty locations are not available
for further insertion then status of the hash table is referred to as overflow situation.

Example: Insert the keys 6, 1, 23, 2 into a hash table of size 3.

2 23 Key 6 => H(6) = 6%3 = 0

1 1 Key 1 => H(1) = 1%3 = 1


6
0 Key 23 => H(23) = 23%3 = 2

Key 2 => H(2) = 2%3 = 2

Here, insertion is not possible. Since, the hash table is full with the given keys.
Overflow occurs.
Overflow problem can be avoided by determining size of the hash table as
greater than the number of inserting keys.

Visvodaya Technical Academy Page 170

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Collision: While inserting keys into the hash table, if two or more keys tried to access the
same location of the hash table then status of the hash table is referred to as collision
situation.

Example: Insert the keys 3, 7, 15 and 60 into a hash table of size 5.

4
Key 3 => H(3) = 3%5 = 3
3 Key 7 => H(7) = 7%5 = 2
Key 15 => H(15) = 15%5 = 0
3
2 Key 60 => H(60) = 60%5 = 0

1 7

0 15

Here, insertion not possible. Collision occurred.

COLLISION RESOLUTION TECHNIQUES

Methods used to resolve collision problems are:

A. Closed hashing
B. Open hashing

A) CLOSED HASHING

The simplest method to resolve a collision problem is closed hashing process. In this
process, whenever a collision occurred then alternative locations are tried until an empty
location is found.

The alternative empty locations can be obtained with various location methods as:

i) Linear probing
ii) Quadratic probing
iii) Double hashing
iv) Rehashing

In every method, the table and locations are made as in circular fashion.

i) Linear Probing:

In linear probing method an alternative empty location is obtained by using the


equation:
f(i) = i
Here, substitute i value as 1, 2, 3, - - - - .

When values are substituted, empty locations are probed as 1 st location from collision,
nd
2 location from collision and so on.

Visvodaya Technical Academy Page 171

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Example: Insert the keys 3, 7, 15 and 60 into a hash table of size 5.

0 1 2 3 4
15 60 7 3

Key 3 => H(3) = 3%5 = 3


Key 7 => H(7) = 7%5 = 2
Key 15 => H(15) = 15%5 = 0
Key 60 => H(60) = 60%5 = 0

Here, collision occurred. Since, other element 15 is available in 0 th index position.


Now, apply linear probing to get an alternative empty location as:

f(i) = i

Let i = 1 => f(1) = 1

i.e. 1st location from collision. This is an empty location. Hence, the key 60 is possible to
insert into index position 1 of the hash table.

ii) Quadratic Probing:

In linear probing method an alternative empty location is obtained by using the


equation:
f(i) = i2
Here, substitute i value as 1, 2, 3, - - - - .

When values are substituted, empty locations are probed as 1 st location from collision,
4th location from collision, 9th location from collision and so on.

Example: Insert the keys 89, 18, 49 and 58 into a hash table of size 10.

0 1 2 3 4 5 6 7 8 9
49 58 18 89

Key 89 => H(89) = 89%10 = 9


Key 18 => H(18) = 18%10 = 8
Key 49 => H(49) = 49%10 = 9

Here, collision occurred. Since, other element 89 is available in 9th index position.

Now, apply quadratic probing to get an alternative empty location as:

Visvodaya Technical Academy Page 172

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

f(i) = i2

Let i = 1 => f(1) = 12 = 1


st
i.e. 1 location from collision. This is an empty location. Hence, the key 49 is possible to
insert into index position 0 of the hash table.

Key 58 => H(58) = 58%10 = 8

Here, collision occurred. Since, other element 18 is available in 8 th index position.


Now, apply quadratic probing to get an alternative empty location as:

f(i) = i2

Let i = 1 => f(1) = 12 = 1


i.e. 1st location from collision. This is not an empty location.

Let i = 2 => f(2) = 22 = 4


st
i.e. 1 location from collision. This is an empty location.

Hence, the key 58 is possible to insert into index position 2 of the hash table.

iii) Double Hashing:

In double hashing method an alternative empty location is obtained by using the


equation:

f(k) = i * h1 (k)
Where i = 1, 2, 3, - - - -

h1 (k) = q – (k mod q)
Where q is a prime number

Double hashing uses two hash functions.

Example: Insert the keys 89, 18, 49, 58 and 69 into a hash table of size 10.

0 1 2 3 4 5 6 7 8 9
69 58 49 18 89

Key 89 => H(89) = 89%10 = 9


Key 18 => H(18) = 18%10 = 8
Key 49 => H(49) = 49%10 = 9

Here, collision occurred. Since, other element 89 is available in 9th index position.

Now, apply double hashing to get an alternative empty location as:

Visvodaya Technical Academy Page 173

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

f(k) = i * h1 (k) h1 (k) =


q – (k mod q)
= 1*7=7 h1(49) =
7 – (49%7)
7–0=7
i.e. 7th location from collision. This is an empty location. Hence, the key 49 is possible to
insert into index position 6 of the hash table.

Key 58 => H(58) = 58%10 = 8

Here, collision occurred. Since, other element 18 is available in 8 th index position.


Now, apply double hashing to get an alternative empty location as:

f(k) = i * h1 (k) h1 (k) =


q – (k mod q)
= 1*5=5 h1(58) =
7 – (58%7)
7–2=5
i.e. 5th location from collision. This is an empty location. Hence, the key 58 is possible to
insert into index position 3 of the hash table.

Key 69 => H(69) = 69%10 = 9

Here, collision occurred. Since, other element 89 is available in 9 th index position.


Now, apply double hashing to get an alternative empty location as:

f(k) = i * h1 (k) h1 (k) =


q – (k mod q)
= 1*1=1 h1(69) =
7 – (69%7)
7–6=1
i.e. 1st location from collision. This is an empty location. Hence, the key 69 is possible to
insert into index position 0 of the hash table.

iv) Rehashing:

In rehashing method if more than half of the locations are filled with the given keys
then build another hash table that is twice as big in size as the old hash table and scan down
all the keys into the new hash table.

Example: Insert the keys 13, 15, 24 and 60 into a hash table of size 7.

0 1 2 3 4 5 6
15 24 60 13

At this stage, it uses the hash function H (k) = k % 7 and more than half of the hash
table is full with the given keys.

Now, if we like to insert another key as 23 into the hash table. Then it is better to
apply rehashing process.

In this method, create a new table of size 17. Since 17 is the first prime number
which is near to the twice of the old hash table size. So that new hash function becomes:

Visvodaya Technical Academy Page 174

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

H (k) = k % 17

After rehashing process, the elements positioned into the new table as:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
23 24 13 13 15

B) OPEN HASHING

Open hashing is also known as separate chaining method. This method uses a hash
table as an array of pointers; each pointer points to a linked list. Hence, this method also
treated as linked list collision resolution.

In this procedure first hash table location of the given key is calculated with the help
of the division hash function. Now control enters into the particular location and then
searches the linked list pointed by the pointer in the same location. Then the key is inserted
into the location at the end of the list.

The main advantage of this type of hashing is the keys are inserted into the same
index positions specified by the division hash function.

Example: Insert the keys 10, 24, 12, 23, 36, 57, 64, 32, 54 into a hash table of
size 10.
9
H(10) = 10%10 = 0
H(24) = 24%10 = 4 8
H(12) = 12%10 = 2
H(23) = 23%10 = 3 7 57
H(36) = 36%10 = 6
H(57) = 57%10 = 7 6 36
H(64) = 64%10 = 4
H(32) = 32%10 = 2 5
H(54) = 54%10 = 4
4 24 64 54

3 23
2 12 32

1
10
0

Advantages of Open hashing

Visvodaya Technical Academy Page 175

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

 It used dynamic storage management policy.


 Collision resolution can be achieved efficiently with the help of pointers.
 Overflow situation never arises.

Load Factor:

To improve efficiency of hashing algorithms, a hashed list should not be allowed to


become more than 75% full. This guideline leads to a concept called load factor.

Load factor (α) of hashed list is the number of filled elements in the list divided by the
number of physical elements allocated for the list, expressed as a percentage. i.e., load factor
is given by

α = (k/n) * 100;

Where,

k is the number of filled elements and n is the total number of elements allocated to
the list.

Clustering:

As data are added to a list and collisions are resolved, then data keys are spread
unevenly across the hashed list. This tendency of data to build up unevenly across a hashed
list is known as clustering.

Clustering is divided into two types as: primary clustering and secondary clustering.

 Primary clustering occurs when data cluster around a home address.


 Secondary clustering occurs when data become grouped along a collision path
throughout a list.

Clustering is usually created by collisions. If a list contains a high degree of clustering,


the number of probes to locate an item grows and reduces the processing efficiency of the list.

BUCKET HASHING

Bucket hashing is another closed hashing collision resolution technique.

Visvodaya Technical Academy Page 176

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

In bucket hashing method, consider a hash table with M slots that is divided into B
buckets with each bucket consists of M/B slots.

The hash function assigns each record to the first slot within one of the specified
buckets. If the slot is occupied with other key; then the bucket slots are searched sequentially
until an open slot is found. If a bucket is entirely full with the given keys then the new key is
placed in an overflow bucket table which is maintained at the end of the hash table. All
buckets of the hash table shares the same overflow bucket.

Example: Insert the keys 23, 16, 30, 20, 48, 25 and 8 into a hash table of size 10.

Given Number of Slots M = 10

Assume Number of Buckets B = 5

Therefore, Number of slots in each bucket = M/B = 10/5 = 2

Hash Table:

0 1 2 3 4 5 6 7 8 9
30 20 16 23 48

B0 B1 B2 B3 B4

Overflow Bucket Table:

0 1 2 3 4 5
25 8

Key 23 = H(23) = 23%5 = 3


i.e. Key 23 is inserted into the first location of B3 bucket.

Key 16 = H(16) = 16%5 = 1


i.e. Key 16 is inserted into the first location of B1 bucket.

Key 30 = H(30) = 30%5 = 0


i.e. Key 30 is inserted into the first location of B0 bucket.

Key 20 = H(20) = 20%5 = 0

Visvodaya Technical Academy Page 177

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

i.e. Key 20 is inserted into the first location of B0 bucket. But location is not empty.
Hence, control moves to next location of the B0 bucket which is empty. Hence insert the key
20 into second location of B0 bucket.

Key 48 = H(48) = 48%5 = 3


i.e. Key 48 is inserted into the first location of B3 bucket. But location is not empty.
Hence, control moves to next location of the B3 bucket which is empty. Hence insert the key
48 into second location of B3 bucket.

Key 25 = H(25) = 25%5 = 0


i.e. Key 25 is inserted into the first location of B0 bucket. But location is not empty.
Hence, control moves to next location of the B0 bucket which is not empty. And Bucket B0
is full with the given keys and overflow occurs. Now the key 25 is inserted into the overflow
bucket table.

Key 8 = H(8) = 8%5 = 3


i.e. Key 8 is inserted into the first location of B3 bucket. But location is not empty.
Hence, control moves to next location of the B3 bucket which is not empty. And Bucket B3
is full with the given keys and overflow occurs. Now the key 8 is inserted into the overflow
bucket table.

***

Unit-V

Visvodaya Technical Academy Page 178

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Files and Advanced sorting

File Organization: Sequential file organization, Direct file organization, Indexed sequential
file organization.
Advanced sorting: Sorting on several keys, Lists and Table sorts, Summary on Internal
sorting and External sorting

***

INTRODUCTION

A file is a collection of records which contain data about individual entities. A record
may be decomposed into more units called fields which contain specific values.

Example: Consider the following employee file as:

Employee Employee Name Department Designation Salary


Number
0189 Ravi Kumar Data Proc. Programmer 26,282.00
0067 Sarath Chandra Data Proc. Programmer 27,859.00
0367 Vishnuvardhan Security Manager 43,000.00

Here,

 Each row represents a record.


 Employee Number, Employee Name, Department, Designation and Salary are fields.
 The primary key is a field, or a composite of several fields, which uniquely
distinguishes a record from all others; all the remaining fields are the secondary keys.
Example: Primary Keys : Employee Number
Secondary Keys : Department, Designation etc.
 To process any file in an efficient way, it must be organized in three ways such as:
1) Sequential file organization
2) Direct file organization
3) Indexed sequential file organization

The basic differences between these file organizations are:

Organization Access
Sequential File Sequential
Direct File Direct
Indexed Sequential File Sequential and Direct

 Sequential access refers to accessing multiple records according to a predefined


order.
 Direct access also called as random access refers to locating a single record.
 Indexed sequential access refers to access in both sequential and direct methods.

Visvodaya Technical Academy Page 179

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

1) SEQUENTIAL FILE ORGANIZATION

In sequential file organization, elements are stored in sequential order i.e., i+1th
element of a file is stored immediately after the ith element as:

1 2 3 - - i i+1 i+2 - - n-1 n

Array is an example of sequential organized information. To access the data of an


array in sequential format, the basic methods are:

a) Sequential search
b) Binary search
c) Interpolation search etc.

a) Sequential Search

Consider an array K with n elements as K[1], K[2], . . . K[n]. Assume an element of


information is given in the variable Key to search.

In sequential search technique, compare the key element with each element of K form
index 1 to index n. At any position i, if K[i] = Key, then return index i value refers to
successful search (Element Found). For the entire procedure K[i] ≠ Key and elements are not
available for further comparison, then return -1 refers to unsuccessful search (Element not
Found). For this, the functional procedure can be placed as:

int search(int K[50], int n, int Key)


{
int i;
for(i=1 ; i<=n ; i++)
{
if(K[i] = = Key)
return i;
}
return -1;
}

Analysis:

Analysis of search methods depends on the number of comparisons applied to search


the given element. In the linear search technique, if the function finds the search element at
the first position of the list, it does only one key comparison. If the target element is the
second element of the list, it requires two comparisons and so on.

Hence, the worst case and average case time complexity of sequential search is O (n).

Visvodaya Technical Academy Page 180

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

b) Binary Search

Binary search can be applied only on the array elements which is available in sorted
order. Consider K is an array consists of n elements in sorted order such that K[1] ≤ K[2]
≤ . . . . ≤ K[n]. Suppose an item of information is given to search in the variable Key. Then
binary search procedure works as:

In binary search technique, first compute

Mid

Where, Low refers to the first index and High refers to last index of the array at the
initial call. Now, the process falls into any one of the following three cases.

Case 1: If Key = K[Mid]; Then the search is successful search i.e., Element Found.
Case 2: If Key > K[Mid]; Then the Key element can appear only in the right half of
the array. So, we reset the Low value as Low = Mid+1 and begin search
again.
Case 3: If Key < K[Mid]; Then the Key element can appear only in the left half of the
array. So, we reset the High value as High = Mid-1 and begin search again.
The above procedure is repeated up to we reach Low > High. When we obtain this
condition, it indicates that the search is unsuccessful search i.e., Element not found.

For this, functional procedure can be placed as:

int search(int K[50], int Low, int High, int Key)


{
int Mid;
while(Low <= High)
{
Mid = (Low + High) / 2;
if (Key = = K[Mid])
return Mid;
else if (Key > K[Mid])
Low = Mid + 1;
else
High = Mid – 1;
}
return -1;
}

Analysis:

The complexity is measured by the number of comparisons to locate the search item
in the given array elements.

In binary search, each comparison reduces the size of the array into half. So that
number of comparisons is less compare to linear search. Hence, the worst case and average
case time complexity of binary search is O (log n).

Visvodaya Technical Academy Page 181

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

c) Interpolation Search

Interpolation search is another searching technique can be used only when the list of
elements is ordered and uniformly distributed.

The interpolation search is an improvement over binary search for instances, where
the values in a sorted array are uniformly distributed. Binary search always goes to the
middle element to check. On the other hand, interpolation search may go to different
locations according to the value of key being searched.

Let K is an array that consists of n elements from index 0 to n-1. Search element is
given in the variable Key. Then interpolation search procedure works as:

Step 1:In a loop, calculate the value of “Pos” using the formula

Pos = Low + [ (Key – K[Low]) * (High – Low) / (K[High] – K[Low]) ]

Where, Low refers to starting index 0 and High refers to ending index n-1 at the first
call.

Step 2:Compare K[Pos] element with Key element. If it is a match, return Pos
position that refers to element found.

Step 3:If the Key element is less than K[Pos] element, calculate the next position at
the left sub-array by changing High value as High = Pos – 1.

Step 4: If the Key element is greater than K[Pos] element, calculate the next position
at the right sub-array by changing Low value as Low = Pos + 1.

Step 5:Repeat the above procedure until the search element is found or the sub-array
reduces to zero.

For this, functional procedure can be placed as:

int search(int K[5],int n,int Key)


{
int Pos;
int Low=0,High=n-1;
while(Low<=High)
{
if(Low==High)
{
if(K[Low]==Key)
return Low;
else
return -1;
}

Visvodaya Technical Academy Page 182

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Pos=Low+((Key-K[Low])*(High-Low)/(K[High]-K[Low]));
if(K[Pos]==Key)
return Pos;
else if(K[Pos]<Key)
Low=Pos+1;
else
High=Pos-1;
}
return -1;
}

Analysis:

The interpolation search is an improvement over binary search for instances, where
the values in a sorted array are uniformly distributed. Binary search always goes to the
middle element to check. On the other hand, interpolation search may go to different
locations according to the value of key being searched.

Hence, the worst case complexity of interpolation search is O(n) and average case
time complexity of interpolation search is O (log2 log2 n).

KEY OBSERVATIONS

 Sequential search is preferable when data is stored in primary memory and need to be
handling limited amount of data.
 Binary search is preferable when data is stored in primary memory and it must be
available in sorted order.
 Interpolation search is preferable when data is stored in auxiliary memory due to
additional calculations and it must be available in sorted order and uniformly
distributed.

SELF-ORGANIZING SEQUENTIAL SEARCH

In previous search methods, all the records in the file would be accessed equally
often. To improve the retrieval performance for a sequential search by organizing the file,
consider the frequency of access for the records and place them at the beginning of the file.

So that a self-organizing sequential search modifies their order for the purpose of
moving the most frequently retrieved records to the beginning of the file to improve the
performance of sequential search.

Different methods to perform a self-organizing sequential search are:


a) Move_to_front
b) Transpose
c) Count

Visvodaya Technical Academy Page 183

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

a) Move_to_front Method

In the Move_to_front algorithm, when the particular record is located, it is moved to


the front position of the file and all the intervening records are moved back one position.

The Move_to_front algorithm does handle locality of access well; locality means that
a record that has recently been accessed is more likely to be accessed again in near future.
This process is essentially the same as the LRU (Least Recently Used) paging algorithm used
by the operating system.

Example: Assume a file consists of records format

A B C D E F G H I J K L M N O P Q R S T U VW X Y Z

Assume frequently occurring records are E and I then, they shifted to front positions as:

E I A B C D F G H JK L M N O P Q R S T U VW X Y Z

Note: Move_to_front is appropriate when space is not limited and locality of access
is important.

b) Transpose Method

In transpose method, the particular search record interchanges with its immediate
predecessor unless it is in the first position. With this approach, a record needs to be accessed
many times before it is moved to the front of the list. This method is stable compared to
Move_to_front and avoid big mistakes.

Note: Transpose is appropriate when space is at premium.

c) Count Method

The count method keeps a count of the number of access of each record. The record
is then moved in the file to a position in front of all the records with fewer accesses. The file
is then always ordered in a decreasing order of frequently of access.

The disadvantage of the count algorithm is it requires extra storage to keep the count
of the accesses and it does not handle the locality of access phenomenon well.

Note: Because of its storage representation, use it only when the counts are needed
for another purpose.

Visvodaya Technical Academy Page 184

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

2) DIRECT FILE ORGANIZATION

Direct file organization refers to access the records directly without accessing all the
records. In such cases, sometimes keys were also treated as address. In direct file
organization, we consider two types of methods for finding direct locations as unique address
based on subscript value and hashing methods.

A) UNIQUE ADDRESS – SUBSCRIPT METHOD

In this method, if the subscript of an array element is given then determine its address
to store in contiguous location as:

Assume the given array dimension as: K[LB:UB]

Where, LB refers to lower bound


UB refers to upper bound of the array K.

Then, the address of particular location K[i] can be calculated as:

Address of K[i] = First Address + (i – LB) * Size of Element

Example: Consider the array format as

1 2 3 4 5 6

1000 1002 1004 1006 1008 1010

To locate 4th record of existing file, it can be located directly as:

K[4] = 1000 + (4 – 1) * 2 = 1000 + 3 * 2 = 1000 + 6 = 1006

Hence, control directly located into the address location 1006.

B) HASHING PROCESS

The process of mapping a collection of records into appropriate positions of the hash
table is known as a hashing process.

Example: 2 14
46
1
30
0

Visvodaya Technical Academy Page 185

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

HASHING TECHNIQUES / HASH FUNCTIONS

The given set of values is stored into appropriate a position of the hash table with the
help of hash function is known as hashing. Methods used for creating hash functions are:

1. Truncation method
2. Folding method
3. Mid square method
4. Division hash functions etc.

1. Truncation Method

In truncation method, the hash function H of the key ‘K’ is obtained by ignoring a set
of digits and rest of the combination is used as the array index position of the hash table.

Example: Assume the key K = 9 2 8 3 1 4 2 7 5

 H(K) = H(928314275) = 37

Therefore, the given key 928314275 is stored in 37th index position of the hash table.

2. Folding Method

In folding method, partition the key ‘K’ into a number of parts as K 1 , K2 , - - - Kn.
Each of these individual parts is added together and applies truncation method to obtain the
final index position to the key element ‘K’.

Example: Assume the key K = 9 2 8 3 1 4 2 7 5

 H(K) = H(928314275) = 928 + 314 + 275 = 1 5 1 7 = 11

Therefore, the given key 928314275 is stored in 11th index position of the hash table.

3. Mid Square Method

In mid square method, the hash function H of the key ‘K’ is obtained by selecting an
appropriate number of digits from the middle of the square of the key value ‘K’.

Example: Assume the key K = 486

 H(K) = H(486) = K2 = 236196 = 269

Therefore, the given key 486 is stored in 269th index position of the hash table.

Visvodaya Technical Academy Page 186

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

4. Division Hash Function Method

One of the fast and most widely used hash function method is division hash function.
In this method, the hash function H of the key ‘K’ is defined by

H (K) = K%D

Where, K is the key value and D is the length/size of the hash table.
Ideal choice is D is taken as a prime number.

Example: Assume the key K = 486 and size of the hash table is 5.

 H(K) = H(486) = 486 % 5 = 1

Therefore, the given key 486 is stored in 1st index position of the hash table

COLLISION

While inserting keys into the hash table, if two or more keys tried to access the same
location of the hash table then status of the hash table is referred to as collision situation.

Example: Insert the keys 3, 7, 15 and 60 into a hash table of size 5.

4
Key 3 => H(3) = 3%5 = 3
3 Key 7 => H(7) = 7%5 = 2
Key 15 => H(15) = 15%5 = 0
3
2 Key 60 => H(60) = 60%5 = 0

1 7

0 15

Here, insertion not possible. Collision occurred.

COLLISION RESOLUTION TECHNIQUES

Methods used to resolve collision problems are:

a) Closed hashing
b) Open hashing

a) CLOSED HASHING

The simplest method to resolve a collision problem is closed hashing process. In this
process, whenever a collision occurred then alternative locations are tried until an empty
location is found.

Visvodaya Technical Academy Page 187

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

The alternative empty locations can be obtained with various location methods as:

i) Linear probing
ii) Quadratic probing
iii) Double hashing
iv) Rehashing

In every method, the table and locations are made as in circular fashion.

i) Linear Probing:

In linear probing method an alternative empty location is obtained by using the


equation:
f(i) = i
Here, substitute i value as 1, 2, 3, - - - - .

When values are substituted, empty locations are probed as 1 st location from collision,
2nd location from collision and so on.

ii) Quadratic Probing:

In linear probing method an alternative empty location is obtained by using the


equation:
f(i) = i2
Here, substitute i value as 1, 2, 3, - - - - .

When values are substituted, empty locations are probed as 1 st location from collision,
4 location from collision, 9th location from collision and so on.
th

iii) Double Hashing:

In double hashing method an alternative empty location is obtained by using the


equation:

f(k) = i * h1 (k)
Where i = 1, 2, 3, - - - -

h1 (k) = q – (k mod q)
Where q is a prime number

Double hashing uses two hash functions.

iv) Rehashing:

In rehashing method if more than half of the locations are filled with the given keys
then build another hash table that is twice as big in size as the old hash table and scan down
all the keys into the new hash table.

b) OPEN HASHING

Visvodaya Technical Academy Page 188

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Open hashing is also known as separate chaining method. This method uses a hash
table as an array of pointers; each pointer points to a linked list. Hence, this method also
treated as linked list collision resolution.

In this procedure first hash table location of the given key is calculated with the help
of the division hash function. Now control enters into the particular location and then
searches the linked list pointed by the pointer in the same location. Then the key is inserted
into the location at the end of the list.

The main advantage of this type of hashing is the keys are inserted into the same
index positions specified by the division hash function.

Note:

1) Close hashing method is treated as static hashing. Since, it uses array


implementation (static implementation).
2) Open hashing method is treated as dynamic hashing. Since, it uses linked list
implementation.

BUCKET HASHING

Bucket hashing is another closed hashing collision resolution technique.

In bucket hashing method, consider a hash table with M slots that is divided into B
buckets with each bucket consists of M/B slots.

The hash function assigns each record to the first slot within one of the specified
buckets. If the slot is occupied with other key; then the bucket slots are searched sequentially
until an open slot is found. If a bucket is entirely full with the given keys then the new key is
placed in an overflow bucket table which is maintained at the end of the hash table. All
buckets of the hash table shares the same overflow bucket.

***

3) INDEXED SEQUENTIAL FILE ORGANIZATION

Visvodaya Technical Academy Page 189

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Indexed sequential file organization is a combination of sequential and direct file


organization and is suitable for those applications in which the records should be access both
directly and sequentially. It is useful when we need to search a single record at some times
and many records at other times.

In general, when we apply sequential file organization performance characteristics is


very low. Since, it accesses the records in sequential order one by one. To improve the
performance of sequential file organization a slight modification is added known as indexed
sequential file organization.

To improve the performance, order the information and put tabs or an index at some
particular points to group the information. Then search the record with index until we find
the group that desired record should be present. So, that it reduces number of comparisons
compared to normal sequential file accessing method. Still to improve performance, next
high level indexes can also be placed and so on.

Example: Consider the given file of records as:

11 23 45 86 125 157 178 200 225 241 269

For this, set the index values as:

100 200

50 100 150 200 250

11 23 57 86 125 137 178 200 225 241 269

Here,
100 and 200 are high level index values
50, 100, 150, 200 and 250 are low level index values

Now to locate a particular record, first search record key value compared with high
level index value. If the value is less than or equal to specific index value then search process
moves to its left side; otherwise, it moves to its right side. Now, it compares with its low
level index value and applies the same procedure.

Based on the comparison, finally control moves to one particular set of records then
search procedure starts sequentially. If key is found then it refers to successful search;
otherwise, it refers to unsuccessful search.

BASIC STRUCTURE

Visvodaya Technical Academy Page 190

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

In general indexed sequential file organization can handle huge amount of data and
uses auxiliary devices to store a collection of records. A file organized with an indexed
sequential structure is commonly referred to as an ISAM (Indexed Sequential Access
Method) file.

To maintain indexed sequential file structure, use tracks as the lowest level of
grouping information and cylinders as the highest level of grouping information.

The cylinder index contains a pair of entries such as key and ptr terms.

Key Ptr

Here, Key is the highest key of any record on that cylinder


Ptr is a pointer to the track index for that cylinder.

The track in a cylinder has two pairs of entries associated with it in the track index.
One pair contains information on the primary storage area and the other pair has
information on the overflow records associated with the track

Key Ptr
Key Ptr

Primary Overflow

Here, the Key in the primary storage area is the highest key on that track and Ptr
indicates the track containing the primary records. The key in the overflow is the highest key
in the overflow area associated with the track and Ptr indicates the first overflow record, if
one exists for the primary track.

Example: Consider the following example of indexed sequential structure as:

Visvodaya Technical Academy Page 191

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Here, the cylinder index gives the explicit entries for the cylinders 1, 2, 3 and so on.
The highest key on cylinder 1 is 350 and pointer notation is x-y format. Where x gives the
cylinder number and y gives track number where the track index for that cylinder is stored.
So the pointer 1-0 means cylinder 1 and track 0.

Track index focuses on primary storage index values and overflow storage index
values. The track number keys are the maximum value of primary storage area value as same
as overflow storage key value. Track pointer locations are filled with primary storage index
values and overflow pointer values are initially filled with NULL values.

Assume a record of key 8 is needed to add into the existing structure. First compare it
with cylinder index 1 value 350. Here 8<=350 hence, it enters into cylinder 1. Now the
value is compared with track number 0 which is associated with cylinder 1. Here 8<=10
hence control enters into primary storage index value 1 and compare with the values in
sequential order. Here, 8 is placed in between 7 and 9 so that 13 comes out from the index
number 1 and enters into overflow storage with necessary modifications.

Now the status becomes:

Performance: When we observe the above example, it maintains only one pointer
overflow storage index for each primary index values. If number of values are exceed in
overflow storage are then it causes different complexity problems.

Overflow

The overflow area may be implemented in either one of two ways – cylinder
overflow or independent overflow.

In cylinder overflow, the overflow area is one the same cylinder as the primary
storage area. The advantage of cylinder overflow area is that the read/write head on an
auxiliary storage disk does not need to be repositioned to another cylinder to access an
overflow record. The diagrammatic view is as follows:

Visvodaya Technical Academy Page 192

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

In independent overflow, several cylinders of primary storage share a separate


cylinder for overflow records. The advantage of independent overflow area is that several
cylinders may share a single overflow region so that the amount of overflow space that needs
to be reserved per cylinder can be less. The diagrammatic view is as follows:

***

RADIX SORT

Radix sort is one of the sorting algorithms used to sort a list of integer numbers in
order. In radix sort algorithm, a list of integer numbers will be sorted based on the digits of
individual numbers. Sorting is performed from least significant digit to the most significant
digit.
Radix sort algorithm requires the number of passes which are equal to the number of
digits present in the largest number among the list of numbers. For example, if the largest
number is a 3 digit number then that list is sorted with 3 passes.
To implement radix sort consider base of radix value as r. In general base value is
taken as 10 (Decimal number). Based on the base value, maintain 10 individual buckets with
index values from 1 to 9 to store sorted values temporarily at each pass.

The Radix sort algorithm is performed using the following steps as:
Visvodaya Technical Academy Page 193

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

 Step 1 : Define 10 buckets for each digit from 0 to 9.


 Step 2 : Consider the least significant digit of each number in the list
which is to be sorted. Insert each number into their respective bucket queue based on
the least significant digit.
 Step 3 : Group all the numbers from bucket 0 to queue 9 in the order
they have inserted into their respective buckets.
 Step 4 : Repeat from step 2 based on the next least significant digit.
 Step 5 : Terminate from the procedure when most significant bit process
completed and all elements are placed in sorted order.

Example: Sort the following elements

179 208 306 93 859 984 55 9 271 33

Solution:

Given K[1:10] = 179 208 306 93 859 984 55 9 271 33


Buckets are treated as : 0 1 2 3 4 5 6 7 8 9
Largest Number : 984 which consists 3 digits.
Hence, it requires 3 passes to complete the sort process.

Pass 1: 0:
1: 271
2:
3: 93 33
4: 984
5: 55
6: 306
7:
8: 208
9: 179 859 9

Then K[1:10] = 271 93 33 984 55 306 208 179 859 9

Pass 2: 0: 306 208 9


1:
2:
3: 33
4:
5: 55 859
6:
7: 271 179
8: 984
9: 93

Then K[1:10] = 306 208 9 33 55 859 271 179 984 93

Visvodaya Technical Academy Page 194

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Pass 3: 0: 9 33 55 93
1: 179
2: 208 271
3: 306
4:
5:
6:
7:
8: 859
9: 984

Then K[1:10] = 9 33 55 93 179 208 271 306 859 954

Therefore,
Sorted list of elements = 9 33 55 93 179 208 271 306 859 954

Radix sort is a non-comparative sorting algorithm. It avoids comparison by creating


and distributing elements into buckets according to their radix. For elements with more than
one significant digit, this bucketing process is repeated for each digit, while preserving the
ordering of the prior step, until all digits have been considered. For this reason, radix
sort has also been called bucket sort and digital sort.

The functional implementation of radix sort is as follows:

int largest(int a[100], int n)


{
int large = a[0], i;
for(i = 1; i < n; i++)
{
if(large < a[i])
large = a[i];
}
return large;
}

void RadixSort(int a[100], int n)


{
int bucket[10][10], bucket_count[10];
int i, j, k, remainder, NOP=0, divisor=1, large, pass;

large = largest(a, n);


printf("The large element %d\n",large);
while(large > 0)
{
NOP++;
large/=10;
}

Visvodaya Technical Academy Page 195

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

for(pass = 0; pass < NOP; pass++)


{
for(i = 0; i < 10; i++)
{
bucket_count[i] = 0;
}
for(i = 0; i < n; i++)
{
remainder = (a[i] / divisor) % 10;
bucket[remainder][bucket_count[remainder]] = a[i];
bucket_count[remainder] += 1;
}

i = 0;
for(k = 0; k < 10; k++)
{
for(j = 0; j < bucket_count[k]; j++)
{
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;

for(i = 0; i < n; i++)


printf("%d ",a[i]);
printf("\n");
}
}

Analysis:

Radix sort makes d number of passes over the data, each pass taking O(n+r) time.
The value of d will depend on the choice of the radix r and also on the largest key. Hence, the
total computing time is O(d (n+r)) .

Visvodaya Technical Academy Page 196

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

LIST AND TABLE SORTS

A) LIST SORT

Apart from radix sort and merge sort, all the sorting methods require excessive data
movement. As result of comparisons, records may be physically moved. When number of
records is too large, it degrades the performance of sorting procedure. Thus, with large
records, it is necessary to modify the sorting methods so as to minimize data movement.

Methods such as insertion sort and iterative merge sort can be modified to work with
a linked list rather than a sequential list. In this case each record will require an additional
link field. So that simply changes the link fields to reflect the change in the position of the
record in the list. At the end of the sorting process, the records are linked together in the
required order. This modified sorted process is known as list sort.

Consider a list of records as R1 , R2 , - - - , Rn with the corresponding link fields. In


list sort, initially set a pointer variable as first that points to smallest element of the list. Then
interchange records R1 and Rfirst. Now, the record in the position R1 has the smallest key. The
same procedure is repeated for the records R2 to Rn. At the end of the n-1th pass, all the
records are available in sorted order.

Example: Consider the following list of records with the link fields as:

i R1 R2 R3 R4 R5

Key 26 5 77 1 61

Link 9 6 0 2 3

Pass-1: first = 4
Interchange R1 and R4 records, then it becomes

i R1 R2 R3 R4 R5

Key 1 5 77 26 61

Link 2 6 0 9 3

Pass-2: first = 2
Interchange R2 and R2 records, then it becomes

i R1 R2 R3 R4 R5

Key 1 5 77 26 61

Link 2 6 0 9 3

Visvodaya Technical Academy Page 197

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Pass-3: first = 4
Interchange R3 and R4 records, then it becomes

i R1 R2 R3 R4 R5

Key 1 5 26 77 61

Link 2 6 9 0 3

Pass-4: first = 5
Interchange R4 and R5 records, then it becomes

i R1 R2 R3 R4 R5

Key 1 5 26 61 77

Link 2 6 9 3 0

Therefore, the sorted list of records with link fields is:

i R1 R2 R3 R4 R5

Key 1 5 26 61 77

Link 2 6 9 3 0

Analysis:

If the list consists n records, and number of swap operations is m, then total time
complexity is O(nm).

B) TABLE SORT

The list sort technique is not well suited for quick sort and heap sort. For these sort
methods, as well as for methods suited to list sort, it is necessary to maintain an auxiliary
table as t with one entry per record. The entries in this table serve as an indirect reference to
the records. This modified sorting process is referred to as a table sort.

Consider the list of records are given in the array a, and table index values are in t. In
table sort process, when records are shifted to some other location, table entries are also
changed. At the end of the sorting process, the record with the smallest key is a[t[1]] and that
with the largest key is a[t[n]].

Visvodaya Technical Academy Page 198

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Here, the rearrangement of values followed cycle process. It involves two types such
as trivial cycle and nontrivial cycle.

 In a trivial cycle for Ri (i.e., t[i] = i), no rearrangement is required, since the condition
t[i] = i means that the record with the ith smallest key is Ri.
 In a nontrivial cycle for Ri (i.e., t[i] ≠ i), rearrangement is required to some other
locations based on the specified conditions and so on.

For this, functional implementation can be shown as:

void Tablesort( int a[100], int n, int t[100])


{
int temp, i, current, next;
for(i = 1; i < n; i++)
{
if (t[i] != i) /* nontrivial cycle */
{
temp = a[i];
current = i;
do
{
next = t[current];
a[current] = a[next];
t[current] = current;
current = next;
}
while ( t[current] != i );
a[current] = temp;
t[current] = current;
}
}
}

Example: Sort following list of records using table sort

i R1 R2 R3 R4 R5 R6 R7 R8

Key 35 14 12 42 26 50 31 18

t 3 2 8 5 7 1 4 6

Analysis:

If the list consists n records, and number of swap operations is m only in nontrivial
cycles, then total time complexity is O(nm).

Visvodaya Technical Academy Page 199

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

SUMMARY OF SORTING TECHNIQUES

Sorting refers to the arrangement of data items either in the ascending (increasing)
order or in the descending (decreasing) order. Some of the most important sorting
techniques are:
a) Bubble Sort
b) Insertion Sort
c) Selection Sort
d) Quick Sort
e) Merge Sort etc.,

Sorting techniques are classified into two types as: Internal sorting techniques and
External sorting techniques.

Sorting that performed in main memory is called internal sorting i.e., in an internal
sort, all the data is held in primary memory during the sorting process. Internal sorting
techniques are used to handle small amount of data.

Examples: Bubble Sort, Insertion Sort, Selection Sort, Quick Sort etc.

Sorting that performed with the interaction of secondary storage devices like disks or
tapes is called external sorting i.e., in an external sort, some part of the data is in primary
memory during the sorting process and the remaining data is in secondary storage devices
that doesn’t fit in the primary memory. External sorting techniques are used to handle large
volume of data.

Examples: Merge Sort, Tape sort, Disk sort etc.

Key observations:

1) When we observe several sorting methods, no one method is best under all
circumstances.
2) Internal sorting techniques are good to handle limited amount of data and external
sorting techniques are good to handle large volume of data.
3) Consider the following sorting techniques performance characteristics.

Method Worst case Average case


Insertion sort n2 n2
Heap sort nlogn nlogn
Merge sort nlogn nlogn
Quick sort n2 nlogn

Here,
 Insertion sort is good when the list is already partially ordered and consist small
amount of data.
 Merge sort has the best average case behavior, but it requires more space
compared to other sorting techniques.

Visvodaya Technical Academy Page 200

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

 Quick sort average case behavior is good, but it takes more time in case of worst
case.
 In such a way they lead to different performance characteristics based on the size
of the input.
 Finally, we can conclude that quick sort is best compared to other sorting
techniques since, its average case time complexity is O(n logn) and takes less
space compared to external sorting techniques.

EXTERNAL SORTING

Sorting that performed with the interaction of secondary storage devices like disks or
tapes is called external sorting i.e., in an external sort, some part of the data is in primary
memory during the sorting process and the remaining data is in secondary storage devices
that doesn’t fit in the primary memory. External sorting techniques are used to handle large
volume of data.

Examples: Merge Sort, Tape sort, Disk sort etc.

Assume that the list (or file) to be sorted resides on a disk. At this stage, a unit of data
is necessary to read from or write to a disk at a time referred to as block. A block generally
consists of several records. For a disk, there are three factors contributing to the read/write
time:

a) Seek time: Time taken to position the read/write heads to correct cylinder. This
will depend on the number of cylinders across which the heads have to move.
b) Latency time:Time until the right sector of the track is under the read/write head.
c) Transmission time: Time to transmit the block of data to/from the disk.

The most popular method for sorting on external storage devices is merge sort. This
method consists of two phases.

First, segments of the input list are sorted using a sorting method. These segments,
known as runs, are written onto external storage as they are generated. Second, the runs
generated in phase one are merged together using merge phases, until only one run is left.

Example: Assume that a file of 2300 records needs to be sorted. Suppose the primary is
capable of sorting a maximum size of 500 records at a time.

In this case, procedure begins by reading and sorting the first 500 records and writing
them into a merge output file. At this stage, the remaining 1800 records are in secondary
storage device. After the first merge run, another 500 records are accessed and stored in
another merge output file and so on. This processing of data into merge runs is known as the
sort phase. After completing sort phase, proceed with merge phase. Merging of the input
merge files into one or more merge output files is known as merge phase. Number of merge
phases depends on the number of merge runs.

In computer technology, merge phases are available in different forms as: Natural
Merge, Balanced Merge and Poly-phase Merge.

Visvodaya Technical Academy Page 201

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Natural Merge

A natural merge, merges a constant number of input files to one output file. Between
each merge phase, a distribution phase is required to redistribute the merge runs to the input
files for remerging.

Example: Input
File:
2300
Records

SORT PHASE

Three Merge Runs Two Merge Runs


1 – 500 Merg Merg 501 – 1000
1001 – 1500 e 1 e 2 1501 – 2000
2001 – 2300
MERGE

Three Merge Runs


Merg 1 – 1000
e 3 1001 – 2000
2001 – 2300

DISTRIBUTION PHASE

Two Merge Runs Merg Merg


One Merge Run
1 – 1000 e 1 e 2
1001 – 2000
2001 – 2300

MERGE

Two Merge Runs


Merg 1 - 2000
e 3 2001 - 2300

DISTRIBUTION PHASE

One Merge Run One Merge Run


Merg Merg
1 – 2000 2001 - 2300
e 1 e 2

MERGE

Merg One Merge Run 1 – 2300


e 3 Sorted File data

Visvodaya Technical Academy Page 202

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Balanced Merge

A balanced merge, merges a constant number of input files to same number of output
files. In addition to this, distribution phase is not required in between the merge phases.

Example: Input
File:
2300
Records

SORT PHASE

Three Merge Runs Two Merge Runs


1 – 500 Merg Merg 501 – 1000
1001 – 1500 e 1 e 2 1501 – 2000
2001 – 2300
MERGE

Two Merge Runs Merg Merg


One Merge Run
1 – 1000 1001 – 2000
e 3 e 4
2001 – 2300

MERGE

One Merge Run Merg One Merge Run


Merg
1 – 2000 e 6 2001 - 2300
e 5

MERGE

Merg One Merge Run 1 – 2300


e 7 Sorted File data

Visvodaya Technical Academy Page 203

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Poly-phase Merge

A poly-phase merge, merges a constant number of input files to one output file. As
the data in each input file are completely merged, it immediately becomes the output file and
the output file becomes as an input file for the next merge phase run.

Example:

First Merge phase Input


File:
2300
Records

SORT PHASE

Three Merge Runs Two Merge Runs


1 – 500 Merg Merg 501 – 1000
1001 – 1500 e 1 e 2 1501 – 2000
2001 – 2300
MERGE

Two Merge Runs


1 – 1000 Merg
1001 – 2000 e 3

Second Merge phase

Three Merge Runs Two Merge Runs


1 – 500 Merg Merg 1 – 1000
1001 – 1500 e 1 e 3 1001 – 2000
2001 – 2300
MERGE

One Merge Run Merg


1 – 1000 AND 2001 - 2300 e 2

Visvodaya Technical Academy Page 204

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

Third Merge Phase

One Merge Run Two Merge Runs


1-1000 AND 2001 - 2300 Merg Merg 1 – 1000
e 2 e 3 1001 – 2000

MERGE

One Merge Run 1 – 2300 Merg


Sorted File data e 1

NOTE:

1. Sort phase uses natural merge for merging process is referred as “Natural Two-way
Merge Sort”.
2. Sort phase uses balanced merge for merging process is referred as “Balanced Two-
way Merge Sort”.
3. Sort phase uses polyphase merge for merging process is referred as “Polyphase
Merge Sort”.

SORT STABILITY

Stability of a sorting method refers to the process of arranging two or more data
elements with same key in their relative input order in the output format.

Stable sorting methods : Bubble sort, Insertion sort, Merge sort etc.

Unstable sorting methods : Shell sort, Selection sort, Quick sort etc.

Example:

Input File Stable Sort Output Unstable Sort Output

365 BLUE 119 RED 119 RED


212 GREEN 212 GREEN 212 BLUE
876 WHITE 212 YELLOW 212 GREEN
212 YELLOW 212 BLUE 212 YELLOW
119 RED 365 BLUE 365 BLUE
737 PURPLE 737 PURPLE 737 PURPLE
212 BLUE 876 WHITE 876 WHITE

Visvodaya Technical Academy Page 205

Downloaded by Karan Kamble ([email protected])


lOMoARcPSD|35685630

I B.TECH – ALL BRANCHES (R19) DATA STRUCTURES 2019-20, II Semester

SORT EFFICIENCY

Sort efficiency is the process of measuring efficiency of sorting techniques in terms of


time and space complexities. In case of sorting techniques, complexities depend on the
number of comparisons and passes required to sort an unordered list.

Among all sorting techniques, quick sort is the best sorting technique. Since, its
average time complexity O(n log n) is less compared to other sorting techniques and takes
less space compared to external sorting techniques.

COMPARISON OF SORTING ALGORITHMS

SORTING TIME COMPLEXITY STABLE / METHOD TO BE


METHODS Best Worst Case Average UNSTABLE FOLLOWED
Case Case

Bubble Sort O(n2) O(n2) O(n2) Stable Exchanging Method

Insertion Sort O(n) O(n2) O(n2) Stable Insertion Method

Shell Sort O(n) O(n log2n) O(n1.5) Unstable Insertion Method

Selection Sort O(n2) O(n2) O(n2) Unstable Selection Method

Quick Sort O(n logn) O(n2) O(n logn) Unstable Partition Exchange
Method
Merge Sort O(n logn) O(n logn) O(n logn) Stable Merging Process

THE END

Visvodaya Technical Academy Page 206

Downloaded by Karan Kamble ([email protected])

You might also like