Data Structures
Data Structures
Data Structures
Sc (Computer Science)
SRI KRISHNADEVARAYA UNIVERSITY : ANANTHAPURAMU
Revised Common Framework of CBCS for Colleges in Andhra Pradesh
(A.P. State Council of Higher Education) 2016-17
II YEAR - IV SEMESTER
Paper-IV : DATA STRUCTURES
SYLLABUS:-
UNIT I
Concept of Abstract Data Types (ADTs)- Data Types, Data Structures, Storage Structures,
and File Structures, Primitive and Non-primitive Data Structures, Linear and Non-linear Data
Structures.
UNIT II
Stacks: Definition, ADT, Array and Linked representations, Implementations and Applications
Queues: Definition, ADT, Array and Linked representations, Circular Queues, Dequeues,
Priority Queues, Implementations and Applications.
UNIT III
Trees: Binary Tree, Definition, Properties, ADT, Array and Linked representations,
Implementations and Applications. Binary Search Trees (BST) – Definition, ADT,
Operations and Implementations, BST Applications. Threaded Binary Trees, Heap trees.
UNIT IV
Graphs – Graph and its Representation, Graph Traversals, Connected Components, Basic
Searching Techniques, Minimal Spanning Trees
UNIT- V
Sorting and Searching: Selection, Insertion, Bubble, Merge, Quick, Heap sort, Sequential
and Binary Searching.
B.sc [Computer Science] DATA STRUCTURES
Data Types:
Data types specify the different sizes and values that can be stored in the variable. There are two
types of data types in Java:
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float
and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 1
B.sc [Computer Science] DATA STRUCTURES
The Boolean data type is used to store only two possible values: true and false. This data type is
used for simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.
Example: Boolean one = false
The byte data type is an example of primitive data type. It isan 8-bit signed two's complement
integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and
maximum value is 127. Its default value is 0.
The byte data type is used to save memory in large arrays where the memory savings is most
required. It saves space because a byte is 4 times smaller than an integer. It can also be used in
place of "int" data type.
Example: byte a = 10, byte b = -20
The short data type is a 16-bit signed two's complement integer. Its value-range lies between -
32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its
default value is 0.
The short data type can also be used to save memory just like byte data type. A short data type is
2 times smaller than an integer.
Example: short s = 10000, short r = -5000
The int data type is a 32-bit signed two's complement integer. Its value-range lies between -
2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is -
2,147,483,648and maximum value is 2,147,483,647. Its default value is 0.
The int data type is generally used as a default data type for integral values unless if there is no
problem about memory.
Example: int a = 100000, int b = -200000
The long data type is a 64-bit two's complement integer. Its value-range lies between -
9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum
value is - 9,223,372,036,854,775,808and maximum value is 9,223,372,036,854,775,807. Its default
value is 0. The long data type is used when you need a range of values more than those provided
by int.
Example: long a = 100000L, long b = -200000L
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 2
B.sc [Computer Science] DATA STRUCTURES
The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is
unlimited. The double data type is generally used for decimal values just like float. The double
data type also should never be used for precise values, such as currency. Its default value is 0.0d.
Example: double d1 = 12.3
The char data type is a single 16-bit Unicode character. Its value-range lies between '\u0000' (or 0)
to '\uffff' (or 65,535 inclusive).The char data type is used to store characters.
Example: char letterA = 'A'
*****
Storage Structure
Computer Memory:
The computer memory holds the data and instructions needed to process raw data and produce
output. The computer memory is divided into large number of small parts known as cells. Each cell
has a unique address which varies from 0 to memory size minus one.
Computer memory is of two types: Volatile (RAM) and Non-volatile (ROM). The secondary memory
(hard disk) is referred as storage not memory.
Register Memory:
Register memory is the smallest and fastest memory in a computer. It is located in the CPU in the
form of registers. A register temporarily holds frequently used data, instructions and memory
address that can be quickly accessed by the CPU.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 3
B.sc [Computer Science] DATA STRUCTURES
Cache Memory:
It is small in size but faster than the main memory. The CPU can access it more quickly than the
primary memory. It holds the data and programs frequently used by the CPU. So if the CPU finds
the required data or instructions in cache memory it doesn't need to access the primary memory
(RAM). Thus, it speeds up the system performance.
Primary Memory:
It is a volatile memory. It means it does not store data or instructions permanently. When you
switch on the computer the data and instructions from the hard disk are stored in RAM.
CPU utilizes this data to perform the required tasks. As soon as you shut down the computer the
RAM loses all the data.
It is a non-volatile memory. It means it does not lose its data or programs that are written on it at
the time of manufacture. So it is a permanent memory that contains all important data and
instructions needed to perform important tasks like the boot process.
Secondary Memory:
The storage devices in the computer or connected to the computer are known as secondary
memory of the computer. It is non-volatile in nature so permanently stores the data even when
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 4
B.sc [Computer Science] DATA STRUCTURES
the computer is turned off. The CPU can't directly access the secondary memory. First the
secondary memory data is transferred to primary memory then CPU can access it.
The hard disk, optical disk and pen drive are some of the popular examples of secondary memory
or storage of computer.
Hard disk:
It is a rigid magnetic disc that is used to store data. It permanently stores data and is located
within a drive unit.
Optical disk:
It has a plastic coating. The data in optical disc is recorded digitally and the recorded data is read
with laser that scans its surface.
Pen drive:
It is a compact secondary storage device. It is connected to a computer through a USB port to
store or retrieve data.
Memory Units:
Memory units are used to measure and represent data. Some of the commonly used memory
units are:
1) Bit: The computer memory units start from bit. A bit is the smallest memory unit to measure
data stored in main memory and storage devices. A bit can have only one binary value out of 0
and 1.
2) Byte: It is the fundamental unit to measure data. It contains 8 bits or is equal to 8 bits. Thus a
byte can represent 2*8 or 256 values.
3) Kilobyte: A kilobyte contains 1024 bytes.
4) Megabyte: A megabyte contains 1024 kilobytes.
5) Gigabyte: A gigabyte contains 1024 megabyte.
6) Terabyte: A terabyte contains 1024 gigabytes.
*****
File Structure
What is File?
File is a collection of records related to each other. The file size is limited by the size of memory
and storage media.
The data is subdivided into records (e.g., student information).
Each record contains a number of fields (e.g., name,GPA).
One (or more) field is the key field (e.g., name).
File Organization:-
File organization ensures that records are available for processing. It is used to determine an
efficient file organization for each base relation.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 5
B.sc [Computer Science] DATA STRUCTURES
For example, if we want to retrieve employee records in alphabetical order of name. Sorting the
file by employee name is a good file organization. However, if we want to retrieve all employees
whose marks are in a certain range, a file is ordered by employee name would not be a good file
organization.
Types of File Organization:-
There are three types of organizing the file:
*****
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 6
B.sc [Computer Science] DATA STRUCTURES
Data Structure
Introduction:
Data Structure can be defined as the group of data elements which provides an efficient
way of storing and organising data in the computer so that it can be used efficiently.Some
examples of Data Structures are arrays, Linked List, Stack, Queue, etc. Data Structures are widely
used in almost every aspect of Computer Science i.e. Operating System, Compiler Design, Artifical
intelligence, Graphics and many more.
Data Structures are the main part of many computer science algorithms as they enable the
programmers to handle the data in an efficient way. It plays a vitle role in enhancing the
performance of a software or a program as the main function of the software is to store and
retrieve the user's data as fast as possible.
Basic Terminology:
Data structures are the building blocks of any program or the software. Choosing the
appropriate data structure for a program is the most difficult task for a programmer.
Data: Data can be defined as an elementary value or the collection of values, for example,
student's name and its id are the data about the student.
Group Items: Data items which have subordinate data items are called Group item, for example,
name of a student can have first name and the last name.
Record: Record can be defined as the collection of various data items, for example, if we talk
about the student entity, then its name, address, course and marks can be grouped together to
form the record for the student.
File: A File is a collection of various records of one type of entity, for example, if there are 60
employees in the class, then there will be 20 records in the related file where each record
contains the data about each employee.
Attribute and Entity: An entity represents the class of certain objects. it contains various
attributes. Each attribute represents the particular property of that entity.
Field: Field is a single elementary unit of information representing the attribute of an entity.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 7
B.sc [Computer Science] DATA STRUCTURES
Processor speed: To handle very large amout of data, high speed processing is required, but as
the data is growing day by day to the billions of files per entity, processor may fail to deal with
that much amount of data.
Data Search: Consider an inventory size of 106 items in a store, If our application needs to search
for a particular item, it needs to traverse 106 items every time, results in slowing down the search
process.
Multiple requests: If thousands of users are searching the data simultaneously on a web server,
then there are the chances that a very large server can be failed during that process
in order to solve the above problems, data structures are used. Data is organized to form a data
structure in such a way that all items are not required to be searched and required data can be
searched instantly.
Reusability: Data structures are reusable, i.e. once we have implemented a particular data
structure, we can use it at any other place. Implementation of data structures can be compiled
into libraries which can be used by different clients.
Abstraction: Data structure is specified by the ADT which provides a level of abstraction. The
client program uses the data structure through interface only, without getting into the
implementation details.
Data Structure Classification
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 8
B.sc [Computer Science] DATA STRUCTURES
Linear Data Structures: A data structure is called linear if all of its elements are arranged in the
linear order. In linear data structures, the elements are stored in non-hierarchical way where each
element has the successors and predecessors except the first and last element.
Types of Linear Data Structures are given below:
Arrays: An array is a collection of similar type of data items and each data item is called an
element of the array. The data type of the element may be any valid data type like char, int, float
or double.
The elements of array share the same variable name but each one carries a different index
number known as subscript. The array can be one dimensional, two dimensional or
multidimensional.
The individual elements of the array age are:
age[0], age[1], age[2], age[3],??? age[98], age[99].
Linked List: Linked list is a linear data structure which is used to maintain a list in the memory. It
can be seen as the collection of nodes stored at non-contiguous memory locations. Each node of
the list contains a pointer to its adjacent node.
Stack: Stack is a linear list in which insertion and deletions are allowed only at one end, called top.
A stack is an abstract data type (ADT), can be implemented in most of the programming
languages. It is named as stack because it behaves like a real-world stack, for example: - piles of
plates or deck of cards etc.
Queue: Queue is a linear list in which elements can be inserted only at one end called rear and
deleted only at the other end called front.
It is an abstract data structure, similar to stack. Queue is opened at both end therefore it follows
First-In-First-Out (FIFO) methodology for storing the data items.
Non Linear Data Structures: This data structure does not form a sequence i.e. each item or
element is connected with two or more other items in a non-linear arrangement. The data
elements are not arranged in sequential structure.
Types of Non Linear Data Structures are given below:
Trees: Trees are multilevel data structures with a hierarchical relationship among its elements
known as nodes. The bottommost nodes in the herierchy are called leaf node while the topmost
node is called root node. Each node contains pointers to point adjacent nodes.
Tree data structure is based on the parent-child relationship among the nodes. Each node in the
tree can have more than one children except the leaf nodes whereas each node can have atmost
one parent except the root node. Trees can be classfied into many categories which will be
discussed later in this tutorial.
Graphs: Graphs can be defined as the pictorial representation of the set of elements (represented
by vertices) connected by the links known as edges. A graph is different from tree in the sense
that a graph can have cycle while the tree cannot have the one.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 9
B.sc [Computer Science] DATA STRUCTURES
2) Insertion: Insertion can be defined as the process of adding the elements to the data structure
at any location.
If the size of data structure is n then we can only insert n-1 data elements into it.
3) Deletion:The process of removing an element from the data structure is called Deletion. We
can delete an element from the data structure at any random location.
If we try to delete an element from an empty data structure then underflow occurs.
4) Searching: The process of finding the location of an element within the data structure is called
Searching. There are two algorithms to perform searching, Linear Search and Binary Search. We
will discuss each one of them later in this tutorial.
5) Sorting: The process of arranging the data structure in a specific order is known as Sorting.
There are many algorithms that can be used to perform sorting, for example, insertion sort,
selection sort, bubble sort, etc.
6) Merging: When two lists List A and List B of size M and N respectively, of similar type of
elements, clubbed or joined to produce the third list, List C of size (M+N), then this process is
called merging.
Algorithm:
An algorithm is a procedure having well defined steps for solving a particular problem. Algorithm
is finite set of logic or instructions, written in order for accomplish the certain predefined task. It is
not the complete program or code, it is just a solution (logic) of a problem, which can be
represented either as an informal description using a Flowchart or Pseudo code.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 10
B.sc [Computer Science] DATA STRUCTURES
Example:
Design an algorithm to multiply the two numbers x and y and display the result in z.
o Step 1 START
o Step 2 declare three integers x, y & z
o Step 3 define values of x & y
o Step 4 multiply values of x & y
o Step 5 store the output of step 4 in z
o Step 6 print z
o Step 7 STOP
Characteristics of an Algorithm
An algorithm must follow the mentioned below characteristics:
*****
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 11
B.sc [Computer Science] DATA STRUCTURES
ARRAY
Definition:
o Arrays are defined as the collection of similar type of data items stored at contiguous
memory locations.
o Arrays are the derived data type in C programming language which can store the primitive
type of data such as int, char, double, float, etc.
o Array is the simplest data structure where each data element can be randomly accessed by
using its index number.
o For example, if we want to store the marks of a student in 6 subjects, then we don't need
to define different variable for the marks in different subject. instead of that, we can
define an array which can store the marks in each subject at a the contiguous memory
locations.
The array marks[10] defines the marks of the student in 10 different subjects where each subject
marks are located at a particular subscript in the array i.e. marks[0] denotes the marks in first
subject, marks[1] denotes the marks in 2nd subject and so on.
1. Each element is of same data type and carries a same size i.e. int = 4 bytes.
2. Elements of the array are stored at contiguous memory locations where the first element
is stored at the smallest memory location.
3. Elements of the array can be randomly accessed since we can calculate the address of
each element of the array with the given base address and the size of data element.
for example, in C language, the syntax of declaring an array is like following:
int arr[10]; char arr[10]; float arr[5]
Advantages of Array:
o Array provides the single name for the group of variables of the same type therefore, it is
easy to remember the name of all the elements of an array.
o Traversing an array is a very simple process, we just need to increment the base address of
the array in order to visit each element one by one.
o Any element in the array can be directly accessed by using the index.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 12
B.sc [Computer Science] DATA STRUCTURES
1. 0 (zero - based indexing) : The first element of the array will be arr[0].
2. 1 (one - based indexing) : The first element of the array will be arr[1].
3. n (n - based indexing) : The first element of the array can reside at any random index
number.
In the following image, we have shown the memory allocation of an array arr of size 5. The
array follows 0-based indexing approach. The base address of the array is 100th byte. This will be
the address of arr[0]. Here, the size of int is 4 bytes therefore each element will take 4 bytes in the
memory.
In 0 based indexing, If the size of an array is n then the maximum index number, an element can
have is n-1. However, it will be n if we use 1 based indexing.
2D Array:
2D array can be defined as an array of arrays. The 2D array is organized as matrices which can be
represented as the collection of rows and columns.
However, 2D arrays are created to implement a relational database look alike data structure. It
provides ease of holding bulk of data at once which can be passed to any number of functions
wherever required.
1. int arr[max_rows][max_columns];
however, It produces the data structure which looks like following.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 13
B.sc [Computer Science] DATA STRUCTURES
Above image shows the two dimensional array, the elements are organized in the form of
rows and columns. First element of the first row is represented by a[0][0] where the number
shown in the first index is the number of that row while the number shown in the second index is
the number of the column.
Initializing 2D Arrays
We know that, when we declare and initialize one dimensional array in C programming
simultaneously, we don't need to specify the size of the array. However this will not work with 2D
arrays. We will have to define at least the second dimension of the array.
The syntax to declare and initialize the 2D array is given as follows.
int arr[2][2] = {0,1,2,3};
The number of elements that can be present in a 2D array will always be equal to (number of
rows * number of columns).
Java Example:
import java.util.Scanner;
publicclass TwoDArray {
publicstaticvoid main(String[] args) {
int[][] arr = newint[3][3];
Scanner sc = new Scanner(System.in);
for (inti =0;i<3;i++)
{
for(intj=0;j<3;j++)
{
System.out.print("Enter Element");
arr[i][j]=sc.nextInt();
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 14
B.sc [Computer Science] DATA STRUCTURES
System.out.println();
}
}
System.out.println("Printing Elements...");
for(inti=0;i<3;i++)
{
System.out.println();
for(intj=0;j<3;j++)
{
System.out.print(arr[i][j]+"\t");
}
}
}
}
The size of a two dimensional array is equal to the multiplication of number of rows and the
number of columns present in the array. We do need to map two dimensional array to the one
dimensional array in order to store them in the memory.
A 3 X 3 two dimensional array is shown in the following image. However, this array needs to be
mapped to a one dimensional array in order to store it into the memory.
There are two main techniques of storing 2D array elements into memory
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 15
B.sc [Computer Science] DATA STRUCTURES
first, the 1st row of the array is stored into the memory completely, then the 2 nd row of the array is
stored into the memory completely and so on till the last row.
first, the 1st column of the array is stored into the memory completely, then the 2nd row of the
array is stored into the memory completely and so on till the last column of the array.
Sparse Matrix and its representations | Set 1 (Using Arrays and Linked Lists):
A matrix is a two-dimensional data object made of m rows and n columns, therefore having total
m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 16
B.sc [Computer Science] DATA STRUCTURES
Example:
00304
00570
00000
02600
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the
matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we
only store non-zero elements. This means storing non-zero elements with triples- (Row, Column,
value).
Sparse Matrix Representations can be done in many ways following are two common
representations:
1. Array representation
2. Linked list representation
*****
LINKED LIST
o Linked List can be defined as collection of objects called nodes that are randomly stored in
the memory.
o A node contains two fields i.e. data stored at that particular address and the pointer which
contains the address of the next node in the memory.
o The last node of the list contains pointer to the null.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 17
B.sc [Computer Science] DATA STRUCTURES
Linked list is the data structure which can overcome all the limitations of an array. Using linked list
is useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously
stored in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available memory
space.
One way chain or singly linked list can be traversed only in one direction. In other words, we can
say that each node contains only next pointer, therefore we can not traverse the list in the reverse
direction.
Consider an example where the marks obtained by the student in three subjects are stored in a
linked list as shown in the figure.
In the above figure, the arrow represents the links. The data part of every node contains the
marks obtained by the student in the different subject. The last node in the list is identified by the
null pointer which is present in the address part of the last node. We can have as many elements
we require, in the data part of the list.
There are various operations which can be performed on singly linked list. A list of all such
operations is given below.
Insertion:
The insertion into a singly linked list can be performed at different positions. Based on the
position of the new node being inserted, the insertion is categorized into the following categories.
SN Operation Description
1 Insertion at It involves inserting any element at the front of the list.
beginning We just need to a few link adjustments to make the new
node as the head of the list.
2 Insertion at It involves insertion at the last of the linked list. The new
end of the list node can be inserted as the only node in the list or it can
be inserted as the last one. Different logics are
implemented in each scenario.
3 Insertion after It involves insertion after the specified node of the linked
specified node list. We need to skip the desired number of nodes in
order to reach the node after which the new node will be
inserted. .
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 19
B.sc [Computer Science] DATA STRUCTURES
4 Traversing In traversing, we simply visit each node of the list at least once
in order to perform some specific operation on it, for example,
printing data part of each node present in the list.
5 Searching In searching, we match each element of the list with the given
element. If the element is found on any of the location then
location of that element is returned otherwise null is returned.
.
A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown
in the following image.
The prev part of the first node and the next part of the last node will always contain null
indicating end in each direction.
In a singly linked list, we could traverse only in one direction, because each node contains address
of the next node and it doesn't have any record of its previous nodes. However, doubly linked list
overcome this limitation of singly linked list. Due to the fact that, each node of the list contains
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 20
B.sc [Computer Science] DATA STRUCTURES
the address of its previous node, we can find all the details about the previous node as well by
using the previous address stored inside the previous part of each node.
In the above image, the first element of the list that is i.e. 13 stored at address 1. The head
pointer points to the starting address 1. Since this is the first element being added to the list
therefore the prev of the list contains null. The next node of the list resides at address 4 therefore
the first node contains 4 in its next pointer.
We can traverse the list in this way until we find any node containing null or -1 in its next part.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 21
B.sc [Computer Science] DATA STRUCTURES
We traverse a circular singly linked list until we reach the same node where we started. The
circular singly liked list has no beginning and no ending. There is no null value present in the next
part of any of the nodes.
Circular linked list are mostly used in task maintenance in operating systems. There are many
examples where circular linked list are being used in computer science including browser surfing
where a record of pages visited in the past by the user, is maintained in the form of circular linked
lists and can be accessed again on clicking the previous button.
1 Deletion at Removing the node from circular singly linked list at the
beginning beginning.
2 Deletion at Removing the node from circular singly linked list at the end.
the end
3 Searching Compare each element of the node with the given item and
return the location at which the item is present in the list
otherwise return null.
4 Traversing Visiting each element of the list at least once in order to
perform some specific operation.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 22
B.sc [Computer Science] DATA STRUCTURES
Due to the fact that a circular doubly linked list contains three parts in its structure therefore, it
demands more space per node and more expensive basic operations. However, a circular doubly
linked list provides easy manipulation of the pointers and the searching becomes twice as
efficient.
SN Operation Description
Traversing and searching in circular doubly linked list is similar to that in the circular singly linked
list.
*****
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 23
B.sc [Computer Science] DATA STRUCTURES
STACK
1. Stack is an ordered list in which, insertion and deletion can be performed only at one end
that is called top.
2. Stack is a recursive data structure having pointer to its top element.
3. Stacks are sometimes called as Last-In-First-Out (LIFO) lists i.e. the element which is
inserted first in the stack, will be deleted last from the stack.
Applications of Stack:
1. Recursion
2. Expression evaluations and conversions
3. Parsing
4. Browsers
5. Editors
6. Tree Traversals
Operations on Stack:
There are various operations which can be performed on stack.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 24
B.sc [Computer Science] DATA STRUCTURES
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 25
B.sc [Computer Science] DATA STRUCTURES
Stack is overflown when we try to insert an element into a completely filled stack therefore, our
main function must always avoid stack overflow condition.
Algorithm:
1. begin
2. if top = n then stack full
3. top = top + 1
4. stack (top) : = item;
5. end
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 26
B.sc [Computer Science] DATA STRUCTURES
The underflow condition occurs when we try to delete an element from an already empty stack.
Algorithm :
1. begin
2. if top = 0 then stack empty;
3. item := stack(top);
4. top = top ? 1;
5. end;
Java Program:
import java.util.Scanner;
class Stack
{
int top;
int maxsize = 10;
int[] arr = new int[maxsize];
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 27
B.sc [Computer Science] DATA STRUCTURES
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 28
B.sc [Computer Science] DATA STRUCTURES
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 29
B.sc [Computer Science] DATA STRUCTURES
Instead of using array, we can also use linked list to implement stack. Linked list allocates
the memory dynamically. However, time complexity in both the scenario is same for all the
operations i.e. push, pop and peek.
In linked list implementation of stack, the nodes are maintained non-contiguously in the
memory. Each node contains a pointer to its immediate successor node in the stack. Stack is said
to be overflown if the space left in the memory heap is not enough to create a node.
The top most node in the stack always contains null in its address field. Lets discuss the way in
which, each operation is performed in linked list implementation of stack.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 30
B.sc [Computer Science] DATA STRUCTURES
C -implementation:
1. void push ()
2. {
3. int val;
4. struct node *ptr =(struct node*)malloc(sizeof(struct node));
5. if(ptr == NULL)
6. {
7. printf("not able to push the element");
8. }
9. else
10. {
11. printf("Enter the value");
12. scanf("%d",&val);
13. if(head==NULL)
14. {
15. ptr->val = val;
16. ptr -> next = NULL;
17. head=ptr;
18. }
19. else
20. {
21. ptr->val = val;
22. ptr->next = head;
23. head=ptr;
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 31
B.sc [Computer Science] DATA STRUCTURES
24.
25. }
26. printf("Item pushed");
27.
28. }
29. }
Check for the underflow condition: The underflow condition occurs when we try to pop from an
already empty stack. The stack will be empty if the head pointer of the list points to null.
Adjust the head pointer accordingly: In stack, the elements are popped only from one end,
therefore, the value stored in the head pointer must be deleted and the node must be freed. The
next node of the head node now becomes the head node.
C-Implementation
1. void pop()
2. {
3. int item;
4. struct node *ptr;
5. if (head == NULL)
6. {
7. printf("Underflow");
8. }
9. else
10. {
11. item = head->val;
12. ptr = head;
13. head = head->next;
14. free(ptr);
15. printf("Item popped");
16.
17. }
18. }
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 32
B.sc [Computer Science] DATA STRUCTURES
Move the temporary pointer through all the nodes of the list and print the value field attached to
every node.
C - Implementation
1. void display()
2. {
3. int i;
4. struct node *ptr;
5. ptr=head;
6. if(ptr == NULL)
7. {
8. printf("Stack is empty\n");
9. }
10. else
11. {
12. printf("Printing Stack elements \n");
13. while(ptr!=NULL)
14. {
15. printf("%d\n",ptr->val);
16. ptr = ptr->next;
17. }
18. }
19. }
INFIX:-
An infix expression is a single letter, or an operator, proceeded by one infix string and followed by
another infix string.
A
A+B
(A + B) + (C – D)
PREFIX:-
A prefix expression is a single letter, or an operator, followed by two prefix strings. Every prefix
string longer than a single variable contains an operator, first operand and second operand
A
+AB
++AB–CD
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 33
B.sc [Computer Science] DATA STRUCTURES
POSTFIX:-
A postfix expression (also called Reverse Polish Notation) is a single letter or an operator,
preceded by two postfix strings. Every postfix string longer than a single variable contains first and
second operands followed by an operator.
A
AB+
AB+CD–
Prefix and postfix notations are methods of writing mathematical expressions without
parenthesis. Time to evaluate a postfix and prefix expression is O(n), where n is the number of
elements in the array.
INFIX PREFIX POSTFIX
(A + B) * C – D –*+ABCD AB+C*D–
THE ALGORITHM:-
Procedure for Postfix Conversion:
1. Scan the Infix string from left to right.
2. Initialize an empty stack.
3. If the scanned character is an operand, add it to the Postfix string.
4. If the scanned character is an operator and if the stack is empty push the character to stack.
If the scanned character is an Operator and the stack is not empty, compare the precedence of
5.
the character with the element on top of the stack.
If top Stack has higher precedence over the scanned character pop the stack else push the
6. scanned character to stack. Repeat this step until the stack is not empty and top Stack has
precedence over the character.
7. Repeat 4 and 5 steps till all the characters are scanned.
After all characters are scanned, we have to add any character that the stack may have to the
8.
Postfix string.
9. If stack is not empty add top Stack to Postfix string and Pop the stack.
10. Repeat this step as long as stack is not empty.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 34
B.sc [Computer Science] DATA STRUCTURES
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 35
B.sc [Computer Science] DATA STRUCTURES
QUEUE
1. A queue can be defined as an ordered list which enables insert operations to be performed at
one end called REAR and delete operations to be performed at another end called FRONT.
2. Queue is referred to be as First In First Out list.
3. For example, people waiting in line for a rail ticket form a queue.
Applications of Queue:
Due to the fact that queue performs actions on first in first out basis which is quite fair for the
ordering of actions. There are various applications of queues discussed as below.
1. Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.
2. Queues are used in asynchronous transfer of data (where data is not being transferred at
the same rate between two processes) for eg. pipes, file IO, sockets.
3. Queues are used as buffers in most of the applications like MP3 media player, CD player,
etc.
4. Queue are used to maintain the play list in media players in order to add and remove the
songs from the play-list.
5. Queues are used in operating systems for handling interrupts.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 36
B.sc [Computer Science] DATA STRUCTURES
The above figure shows the queue of characters forming the English word "HELLO". Since,
No deletion is performed in the queue till now, therefore the value of front remains -1 . However,
the value of rear increases by one every time an insertion is performed in the queue. After
inserting an element into the queue shown in the above figure, the queue will look something like
following. The value of rear will become 5 while the value of front remains same.
After deleting an element, the value of front will increase from -1 to 0. however, the queue will
look something like following.
Algorithm:
o Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
o Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 37
B.sc [Computer Science] DATA STRUCTURES
Algorithm:
o Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
o Step 2: EXIT
The storage requirement of linked representation of a queue with n elements is o(n) while
the time requirement for operations is o(1).
In a linked queue, each node of the queue consists of two parts i.e. data part and the link
part. Each element of the queue points to its immediate next element in the memory.
In the linked queue, there are two pointers maintained in the memory i.e. front pointer
and rear pointer. The front pointer contains the address of the starting element of the queue
while the rear pointer contains the address of the last element of the queue.
Insertion and deletions are performed at rear and front end respectively. If front and rear
both are NULL, it indicates that the queue is empty.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 38
B.sc [Computer Science] DATA STRUCTURES
Insert operation
The insert operation append the queue by adding an element to the end of the queue. The new
element will be the last element of the queue.
Algorithm:
o Step 1: Allocate the space for the new node PTR
o Step 2: SET PTR -> DATA = VAL
o Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
o Step 4: END
Deletion
Deletion operation removes the element that is first inserted among all the queue elements.
Firstly, we need to check either the list is empty or not. The condition front == NULL becomes true
if the list is empty, in this case , we simply write underflow on the console and make exit.
Otherwise, we will delete the element that is pointed by the pointer front. For this purpose, copy
the node pointed by the front pointer into the pointer ptr. Now, shift the front pointer, point to its
next node and free the node pointed by the node ptr. This is done by using the following
statements.
1. ptr = front;
2. front = front -> next;
3. free(ptr);
The algorithm and C function is given as follows.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 39
B.sc [Computer Science] DATA STRUCTURES
Algorithm:
o Step 1: IF FRONT = NULL
Write " Underflow "
Go to Step 5
[END OF IF]
o Step 2: SET PTR = FRONT
o Step 3: SET FRONT = FRONT -> NEXT
o Step 4: FREE PTR
o Step 5: END
Circular Queue:
Deletions and insertions can only be performed at front and rear end respectively, as far as linear
queue is concerned.
Consider the queue shown in the following figure.
The Queue shown in above figure is completely filled and there can't be inserted any more
element due to the condition rear == max - 1 becomes true.
However, if we delete 2 elements at the front end of the queue, we still can not insert any
element since the condition rear = max -1 still holds.
This is the main problem with the linear queue, although we have space available in the array, but
we cannot insert any more element in the queue. This is simply the memory wastage and we need
to overcome this problem.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 40
B.sc [Computer Science] DATA STRUCTURES
One of the solutions of this problem is circular queue. In the circular queue, the first index
comes right after the last index. You can think of a circular queue as shown in the following figure.
Circular queue will be full when front = -1 and rear = max-1. Implementation of circular queue is
similar to that of a linear queue. Only the logic part that is implemented in the case of insertion
and deletion is different from that in a linear queue.
2. If there is only one element in the queue, in this case, the condition rear = front holds and
therefore, both are set to -1 and the queue is deleted completely.
3. If front = max -1 then, the value is deleted from the front end the value of front is set to 0.
4. Otherwise, the value of front is incremented by 1 and then delete the element at the front
end.
Algorithm:
o Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]
o Step 2: SET VAL = QUEUE[FRONT]
o Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]
o Step 4: EXIT
Deque:
Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert
and delete at both ends.
Operations on Deque:
Mainly the following four basic operations are performed on queue:
insertFront(): Adds an item at the front of Deque.
insertLast(): Adds an item at the rear of Deque.
deleteFront(): Deletes an item from front of Deque.
deleteLast(): Deletes an item from rear of Deque.
In addition to above operations, following operations are also supported
getFront(): Gets the front item from queue.
getRear(): Gets the last item from queue.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.
Applications of Deque:
Since Deque supports both stack and queue operations, it can be used as both. The Deque data
structure supports clockwise and anticlockwise rotations in O(1) time which can be useful in
certain applications. Also, the problems where elements need to be removed and or added both
ends can be efficiently solved using Deque.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 42
B.sc [Computer Science] DATA STRUCTURES
Implementation:
A Deque can be implemented either using a doubly linked list or circular array. In both
implementation, we can implement all operations in O(1) time.
Priority Queue :
Priority Queue is an extension of queue with following properties.
1. Every item has a priority associated with it.
2. An element with high priority is dequeued before an element with low priority.
3. If two elements have the same priority, they are served according to their order in the
queue.
In the below priority queue, element with maximum ASCII value will have the highest priority.
TREE
o A Tree is a recursive data structure containing the set of one or more data nodes where
one node is designated as the root of the tree while the remaining nodes are called as the
children of the root.
o The nodes other than the root node are partitioned into the non empty sets where each
one of them is to be called sub-tree.
o Nodes of a tree either maintain a parent-child relationship between them or they are sister
nodes.
o In a general tree, A node can have any number of children nodes but it can have only a
single parent.
o The following image shows a tree, where the node A is the root node of the tree while the
other nodes can be seen as the children of A.
Basic terminology:
o Root Node :- The root node is the topmost node in the tree hierarchy. In other words, the
root node is the one which doesn't have any parent.
o Sub Tree :- If the root node is not null, the tree T1, T2 and T3 is called sub-trees of the root
node.
o Leaf Node :- The node of tree, which doesn't have any child node, is called leaf node. Leaf
node is the bottom most node of the tree. There can be any number of leaf nodes present
in a general tree. Leaf nodes can also be called external nodes.
o Path :- The sequence of consecutive edges is called path. In the tree shown in the above
image, path to the node E is A→ B → E.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 44
B.sc [Computer Science] DATA STRUCTURES
o Ancestor node :- An ancestor of a node is any predecessor node on a path from root to
that node. The root node doesn't have any ancestors. In the tree shown in the above
image, the node F have the ancestors, B and A.
o Degree :- Degree of a node is equal to number of children, a node have. In the tree shown
in the above image, the degree of node B is 2. Degree of a leaf node is always 0 while in a
complete binary tree, degree of each node is equal to 2.
o Level Number :- Each node of the tree is assigned a level number in such a way that each
node is present at one level higher than its parent. Root node of the tree is always present
at level 0.
Types of Tree:
The tree data structure can be classified into six different categories.
General Tree:
General Tree stores the elements in a hierarchical order in which the top level element is
always present at level 0 as the root element. All the nodes except the root node are present at
number of levels. The nodes which are present on the same level are called siblings while the
nodes which are present on the different levels exhibit the parent-child relationship among them.
A node may contain any number of sub-trees. The tree in which each node contain 3 sub-tree, is
called ternary tree.
Forests:
Forest can be defined as the set of disjoint trees which can be obtained by deleting the root node
and the edges which connects root node to the first level node.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 45
B.sc [Computer Science] DATA STRUCTURES
Binary Tree:
Binary tree is a data structure in which each node can have at most 2 children. The node present
at the top most level is called the root node. A node with the 0 children is called leaf node. Binary
Trees are used in the applications like expression evaluation and many more. We will discuss
binary tree in detail, later in this tutorial.
Expression Tree:
Expression trees are used to evaluate the simple arithmetic expressions. Expression tree is
basically a binary tree where internal nodes are represented by operators while the leaf nodes are
represented by operands. Expression trees are widely used to solve algebraic expressions like
(a+b)*(a-b). Consider the following example.
(a + b) / (a*b - c) + d
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 46
B.sc [Computer Science] DATA STRUCTURES
Binary Tree:
Binary Tree is a special type of generic tree in which, each node can have at most two children.
Binary tree is generally partitioned into three disjoint subsets.
1. Root of the node
2. left sub-tree which is also a binary tree.
3. Right binary sub-tree
A binary Tree is shown in the following image.
In Strictly Binary Tree, every non-leaf node contain non-empty left and right sub-trees. In other
words, the degree of every non-leaf node will always be 2. A strictly binary tree with n leaves, will
have (2n - 1) nodes.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 47
B.sc [Computer Science] DATA STRUCTURES
A Binary Tree is said to be a complete binary tree if all of the leaves are located at the same level
d. A complete binary tree is a binary tree that contains exactly 2^l nodes at each level between
level 0 and d. The total number of nodes in a complete binary tree with depth d is 2 d+1-1 where
leaf nodes are 2d while non-leaf nodes are 2d-1.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 48
B.sc [Computer Science] DATA STRUCTURES
1. Linked Representation
In this representation, the binary tree is stored in the memory, in the form of a linked list
where the number of nodes are stored at non-contiguous memory locations and linked together
by inheriting parent child relationship like a tree. every node contains three parts : pointer to the
left node, data element and pointer to the right node. Each binary tree has a root pointer which
points to the root node of the binary tree. In an empty binary tree, the root pointer will point to
null.
In the above figure, a tree is seen as the collection of nodes where each node contains
three parts : left pointer, data element and right pointer. Left pointer stores the address of the left
child while the right pointer stores the address of the right child. The leaf node contains null in its
left and right pointers.
The following image shows about how the memory will be allocated for the binary tree by
using linked representation. There is a special pointer maintained in the memory which points to
the root node of the tree. Every node in the tree contains the address of its left and right child.
Leaf node contains null in its left and right pointers.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 49
B.sc [Computer Science] DATA STRUCTURES
2. Sequential Representation
This is the simplest memory allocation technique to store the tree elements but it is an
inefficient technique since it requires a lot of space to store the tree elements. A binary tree is
shown in the following figure along with its memory allocation.
In this representation, an array is used to store the tree elements. Size of the array will be equal to
the number of nodes present in the tree. The root node of the tree will be present at the 1 st index
of the array. If a node is stored at ith index then its left and right children will be stored at 2i and
2i+1 location. If the 1st index of the array i.e. tree[1] is 0, it means that the tree is empty.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 50
B.sc [Computer Science] DATA STRUCTURES
A Binary search tree is shown in the above figure. As the constraint applied on the BST, we can see
that the root node 30 doesn't contain any value greater than or equal to 30 in its left sub-tree and
it also doesn't contain any value less than 30 in its right sub-tree.
Q. Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50
1. Insert 43 into the tree as the root of the tree.
2. Read the next element, if it is lesser than the root node element, insert it as the root of the
left sub-tree.
3. Otherwise, insert it as the root of the right of the right sub-tree.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 51
B.sc [Computer Science] DATA STRUCTURES
The process of creating BST by using the given elements, is shown in the image below.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 52
B.sc [Computer Science] DATA STRUCTURES
Heap Tree:
Heap is a special case of balanced binary tree data structure where the root-node key is
compared with its children and arranged accordingly. If α has child node β then −
key(α) ≥ key(β)
As the value of parent is greater than that of child, this property generates Max Heap. Based on
this criteria, a heap can be of two types –
For Input → 35 33 42 10 14 19 27 44 26 31
Min-Heap − Where the value of the root node is less than or equal to either of its children.
Max-Heap − Where the value of the root node is greater than or equal to either of its children.
Both trees are constructed using the same input and order of arrival.
*****
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 53
B.sc [Computer Science] DATA STRUCTURES
GRAPH
A graph can be defined as group of vertices and edges that are used to connect these
vertices. A graph can be seen as a cyclic tree, where the vertices (Nodes) maintain any complex
relationship among them instead of having parent child relationship.
Definition:
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of vertices and
E(G) represents the set of edges which are used to connect these vertices.
A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D), (D,B), (D,A)) is
shown in the following figure.
In a directed graph, edges form an ordered pair. Edges represent a specific path from some
vertex A to another vertex B. Node A is called initial node while node B is called terminal node.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 54
B.sc [Computer Science] DATA STRUCTURES
Graph Terminology:
Path:- A path can be defined as the sequence of nodes that are followed in order to reach some
terminal node V from the initial node U.
Closed Path:- A path will be called as closed path if the initial node is same as terminal node. A path
will be closed path if V0=VN.
Simple Path:- If all the nodes of the graph are distinct with an exception V0=VN, then such path P is
called as closed simple path.
Cycle:- A cycle can be defined as the path which has no repeated edges or vertices except the first
and last vertices.
Connected Graph:- A connected graph is the one in which some path exists between every two
vertices (u, v) in V. There are no isolated nodes in connected graph.
Complete Graph:- A complete graph is the one in which every node is connected with all other
nodes. A complete graph contain n(n-1)/2 edges where n is the number of nodes in the graph.
Weighted Graph:- In a weighted graph, each edge is assigned with some data such as length or
weight. The weight of an edge e can be given as w(e) which must be a positive (+) value indicating the
cost of traversing the edge.
Digraph:- A digraph is a directed graph in which each edge of the graph is associated with some
direction and the traversing can be done only in the specified direction.
Loop:- An edge that is associated with the similar end points can be called as Loop.
Adjacent Nodes:- If two nodes u and v are connected via an edge e, then the nodes u and v are
called as neighbours or adjacent nodes.
Degree of the Node:- A degree of a node is the number of edges that are connected with that
node. A node with degree 0 is called as isolated node.
----------------
Graph Representation:
By Graph representation, we simply mean the technique which is to be used in order to store
some graph into the computer's memory.
There are two ways to store Graph into the computer's memory. In this part of this tutorial, we
discuss each one of them in detail.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 55
B.sc [Computer Science] DATA STRUCTURES
1. Sequential Representation:
In sequential representation, we use adjacency matrix to store the mapping represented by
vertices and edges. In adjacency matrix, the rows and columns are represented by the graph
vertices. A graph having n vertices, will have a dimension n x n.
An entry Mij in the adjacency matrix representation of an undirected graph G will be 1 if there
exists an edge between Vi and Vj.
An undirected graph and its adjacency matrix representation is shown in the following figure.
In the above figure, we can see the mapping among the vertices (A, B, C, D, E) is represented by
using the adjacency matrix which is also shown in the figure.
There exists different adjacency matrices for the directed and undirected graph. In directed graph,
an entry Aij will be 1 only when there is an edge directed from Vi to Vj.
A directed graph and its adjacency matrix representation is shown in the following figure.
Representation of weighted directed graph is different. Instead of filling the entry by 1, the Non-
zero entries of the adjacency matrix are represented by the weight of respective edges.
The weighted directed graph along with the adjacency matrix representation is shown in the
following figure.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 56
B.sc [Computer Science] DATA STRUCTURES
Linked Representation:
In the linked representation, an adjacency list is used to store the Graph into the computer's
memory.
Consider the undirected graph shown in the following figure and check the adjacency list
representation.
An adjacency list is maintained for each node present in the graph which stores the node
value and a pointer to the next adjacent node to the respective node. If all the adjacent nodes are
traversed then store the NULL in the pointer field of last node of the list. The sum of the lengths of
adjacency lists is equal to the twice of the number of edges present in an undirected graph.
Consider the directed graph shown in the following figure and check the adjacency list
representation of the graph.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 57
B.sc [Computer Science] DATA STRUCTURES
In a directed graph, the sum of lengths of all the adjacency lists is equal to the number of edges
present in the graph.
In the case of weighted directed graph, each node contains an extra field that is called the weight
of the node. The adjacency list representation of a directed graph is shown in the following figure.
Breadth first search is a graph traversal algorithm that starts traversing the graph from root node
and explores all the neighbouring nodes. Then, it selects the nearest node and explore all the
unexplored nodes. The algorithm follows the same process for each of the nearest node until it
finds the goal.
The algorithm of breadth first search is given below. The algorithm starts with examining the node
A and all of its neighbours. In the next step, the neighbours of the nearest node of A are explored
and process continues in the further steps. The algorithm explores all neighbours of all the nodes
and ensures that each node is visited exactly once and no node is visited twice.
Algorithm:
o Step 1: SET STATUS = 1 (ready state)
for each node in G
o Step 2: Enqueue the starting node A
and set its STATUS = 2
(waiting state)
o Step 3: Repeat Steps 4 and 5 until
QUEUE is empty
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 58
B.sc [Computer Science] DATA STRUCTURES
Example:-
Consider the graph G shown in the following image, calculate the minimum path p from node A to
node E. Given that each edge has a length of 1.
Solution:
Minimum Path P can be found by applying breadth first search algorithm that will begin at node A
and will end at E. the algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 holds all
the nodes that are to be processed while QUEUE2 holds all the nodes that are processed and
deleted from QUEUE1.
1. QUEUE1 = {F, E, G}
2. QUEUE2 = {A, B, D, C}
6. Remove F from QUEUE1 and add all its neighbours. Since all of its neighbours has already been
added, we will not add them again. Add node F to QUEUE2.
1. QUEUE1 = {E, G}
2. QUEUE2 = {A, B, D, C, F}
7. Remove E from QUEUE1, all of E's neighbours has already been added to QUEUE1 therefore we
will not add them again. All the nodes are visited and the target node i.e. E is encountered into
QUEUE2.
1. QUEUE1 = {G}
2. QUEUE2 = {A, B, D, C, F, E}
Now, backtrack from E to A, using the nodes available in QUEUE2.
The minimum path will be A→ B → C → E.
The data structure which is being used in DFS is stack. The process is similar to BFS algorithm. In
DFS, the edges that leads to an unvisited node are called discovery edges while the edges that
leads to an already visited node are called block edges.
Algorithm:
o Step 1: SET STATUS = 1 (ready state) for each node in G
o Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
o Step 3: Repeat Steps 4 and 5 until STACK is empty
o Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
o Step 5: Push on the stack all the neighbours of N that are in the ready state (whose
STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
o Step 6: EXIT
Example :
Consider the graph G along with its adjacency list, given in the figure below. Calculate the order to
print all the nodes of the graph starting from node H, by using depth first search (DFS) algorithm.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 60
B.sc [Computer Science] DATA STRUCTURES
Solution :
Push H onto the stack
1. STACK : H
POP the top element of the stack i.e. H, print it and push all the neighbours of H onto the stack
that are is ready state.
1. Print H
2. STACK : A
Pop the top element of the stack i.e. A, print it and push all the neighbours of A onto the stack
that are in ready state.
1. Print A
2. Stack : B, D
Pop the top element of the stack i.e. D, print it and push all the neighbours of D onto the stack
that are in ready state.
1. Print D
2. Stack : B, F
Pop the top element of the stack i.e. F, print it and push all the neighbours of F onto the stack that
are in ready state.
1. Print F
2. Stack : B
Pop the top of the stack i.e. B and push all the neighbours
1. Print B
2. Stack : C
Pop the top of the stack i.e. C and push all the neighbours.
1. Print C
2. Stack : E, G
Pop the top of the stack i.e. G and push all its neighbours.
1. Print G
2. Stack : E
Pop the top of the stack i.e. E and push all its neighbours.
1. Print E
2. Stack :
Hence, the stack now becomes empty and all the nodes of the graph have been traversed.
The printing sequence of the graph will be :
1. H→A→D→F→B→C→G→E
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 61
B.sc [Computer Science] DATA STRUCTURES
Spanning Tree:
Spanning tree can be defined as a sub-graph of connected, undirected graph G that is a tree
produced by removing the desired number of edges from a graph. In other words, Spanning tree
is a non-cyclic sub-graph of a connected and undirected graph G that connects all the vertices
together. A graph G can have multiple spanning trees.
In this section of the tutorial, we will discuss the algorithms to calculate the shortest path
between two nodes in a graph.
There are two algorithms which are being used for this purpose.
1. Prim's Algorithm:-
Prim's Algorithm is used to find the minimum spanning tree from a graph. Prim's algorithm finds
the subset of edges that includes every vertex of the graph such that the sum of the weights of
the edges can be minimized.
Prim's algorithm starts with the single node and explore all the adjacent nodes with all the
connecting edges at every step. The edges with the minimal weights causing no cycles in the
graph got selected.
The algorithm is given as follows.
Algorithm:-
o Step 1: Select a starting vertex
o Step 2: Repeat Steps 3 and 4 until there are fringe vertices
o Step 3: Select an edge e connecting the tree vertex and fringe vertex that has minimum
weight
o Step 4: Add the selected edge and the vertex to the minimum spanning tree T
[END OF LOOP]
o Step 5: EXIT
Example :-
Construct a minimum spanning tree of the graph given in the following figure by using prim's
algorithm.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 62
B.sc [Computer Science] DATA STRUCTURES
Solution:-
o Step 1 : Choose a starting vertex B.
o Step 2: Add the vertices that are adjacent to A. the edges that connecting the vertices are
shown by dotted lines.
o Step 3: Choose the edge with the minimum weight among all. i.e. BD and add it to MST.
Add the adjacent vertices of D i.e. C and E.
o Step 3: Choose the edge with the minimum weight among all. In this case, the edges DE
and CD are such edges. Add them to MST and explore the adjacent of C i.e. E and A.
o Step 4: Choose the edge with the minimum weight i.e. CA. We can't choose CE as it would
cause cycle in the graph.
The graph produces in the step 4 is the minimum spanning tree of the graph shown in the above
figure.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 63
B.sc [Computer Science] DATA STRUCTURES
2. Kruskal's Algorithm:-
Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph.
The main target of the algorithm is to find the subset of edges by using which, we can traverse
every vertex of the graph. Kruskal's algorithm follows greedy approach which finds an optimum
solution at every stage instead of focusing on a global optimum.
Algorithm:
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
o Step 4: Remove an edge from Q
o Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it to the
forest (for combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Example:
Solution:
The weight of the edges given as :
Edge AE AD AC AB BC CD DE
Weight 5 10 7 1 3 4 2
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 64
B.sc [Computer Science] DATA STRUCTURES
The next step is to add AE, but we can't add that as it will cause a cycle.
The next edge to be added is AC, but it can't be added as it will cause a cycle.
The next edge to be added is AD, but it can't be added as it will contain a cycle.
Hence, the final MST is the one which is shown in the step 4.
The cost of MST = 1 + 2 + 3 + 4 = 10.
*****
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 65
B.sc [Computer Science] DATA STRUCTURES
SEARCHING
Searching is the process of finding some particular element in the list. If the element is present
in the list, then the process is called successful and the process returns the location of that
element, otherwise the search is called unsuccessful.
There are two popular search methods that are widely used in order to search some item into
the list. However, choice of the algorithm depends upon the arrangement of the list.
Linear Search
Binary Search
Linear Search:-
Linear search is the simplest search algorithm and often called sequential search. In this
type of searching, we simply traverse the list completely and match each element of the list with
the item whose location is to be found. If the match found then location of the item is returned
otherwise the algorithm return NULL.
Linear search is mostly used to search an unordered list in which the items are not sorted.
The algorithm of linear search is given as follows.
Algorithm:
o LINEAR_SEARCH(A, N, VAL)
o Step 1: [INITIALIZE] SET POS = -1
o Step 2: [INITIALIZE] SET I = 1
o Step 3: Repeat Step 4 while I<=N
o Step 4: IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
SET I = I + 1
[END OF LOOP]
o Step 5: IF POS = -1
PRINT " VALUE IS NOT PRESENTIN THE ARRAY "
[END OF IF]
o Step 6: EXIT
Binary Search:-
Binary search is the search technique which works efficiently on the sorted lists. Hence, in
order to search an element into some list by using binary search technique, we must ensure that
the list is sorted.
Binary search follows divide and conquer approach in which, the list is divided into two
halves and the item is compared with the middle element of the list. If the match is found then,
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 66
B.sc [Computer Science] DATA STRUCTURES
the location of middle element is returned otherwise, we search into either of the halves
depending upon the result produced through the match.
Selection Sort:-
In selection sort, the smallest value among the unsorted elements of the array is selected
in every pass and inserted to its appropriate position into the array.
First, find the smallest element of the array and place it on the first position. Then, find the second
smallest element of the array and place it on the second position. The process continues until we
get the sorted array.
The array with n elements is sorted by using n-1 pass of selection sort algorithm.
In 1st pass, smallest element of the array is to be found along with its index pos. then,
swap A[0] and A[pos]. Thus A[0] is sorted, we now have n -1 elements which are to be
sorted.
In 2nd pass, position pos of the smallest element present in the sub-array A[n-1] is found.
Then, swap, A[1] and A[pos]. Thus A[0] and A[1] are sorted, we now left with n-2 unsorted
elements.
In n-1th pass, position pos of the smaller element between A[n-1] and A[n-2] is to be
found. Then, swap, A[pos] and A[n-1].
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 67
B.sc [Computer Science] DATA STRUCTURES
Therefore, by following the above explained process, the elements A[0], A[1], A[2],...., A[n-1]
are sorted.
Example:-
Consider the following array with 6 elements. Sort the elements of the array by using selection
sort.
A = {10, 2, 3, 90, 43, 56}.
Algorithm:
SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for K = 1 to N-1
Step 2: CALL SMALLEST(ARR, K, N, POS)
Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
Step 4: EXIT
SMALLEST (ARR, K, N, POS)
Step 1: [INITIALIZE] SET SMALL = ARR[K]
Step 2: [INITIALIZE] SET POS = K
Step 3: Repeat for J = K+1 to N -1
IF SMALL > ARR[J]
SET SMALL = ARR[J]
SET POS = J
[END OF IF]
[END OF LOOP]
Step 4: RETURN POS
Insertion Sort:-
Insertion sort is the simple sorting algorithm which is commonly used in the daily lives
while ordering a deck of cards. In this algorithm, we insert each element onto its proper place in
the sorted array. This is less efficient than the other sort algorithms like quick sort, merge sort,
etc.
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 68
B.sc [Computer Science] DATA STRUCTURES
Technique:
Consider an array A whose elements are to be sorted. Initially, A[0] is the only element on the
sorted set. In pass 1, A[1] is placed at its proper index in the array.
In pass 2, A[2] is placed at its proper index in the array. Likewise, in pass n-1, A[n-1] is placed at its
proper index into the array.
To insert an element A[k] to its proper index, we must compare it with all other elements i.e. A[k-
1], A[k-2], and so on until we find an element A[j] such that, A[j]<=A[k].
All the elements from A[k-1] to A[j] need to be shifted and A[k] will be moved to A[j+1].
Algorithm:
Step 1: Repeat Steps 2 to 5 for K = 1 to N-1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K ? 1
Step 4: Repeat while TEMP <=ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J ? 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
Bubble Sort:-
In Bubble sort, each element of the array is compared with its adjacent element. The algorithm
processes the list in passes. A list with n elements requires n-1 passes for sorting. Consider an
array A of n elements whose elements are to be sorted by using Bubble sort. The algorithm
processes like following.
1. In Pass 1, A[0] is compared with A[1], A[1] is compared with A[2], A[2] is compared with
A[3] and so on. At the end of pass 1, the largest element of the list is placed at the highest
index of the list.
2. In Pass 2, A[0] is compared with A[1], A[1] is compared with A[2] and so on. At the end of
Pass 2 the second largest element of the list is placed at the second highest index of the
list.
3. In pass n-1, A[0] is compared with A[1], A[1] is compared with A[2] and so on. At the end of
this pass. The smallest element of the list is placed at the first index of the list.
Algorithm:
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 69
B.sc [Computer Science] DATA STRUCTURES
Merge sort:-
Merge sort is the algorithm which follows divide and conquer approach. Consider an array A of n
number of elements. The algorithm processes the elements in 3 steps.
1. If A Contains 0 or 1 elements then it is already sorted, otherwise, Divide A into two sub-
array of equal number of elements.
2. Conquer means sort the two sub-arrays recursively using the merge sort.
3. Combine the sub-arrays to form a single final sorted array maintaining the ordering of the
array.
The main idea behind merge sort is that, the short list takes less time to be sorted.
Algorithm:
Step 1: [INITIALIZE] SET I = BEG, J = MID + 1, INDEX = 0
Step 2: Repeat while (I <= MID) AND (J<=END)
IF ARR[I] < ARR[J]
SET TEMP[INDEX] = ARR[I]
SET I = I + 1
ELSE
SET TEMP[INDEX] = ARR[J]
SET J = J + 1
[END OF IF]
SET INDEX = INDEX + 1
[END OF LOOP]
Step 3: [Copy the remaining
elements of right sub-array, if
any]
IF I > MID
Repeat while J <= END
SET TEMP[INDEX] = ARR[J]
SET INDEX = INDEX + 1, SET J = J + 1
[END OF LOOP]
[Copy the remaining elements of
left sub-array, if any]
ELSE
Repeat while I <= MID
SET TEMP[INDEX] = ARR[I]
SET INDEX = INDEX + 1, SET I = I + 1
[END OF LOOP]
[END OF IF]
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 70
B.sc [Computer Science] DATA STRUCTURES
Quick Sort:-
Quick sort is the widely used sorting algorithm that makes n log n comparisons in average case for
sorting of an array of n elements. This algorithm follows divide and conquer approach.
Algorithm:
PARTITION (ARR, BEG, END, LOC)
Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =
Step 2: Repeat Steps 3 to 6 while FLAG =
Step 3: Repeat while ARR[LOC] <=ARR[RIGHT]
AND LOC != RIGHT
SET RIGHT = RIGHT - 1
[END OF LOOP]
Step 4: IF LOC = RIGHT
SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT]
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
Step 5: IF FLAG = 0
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
Step 6:IF LOC = LEFT
SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT]
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]
Step 7: [END OF LOOP]
Step 8: END
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 71
B.sc [Computer Science] DATA STRUCTURES
Heap Sort:-
Heap sort processes the elements by creating the min heap or max heap using the
elements of the given array. Min heap or max heap represents the ordering of the array in which
root element represents the minimum or maximum element of the array. At each step, the root
element of the heap gets deleted and stored into the sorted array and the heap will again be
heapified.
Algorithm:-
HEAP_SORT(ARR, N)
Step 1: [Build Heap H]
Repeat for i=0 to N-1
CALL INSERT_HEAP(ARR, N, ARR[i])
[END OF LOOP]
Step 2: Repeatedly Delete the root element
Repeat while N > 0
CALL Delete_Heap(ARR,N,VAL)
SET N = N+1
[END OF LOOP]
Step 3: END
Prepared By
Mr.D.Sulthan Basha., Lecturer in Computer Science Page 72