0% found this document useful (0 votes)
9 views62 pages

DS Notes-II Sem-Vins

The document outlines the syllabus and course outcomes for a Data Structures course in the II Semester BCA program. It covers fundamental concepts, classifications, and practical applications of various data structures, including stacks, queues, linked lists, trees, and graphs, along with sorting and searching algorithms. Additionally, it emphasizes the importance of data structures in programming and provides a classification of primitive and non-primitive data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views62 pages

DS Notes-II Sem-Vins

The document outlines the syllabus and course outcomes for a Data Structures course in the II Semester BCA program. It covers fundamental concepts, classifications, and practical applications of various data structures, including stacks, queues, linked lists, trees, and graphs, along with sorting and searching algorithms. Additionally, it emphasizes the importance of data structures in programming and provides a classification of primitive and non-primitive data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

II SEMESTER BCA

Data Structures
(AS PER SEP SYLLABUS)

Prepared By

Ms. Vinanthi S
Assistant Professor,
Department of Computer Science,
Hindustan College, Mysuru
Data Structures

Course Outcomes :

 Understand the basics of Data Structures.


 Identify the appropriate data structures and algorithms for solving real world problems.
 Understand the practical applications of Tree and Graph.
 Understand the fundamentals of sorting and searching algorithms.

Syllabus :

UNIT – I
Introduction: Data Structure Definition, Basic Terminology and Concepts,
Importance of Data Structures in Programming. Classification of Data Structures.
Primitive Data Structures, Non-Primitive Data Structures.
Stack: Definition, Memory Representation, Algorithms for Stack Operations
(Push,
Pop), Applications of Stack.

UNIT – II
Queue: Definition, Memory Representation, Linear Queue, Circular Queue,
Enqueue, Dequeue. Applications of Queue.
Linked Lists: Definition, Types.
Singly Linked List: Implementation, Insertion [At the Beginning], Deletion [At the
End]. Doubly Linked List: Memory Representation of Singly Linked List and Doubly
Linked Lists. Applications of Linked List.

UNIT – III
Tree: Definition, Memory Representation Using Array and Linked List.
Binary Tree: Definition, Traversal Algorithms [Pre-Order, In-Order, Post-
Order], Construction of Tree from In-Order and Pre-Order, In-Order and Post-
Order.
Binary Search Trees: Insertion of a Node, Deletion of A Node.
Advanced Tree Structures AVL And B-Trees: Definition and Applications.
Data Structures

UNIT – IV
Graph: Definition, Memory Representation of Graph. Adjacency Matrix, Adjacency
List. Graph Traversal Algorithms: Breadth-First Search (BFS), Depth-First Search
Hours (DFS). Sorting Techniques: Bubble Sort, Selection Sort [Algorithm, Time &
Space Complexity]. Searching Techniques: Linear And Binary Search Sort
[Algorithm, Time & Space Complexity].
Heap: Heap Operations and Applications.

Text Books :
1. Data Structures Through C++ (4th Edition) Yashvant Kanetkar.
2. Data Structures and Algorithm Analysis in C++" by Mark Allen Weiss.
3. Data structure and Algorithms using C++ by Sachi Nandan Mohanty, Pabitra
Kumar Tripathy.
4. Data Structures and Algorithms in C++, Second Edition by Adam Drozdek.
Data Structures

DATA STRUCTURES

UNIT – 1
Data Structure Definition : A data structure is a specialized format for organizing,
processing, storing and retrieving (getting back) the data so that it can be accessed and used
efficiently. Data structures define the relationships between data and the operations that
can be performed on it.
Example: Array, Linked List, Stack queue.
A data structure is a Specific way to store and organize data in a computer's memory so
that these data can be used efficiently later.
Or
The data structure is basically a technique of organizing and storing of different types of
data items in computer memory. It is considered as not only the storing of data elements
but also the maintaining of the logical relationship existing between individual data
elements. The Data structure can also be defined as a mathematical or logical model,
which relates to a particular organization of different data elements.

 Basic Terminology and concepts in data structures:

1. Element: An element is an individual item or value in a data structure. For


example, in an array, each number (or item) is an element.
2. Node: A node is a basic unit of a data structure, especially in linked structures like
linked lists and trees. It contains data and may also have references (or pointers) to
other nodes.
3. Array: An array is a collection of elements stored in contiguous (one next to the
other) memory locations. It is indexed, allowing fast access to any element using its
index.

4. Pointer: A pointer is a variable that holds the memory address of another variable or
element. Pointers are particularly useful in linked lists and trees for navigating
through elements.
Data Structures

5. Index: An index is a numerical value that represents the position of an element in a


data structure, like an array or list.

6. Link: A link is a reference that connects nodes in linked data structures like linked lists
or trees.

7. Head: The head is the first node in a linked list. It’s the starting point of traversal in a
list.

8. Tail: The tail is the last node in a linked list, or the last element in a data structure like
a queue.

9. Stack: A stack is a data structure that follows the Last In, First Out (LIFO) principle.
Elements are added (pushed) and removed (popped) from the top of the stack.

10. Queue: A queue is a data structure that follows the First In, First Out (FIFO)
principle. Elements are added (enqueued) at the rear and removed (dequeued) from
the front.

11. Push: In a stack, push refers to adding an element to the top of the stack.

12. Pop: In a stack, pop refers to removing the element from the top of the stack.
13. Enqueue: In a queue, enqueue refers to adding an element to the rear of the queue.
14. Dequeue: In a queue, dequeue refers to removing an element from the front of the
queue.
15. Linked List: A linked list is a linear data structure where each element (node) contains
data and a reference (link) to the next node. It allows dynamic memory allocation and
efficient insertion and deletion.

16. Singly Linked List: A Singly Linked List (SLL) is a linear data structure where
each element, called a node, contains two parts:
Data: The value stored in the node.
Next: A pointer to the next node in the sequence.
Nodes are connected sequentially, forming a chain, with the last node pointing to Null.
17. Doubly Linked List: A doubly linked list is a linked list in which each node
contains two links: one pointing to the next node and one pointing to the previous
node.
Data Structures

18. Traversal: Traversal refers to the process of visiting each element in a data
structure. Common types of traversal include:

o In-order (for trees)


o Pre-order (for trees)

o Post-order (for trees)


o Linear traversal (for arrays, lists)

19. Tree: A tree is a hierarchical data structure consisting of nodes connected by edges. The
topmost node is called the root, and it branches out into sub-nodes.

20. Binary Tree: A binary tree is a tree where each node has at most two children,
commonly referred to as the left child and right child.

21. Leaf : A leaf node is a node in a tree that does not have any children (i.e., it has no
further branches).

22. Root: The root is the topmost node in a tree from which all other nodes descend.

23. Depth of a Node: The depth of a node is the number of edges from the root to that
node.

24. Height of a Tree: The height of a tree is the length of the longest path from the root
to a leaf node.

25. Binary Search Tree: A Binary Search Tree (BST) is a binary tree data structure in
which each node has the following properties:
 Left Subtree: Contains only nodes with values less than the parent node.

 Right Subtree: Contains only nodes with values greater than the parent node.

 No Duplicates: Typically, duplicate values are not allowed.

 Left Subtree: Contains only nodes with values less than the parent node.

 Right Subtree: Contains only nodes with values greater than the parent node.

 No Duplicates: Typically, duplicate values are not allowed.

26. B-tree: A B-tree is a self-balancing tree data structure used for indexing large
amounts of data, particularly in databases and file systems. It maintains sorted data
Data Structures

and allows efficient insertion, deletion, and search operations.

27. AVL Tree: An AVL tree is a self-balancing binary search tree in which the
difference in height between the left and right subtrees of any node is at most one.
If the balance factor becomes greater than one, rotations are used to restore balance.

28. Graph: A graph is a collection of nodes (vertices) and edges (connections between
nodes). Graphs can be directed (edges have a direction) or undirected (edges have
no direction).

29. Degree of a Node: The degree of a node in a graph is the number of edges
connected to the node. In a directed graph, the in-degree is the number of incoming
edges, and the out-degree is the number of outgoing edges.

30. Adjacency: In a graph, adjacency refers to the relationship between two vertices that
are connected by an edge.

28. Adjacency Matrix: An adjacency matrix is a 2D array used to represent a graph,


where each element at position (i, j) indicates whether there is an edge between
vertices i and j.

29. Adjacency List: An adjacency list is a collection of lists or arrays used to represent
a graph, where each list corresponds to a vertex and contains a list of adjacent
vertices.

30. Sorting : Sorting is the process of arranging the elements in a specific order, typically
either in ascending or descending order. Sorting techniques include :

 Bubble Sort

 Selection Sort

 Insertion Sort

 Merge Sort

 Quick Sort

 Heap Sort

31. Searching : Searching is the process of finding the location or existence of a


Data Structures

specific element within a collection of elements, such as an array, list, or database.

 Linear Search (for arrays, linked list)

 Binary Search (for arrays, binary search trees)

 Depth-First Search (DFS) (for graphs)

 Breadth-First Search (BFS) (for graphs)

32. Heap: A heap is a specialized tree-based data structure that satisfies the heap
property:
In a max heap, the value of the parent node is greater than or equal to the values of its
children; in a min heap, the value of the parent node is less than or equal to the
values of its children.

 Importance of Data Structures in Programming :

1) Efficiency: Data structures are fundamental to achieving efficient data processing.


They enable algorithms to run faster and consume fewer resources by optimizing data
storage and access patterns.
2) Problem Solving: Many computer science problems require the manipulation of
data. Data structures provide a systematic way to approach these problems and frame
efficient solutions.
3) Code Reusability: Well-designed data structures can be reused across different
projects, saving time and effort in software development.
4) Memory Management: Data structures help manage memory efficiently, reducing
memory leaks and optimizing resource allocation.
5) Algorithm Design: Algorithms often rely on specific data structures. Choosing the right
data structure can make algorithm development and optimization more
straightforwarded.
6) Scalability: As data sizes grow, the choice of data structure becomes critical. A poorly
chosen data structure can lead to performance congestion in large-scale applications.
7) Foundation for Advanced Topics: Forms the backbone for concepts like machine
learning, database indexing, and operating systems.
Data Structures

8) Foundation of Programming Languages: Many programming languages are


built with core data structures like stacks, heaps, and hash tables.
9) Support for Dynamic Applications: Adapts to changes in data size or structure,
as in linked lists or dynamic arrays.
10) Real-World Applications :
Databases - B-trees, hash tables, etc.
Compilers - abstract syntax trees.
Operating systems - queues, stacks for task scheduling.
Networking - graphs for routing algorithms.

 Classification of Data Structures:


Data Structures

 Primitive Data Structures


Primitive data structure is a type of data structure that are pre-defined in the
programming language that stores the data of only one type such as integer,
character, float, double and Boolean.
1. Integer :Integer data type is used to store integer values. Short keyword for
Integer is “int”. Eg: int num = 10;
2. Character :Character data type is used to store Characters i.e., alphabets.
Short keyword for Character is “char”.
Eg: char ch = 'A';
3. Float : Float data type is used to store float values i.e., decimal numbers upto
7-decimals. Short keyword for Float is “float”.
Eg: float f = 3.14;
4. Double : Double data type is similar to float which is used to store decimal
numbers upto 15- decimals. Short keyword for Double is “double”.
Eg: double d = 2.71828;
5. Boolean : Boolean data type is used to store boolean values i.e., true / false. Short
keyword for Boolean is “bool”.

Eg: bool
isTrue
= true;
bool
isFals
e=
false;

 Non-Primitive Data Structures


Non-Primitive data structure is a type of data structure which is a user -defined that
stores the data of different types in a single entity such as array, linked list and stack.
1. Linear data structure

In a linear data structure, the data elements connect to each other sequentially (in a
Data Structures

line). A user can traverse each element through a single run.


 Array : An array is a data structure that stores a collection of elements of the
same type in a contiguous(continuous) memory blocks. It allows for efficient
access to elements using indices.
Eg: int arr [5] = {1, 2, 3, 4, 5};

 Stack :A stack is a linear data structure where elements are stored in the LIFO
(Last In First Out) principle where the last element inserted would be the first
element to be deleted.
Eg: struct Stack
{
int
items[
MAX_
SIZE];
int top;
};

 Queue :A queue is a linear data structure where elements are stored in the FIFO (First
In First Out) principle where the first element inserted would be the first element to be
accessed.
Eg: struct Queue
{
int items[MAX_SIZE]; int front, rear;
};

 Linked List :A linked list consists of nodes where each node contains a data field and
a reference(link) to the next node in the list.
Eg: struct Node
{
int data;
struct Node* next;
Data Structures

};
2)Non-Linear data structure:In a non-linear data structure, the data elements connect to each
other hierarchically. Thus, they are presented at various levels.

 Trees : Tree Data Structure is a hierarchical data structure in which a collection of


elements known as ‘nodes’ are connected to each other via ‘edges’ such that there
exists exactly one path between any two nodes.

 Graphs : A graph is a collection of two sets V and E where V is a finite set of vertices
and E is a finite set of edges.

 Difference between Primitive and Non-Primitive Data Structures

Aspect Primitive Data Structure Non-Primitive Data


Structure
Basic data types that store a Advanced data types that
1. Definition
single value. store multiple values.

Array, Linked List, Stack,


2. Examples int, char, float, double, boolean.
Queue, Tree, Graph.

Stored in heap memory


3. Storage Stored in stack memory.
(requires references).

Predefined in programming Can be user-defined (e.g.,


4. Data Type
languages. classes in OOP).

More complex with


5. Simplicity Simple and easy to use.
additional functionalities.

Directly operated using Require specific methods


6. Operations
arithmetic/logical operations. for insertion, deletion, and
traversal.
Data Structures

May require more


7.Memory Requires less memory.
memory due to
Efficiency
additional structures
like pointers.

8.Relationship No relationship between values Elements can be connected

Between Data (each holds a single value). (e.g., linked lists, trees).

Easy to manipulate with direct Requires


9. Manipulation
assignments (int a = 5;). complex
logic
LinkedList
.add(5)

Used for storing and


Used for storing simple values.
managing large amounts of
data efficiently.

 Difference between Linear and Non-Linear Data Structure

Data elements are


1. Definition Data elements are arranged
sequentially. connected in a
hierarchical or complex
manner.

2. Examples Array, Linked List, Stack, Queue. Tree, Graph, Hash Table.

Stored in a contiguous memory


3.Storage Structure Stored in non-contiguous
location (like arrays) or sequentially
memory locations with
linked (like linked lists).
pointers.

Each element has a direct predecessor Elements are connected


4.Relationship
and successor (except first and last). in multiple ways, not
Between Elements
Data Structures

necessarily in
sequence.

Requires special
Can be traversed linearly in one or
5.Traversal traversal techniques
both directions.
like DFS (Depth-First
Search) or BFS
(Breadth-First Search).

More complex in structure


6.Complexity Simpler in implementation and usage.
and manipulation.

7. Memory Uses memory efficiently as elements May require extra


Utilization are stored in order. memory due to pointers
or references.

Easier to implement operations like Requires complex


8. Operations
insertion and deletion. operations like tree
balancing, graph
traversal, etc.

Suitable for tasks requiring simple data Suitable for complex data
9. Usage
management (e.g., queues in CP U relationships (e.g., social
scheduling) networks, file systems).

 ARRAYS

An array is a collection of homogeneous elements with unique name and the


elements are arranged one after another in adjacent memory location. The data
items in an array are called as elements. These elements are accessed by numbers
called as subscripts or indices. Since the elements are accessed using subscripts,
arrays are also called as subscripted variables.

Types of Arrays

There are three types of array:


Data Structures

 One-dimensional Array
 Two-dimensional Array
 Multi-dimensional Array

Basic operations on one-dimensional arrays
The following operations are performed on arrays:
 Traversing: Accessing each element of the array exactly once to do some
operation.
 Searching: Finding the location of an element in the array.
 Sorting: Arranging the elements of the array in some order. Insertion: Inserting
an element into the array.
 Deletion: Removing an element from the array.
 Merging: Combining one or more arrays to form a single array.
Data Structures

STACK :

Stack Definition : A stack is a linear data structure that follows the Last In, First Out
(LIFO) principle, meaning the last element added to the stack is the first one to be
removed. Insertion and Deletion of elements takes place at the same end, called the top of
the stack.
Example: Tower of Hanoi game, Stack of plates, A deck of cards.

 Memory Representation of Stack :

The image illustrates the concept of a Stack in computer science, a data structure
that operates on the principle of Last In, First Out (LIFO).
1. Stack Structure: A stack is represented as a vertical collection of elements where
operations can only be performed at one end, called the top.
2. LIFO Principle: The phrase "Last In, First Out" (LIFO) means that the last element
added (pushed) to the stack will be the first to be removed (popped).
3. Push Operation: Push is the operation of adding an element to the top of the stack.
For example, in the image, 6 is the most recently pushed element and is now at the
top of the stack.
4. Pop Operation: Pop is the operation of removing the top element from the stack. In
the current state, if a pop operation is performed, the number 6 will be removed.
5. Top Pointer: The top indicates the element that is currently at the highest position in
the stack. It helps track where the next push or pop operation will occur.

Elements in the Stack: The stack contains elements 1, 2, 3, 4, 5, 6 from bottom to top.
The element 1 was the first to be pushed, making it the last to be popped.
Data Structures

 Algorithms for Stack Operations (Push, Pop):

 PUSH OPERATION

Through this operation, we add new elements to our stack. The push( ) operation
goes through the following steps:

1. Before inserting a new element into the stack, check whether the stack is full or not.

2. If the stack is already full and we try to insert more elements into it, it will return an
overflow error.

3. If the stack is not full, increment the ‘top’ to point to the next empty space and insert the
new element there.

4. When the stack is empty, ‘top’ points to -1. Once we add a new element, the ‘top’
points to that element and has a value of 0.
5. The stack can accommodate elements as long as it does not reach its maximum

capacity .

Algorithm for PUSH operation

Step 1: [ check for


overflow ]
if TOP =
MAX-1
then
print “Stack is full
or Overflow” exit
Step 2: [ increment TOP
Data Structures

pointer ]
TOP TOP + 1

Step 3: [ Insert an item ]


Stack[TOP] item

Step 4: return
[ end if ]

OR

if (top == max - 1) // When stack is full // Here, max is the total stack capacity
{
print("Overflow");
}
else
{
top = top + 1; //
Incrementing top by 1
stack[top]
=item;//Inserting the
element
Data Structures

 POP OPERATION
It helps in removing elements from the top of the stack. The steps involved in the
pop operation are:

1. The first step is to check whether the stack is empty or not.

2. If the stack is already empty, we cannot remove any element from it. So, we will return
an underflow error.

3. If the stack is not empty, remove the top most element of the stack.

4. Decrement the position of ‘top’ by 1 and return.

Algorithm :
if (top = = -1) // If stack is empty
{
print("Underflow");
}
else
{
item = stack [top]; //
Deleting the element top =
top - 1; //
Decrementing top by 1
}
Data Structures

Applications of Stack:

1. Stacks can be used to check if parentheses are balanced.


2. Converts infix expressions into postfix or prefix notations.

3. Used to evaluate postfix and prefix expressions in mathematical computations.

4. Stacks are used to allocate and manage memory in some operating systems and
programming languages.
5. Maintains browser history for back and forward navigation in web browsers.

6. Reverses strings by pushing and popping characters from the stack.

7. Solves recursive problems like the Tower of Hanoi using stack operations.

8. Implements undo and redo functionality in text editors and design software.
Data Structures

DATA STRUCTURES
UNIT – 2

QUEUE
A queue is a linear data structure that follows the FIFO (First In, First Out) principle. It has
two primary operations:

 Enqueue: Adds an element to the rear.

 Dequeue: Removes an element from the front.

Memory Representation of Queue

A queue can be represented using:


1. Array-based implementation (Static Queue)
2. Linked List-based implementation (Dynamic Queue)

Types of Queue
1. Linear Queue: A simple queue where insertion happens at the rear and deletion at the
front. The queue becomes full when the rear reaches the last index, even if there are
vacant spaces at the front.

2. Circular Queue: A queue that overcomes the limitation of a linear queue by


connecting the last index back to the first index (modular arithmetic is used).

1. Linear Queue Representation Steps


A Linear Queue is a simple queue where elements are inserted at the rear and removed from
the front. It follows the FIFO (First In, First Out) principle.

Operations in a Linear Queue


1. Enqueue (Insertion)
o Check if the queue is full (rear == SIZE - 1).

o If not full, increment rear and insert the new element.


o If inserting the first element, set front = 0.

2. Dequeue (Deletion)
o Check if the queue is empty (front == -1 or front > rear).

o If not empty, remove the front element and increment front.


o If the last element is removed, reset front and rear to -1.
Data Structures

Algorithm for Enqueue (Insertion) in Linear Queue

void enqueue(int queue[], int Crear, int size, int value) { if (rear

== size - 1) {

cout << "Queue is Full (Overflow)\n"; return;

queue[++rear] = value;

Algorithm for Dequeue (Deletion) in Linear Queue

void dequeue(int queue[], int Cfront, int rear) { if

(front > rear) {

cout << "Queue is Empty (Underflow)\n";

return;

}
Data Structures
cout << "Deleted: " << queue[front++] << endl;

Drawback of Linear Queue

 Even after dequeuing elements, space is not reused, leading to inefficiency.

 Requires shifting of elements, which is costly.

2. Circular Queue Representation Steps


A Circular Queue overcomes the limitation of the linear queue by connecting the last position
back to the first position, making use of empty spaces.

Operations in a Circular Queue

1. Enqueue (Insertion)
o Check if the queue is full using the condition:
(rear + 1) % SIZE == front
o If not full, insert the new element at (rear + 1) % SIZE.

o If inserting the first element, set front = 0.

2. Dequeue (Deletion)
o Check if the queue is empty (front == -1).
o Remove the front element and move front to (front + 1) % SIZE.
o If the last element is removed, reset front and rear to -1.
Data Structures

Algorithm for Enqueue in Circular Queue

void enqueue(int queue[], int Cfront, int Crear, int size, int value) { if ((rear +

1) % size == front) {

cout << "Queue is Full (Overflow)\n"; return;

if (front == -1) front = 0; // First element rear =

(rear + 1) % size;

queue[rear] = value;

Algorithm for Dequeue in Circular Queue

void dequeue(int queue[], int Cfront, int Crear, int size) { if (front

== -1) {

cout << "Queue is Empty (Underflow)\n";

return;

cout << "Deleted: " << queue[front] << endl; if

(front == rear) {

front = rear = -1; // Queue becomes empty

} else {

front = (front + 1) % size;

Advantages of Circular Queue

 Efficient use of memory as spaces are reused.

 No need for shifting elements.


Data Structures

Applications of Queue

A queue is a data structure that follows the First-In-First-Out (FIFO) principle. It has
numerous applications in computing, networking, and real-world scenarios. Here are some
key applications:

1. Operating System & Process Scheduling


 CPU Scheduling: Queues manage processes in operating systems using scheduling
algorithms like First-Come-First-Serve (FCFS) and Round Robin Scheduling.

 Disk Scheduling: Disk I/O requests are handled using queues to optimize data
retrieval.

2. Data Structure and Algorithm Applications


 Breadth-First Search (BFS): A queue is used to explore nodes level by level in
graph and tree traversal.

 Tree and Graph Traversals: Used to explore nodes in hierarchical structures.

 Priority Queues in Dijkstra’s Algorithm: Helps in finding the shortest path in


graphs.

3. Network Applications
 Packet Scheduling: Routers and switches use queues to manage data packet
transfers.

 Call Handling in Call Centres: Incoming customer calls are queued and answered in
order.

4. Job Scheduling in Printers


 When multiple print jobs are submitted, they are stored in a queue and executed one
by one.

5. Real-time Systems & Task Scheduling

 In real-time applications, queues prioritize urgent tasks (e.g., medical monitoring


systems).

6. Multi-threading and Inter-Process Communication


Data Structures

 Producer-Consumer Problem: One thread produces data, and another consumes it


using a queue for synchronization.

7. Banking & Ticket Counters


 Customers are served in a FIFO manner at banks, ticket counters, and service centers.

8. Traffic Management Systems


 Traffic signals use queues to manage vehicles at intersections efficiently.

9. Message Queues in Distributed Systems

 Messaging services like Kafka, RabbitMQ use queues for asynchronous


communication between services.

10. Call Center and Customer Support


 Calls are queued and assigned to available agents in First-Come-First-Serve order.
Data Structures

LINKED LIST
A linked list is a linear data structure where each node contains:

1. Data

2. Pointer to the next node (Singly Linked List) or both next and previous nodes (Doubly
Linked List).

Types of Linked Lists


1. Singly Linked List: Each node has one pointer to the next node.

2. Doubly Linked List: Each node has two pointers, one to the next and one to the
previous node.

3. Circular Linked List: The last node connects to the first node.

1. Singly Linked List:


A Singly Linked List (SLL) is a linear data structure where each element, known as a node,
contains data and a reference (or link) to the next node in the sequence. This structure allows
for efficient insertion and deletion operations without the need for contiguous memory
allocation.

Graphical Representation of a Singly Linked List:


Each node in a singly linked list comprises two components:
1. Data Field: Stores the actual data.

2. Next Pointer: Holds the reference to the next node in the list.
Data Structures

Algorithm for Insertion at Beginning (SLL)

struct Node { int

data;

Node* next;

};

void insertAtBeginning(Node* Chead, int value) {

Node* newNode = new Node();

newNode->data = value;

newNode->next = head;

head = newNode;

Algorithm for Deletion at End (SLL)

void deleteAtEnd(Node* Chead) { if

(!head) return;

if (!head->next) {

delete head; head =

NULL; return;

Node* temp = head;

while (temp->next->next) temp = temp->next; delete

temp->next;

temp->next = NULL;

}
Data Structures

2. Doubly Linked List:

A Doubly Linked List (DLL) is a type of linked list in which each node contains three
components:

1. Data Field: Stores the actual data.

2. Next Pointer: References the next node in the sequence.

3. Previous Pointer: References the previous node in the sequence.

This bidirectional linkage allows traversal in both forward and backward directions,
enhancing flexibility over singly linked lists.

Algorithm for Insertion at Beginning (DLL)

struct DNode {

int data;

DNode* prev;

DNode* next;

};

void insertAtBeginning(DNode* Chead, int value) { DNode*

newNode = new DNode();

newNode->data = value;
Data Structures

ewNode->prev = NULL;

newNode->next = head;

if (head) head->prev = newNode;

head = newNode;

Algorithm for Deletion at End (DLL)

void deleteAtEnd(DNode* Chead) { if

(!head) return;

if (!head->next) {

delete head; head =

NULL; return;

DNode* temp = head;

while (temp->next) temp = temp->next;

temp->prev->next = NULL;

delete temp;

Advantages of Linked Lists

Feature Singly Linked List Doubly Linked List

Insertion/Deletion Faster at head Faster at head C tail

Memory Usage Less (one pointer) More (two pointers)

Traversal One direction Both directions


Data Structures

3. Circular Linked List:

A Circular Linked List is a variation of the linked list where the last node points back to the
first node, forming a circle. This structure allows for continuous traversal without a defined
beginning or end.

Graphical Representation:

1. Circular Singly Linked List: In this type, each node contains data and a reference to
the next node. The last node's reference points back to the first node.

2. Circular Doubly Linked List: Here, each node has three components:
o Data: The value stored in the node.

o Next Pointer: Reference to the next node.

o Previous Pointer: Reference to the previous node.

Applications of Linked Lists


Linked lists are widely used in various domains due to their dynamic memory
allocation, efficient insertion/deletion, and non-contiguous storage. Below are some
key applications:

1. Implementation of Data Structures

Stacks and Queues: Implemented using linked lists for efficient dynamic storage.
Graphs: Adjacency lists use linked lists to represent graph edges.

Hash Tables: Collision resolution using chaining (linked list implementation).

2. Dynamic Memory Allocation


Operating Systems: Memory management uses linked lists for heap allocation (free
and allocated memory blocks).
Data Structures

3. Undo/Redo Functionality

Text Editors & Software Applications: Maintain a history of actions using a doubly
linked list for undo and redo operations.

4. Web Browsers – Forward & Backward Navigation


Doubly Linked List stores browsing history, allowing users to move back and
forward between pages.

5. Music & Video Playlists


Circular Linked Lists are used in media players to loop through songs or videos
continuously.

6. CPU Scheduling (Operating Systems)


Circular Linked Lists implement Round Robin Scheduling, where processes are
executed in a cyclic order.

7. File Systems

Linked allocation in file systems stores file data across non-contiguous blocks to
minimize fragmentation.

8. Polynomial Arithmetic
Linked Lists store and process polynomials efficiently for mathematical
computations.

9. Social Media & Recommendation Systems

Graph-based models using linked lists connect users, friends, or recommended


products dynamically.

10. Network Packet Transmission

Linked Lists store incoming and outgoing packets efficiently in network routers.
Data Structures

DATA STRUCTURES
UNIT – 3

TREE

A tree is a non-linear data structure that consists of nodes connected by edges. It follows
a hierarchical structure with a designated root node, and each node can have child
nodes.

Nodes and Edges in a Tree:

In a Tree Data Structure, the basic components are:

1. Nodes:

o Each element in the tree is called a node.


o The root node is the topmost node (e.g.,
A).

o Each node can have child nodes.

2. Edges:
o The connection between two nodes is called an edge.

o It represents the relationship (parent-child) between nodes.

Graphical Representation of Nodes and Edges


Nodes: {A, B, C, D, E, F}

Edges:
 A→B

 A→C

 B→D

 B→E

 C→F

Memory Representation of Trees


1. Using Arrays:

o Trees (especially binary trees) can be stored in arrays using level order
traversal.

o For a node at index i:


Data Structures

 Left child is at index 2i + 1.

 Right child is at index 2i + 2.

o B (index 1) → Left child of A (index 0)


o C (index 2) → Right child of A (index 0)
o D (index 3) → Left child of B (index 1)
o E (index 4) → Right child of B (index 1)
o F (index 5) → Right child of C (index 2)

2. Using Linked List:

o Each node contains:

 Data

 Pointer to the left child

 Pointer to the right child (for binary trees)


Data Structures

BINARY TREE

A Binary Tree is a tree where each node has at most two children: left and right.

Traversal Algorithms
1. Pre-Order (Root → Left → Right): [Preorder: 1 → 2 → 3 → 4 → 5 → 6 → 7]

o Visit root.

o Traverse left subtree.

o Traverse right subtree.


Data Structures

2. In-Order (Left → Root → Right): [In Order: 5 → 2 → 6 → 1 → 8 → 3 → 7]

o Traverse left subtree.

o Visit root.

o Traverse right subtree.

3. Post-Order (Left → Right → Root): [Post Order: 5 → 6 → 2 → 8 → 7 → 3 → 1]

o Traverse left subtree.


o Traverse right subtree.

o Visit root.

Constructing a Binary Tree

1. From In-Order and Pre-Order:

o The first element of the pre-order is the root.

o Locate this root in the in-order sequence (left side becomes left subtree,
right side becomes right subtree).

o Recursively repeat for subtrees.

2. From In-Order and Post-Order:

o The last element of the post-order is the root.

o Locate this root in the in-order sequence.

o Recursively construct left and right subtrees.

Types of Binary Tree

Here are some important types of Binary Tree:


Data Structures

 Full Binary Tree: Each node can have 0 or 2 child nodes in this binary tree. Only
one child node is not allowed in this type of binary tree. So, except for the leaf
node, all nodes will have 2 children.

 Full Binary Tree: Each node can have 0 or 2 nodes. It seems like the Full Binary
Tree, but all the leaf elements are lean to the left subtree, whereas in the full binary
tree node can be in the right or left subtree.

 Perfect Binary Tree: All the nodes must have 0 or 2 nodes, and all the leaf nodes
should be at the same level or height. The above example of a full binary tree
structure is not a Perfect Binary Tree because node 6 and node 1,2,3 are not in the
same height. But the example of the Complete Binary Tree is a perfect binary tree.

 Degenerate Binary Tree: Every node can have only a single child. All the
operations like searching, inserting, and deleting take O(N) time.
Data Structures

 Balanced Binary Tree: Here this binary tree, the height difference of left and
right subtree is at most 1. So, while adding or deleting a node, we need to
balance the tree’s height again. This type of Self-Balanced Binary Tree is called
the AVL tree.

BINARY SEARCH TREE (BST)

A BST is a binary tree where:

 Left subtree contains nodes with values less than the root.
 Right subtree contains nodes with values greater than the root.

Operations
Binary trees support several fundamental operations, including insertion, deletion,
searching, and traversal:

1. Insertion

Insertion involves adding a new node to the binary tree. In a binary tree, a new node is
usually inserted at the first available position in level order to maintain the completeness
of the tree.

Example:

Let's insert the value 6 into the following binary tree:


Data Structures

After insertion, the binary tree will look like this:

2. Deletion

Deletion involves removing a node from the binary tree. In a binary tree, the node to be
deleted is replaced by the deepest and rightmost node to maintain the tree's structure.

Example:
Let's delete the value 3 from the following binary tree:

After deletion, the binary tree will look like this:


Data Structures

3. Search
Searching involves finding a node with a given value in the binary tree. The search
operation can be implemented using any traversal method (in-order, pre-order, post-
order, or level-order).

Example:

Let's search for the value 5 in the following binary tree:

Using level-order traversal:

 Visit node 1

 Visit node 2

 Visit node 3

 Visit node 4

 Visit node 5 (found)

4. Traversal
Traversal involves visiting all the nodes in the binary tree in a specific order. The main
traversal methods are in-order, pre-order, post-order, and level-order.

Example:
Consider the following binary tree:

 In-order Traversal (Left, Root, Right): 4, 2, 5, 1, 3

 Pre-order Traversal (Root, Left, Right): 1, 2, 4, 5, 3


Data Structures

 Post-order Traversal (Left, Right, Root): 4, 5, 2, 3, 1

 Level-order Traversal (Breadth-First): 1, 2, 3, 4, 5

Advanced Tree Structures

AVL Tree (Adelson-Velsky and Landis Tree)

 A self-balancing BST where the height difference of left and right


subtrees (Balance Factor) is at most 1.

 Rotations are used to maintain balance:

1. Left-Left (LL) Rotation

2. Right-Right (RR) Rotation

3. Left-Right (LR) Rotation

4. Right-Left (RL) Rotation

 Applications:

1. Database Indexing

2. Memory Management

3. File Systems

4. Network Routing

5. Priority Queues

6. Compiler Design

7. Geospatial Databases

8. Event Scheduling Systems

9. Artificial Intelligence and Machine Learning

10. Telecommunication Systems


Data Structures

B-TREES

 A self-balancing search tree used for large data storage (e.g., databases, file
systems).

 Each node has multiple children and stores multiple keys.

 Balanced because all leaves are at the same level.

 Applications:

o It is used in large databases to access data stored on the disk


o Searching for data in a data set can be achieved in significantly less time
using the B-Tree

o With the indexing feature, multilevel indexing can be achieved.

o Most of the servers also use the B-tree approach.

o B-Trees are used in CAD systems to organize and search geometric data.

o B-Trees are also used in other areas such as natural language processing,
computer networks, and cryptography.
Data Structures

DATA STRUCTURES
UNIT – 4
GRAPH

A graph is a data structure consisting of a set of nodes (vertices) and edges that connect
pairs of nodes. Graphs can be directed (edges have a direction) or undirected (edges
have no direction). They can also be weighted (edges have weights) or unweighted.

Memory Representation of Graph


Graphs can be represented in two primary ways:

1. Adjacency Matrix

 A 2D array where matrix[i][j] = 1 if there is an edge between vertex i and


j, otherwise 0.
 Space Complexity: O(V2), where V is the number of vertices.

Example

Consider the following undirected graph representation:

Undirected graph representation


Data Structures

In the above examples, 1 represents an edge from row vertex to column vertex, and 0
represents no edge from row vertex to column vertex.

 Pros: Fast lookup for edge existence.

 Cons: Uses a lot of space for sparse graphs.

2. Adjacency List

 Each vertex has a list of adjacent vertices.

 Implemented using arrays of linked lists or hash maps.

 Space Complexity: O(V+E), where EE is the number of edges.

Example

Let's see the following directed graph representation implemented using linked list:

 Pros: Efficient for sparse graphs.

 Cons: Slower edge lookup than adjacency matrix.


Data Structures

Graph Traversal Algorithms

1. Breadth-First Search (BFS)


 Explores all neighbors of a node before moving to their neighbors.

 Implemented using a queue (FIFO).

 Time Complexity: O(V+E)

 Space Complexity: O(V) (for visited array and queue)

Example:

Step 1: Checking over A.

Figure 2: Checking over


Step 2: Moving A into the Visited and
subsequently adding its neighboring
nodes, B, C, and D, to the Queue. It is
important to note that B is marked as
green, while C and D are marked as
purple, reflecting the current focus on
node B.

Figure 3: Moving
Step 3: B is moved into the Visited, and then the
neighbors of B, E, and F, are placed in
the Queue. While A is a neighbor of C, it cannot
be added to the Queue as it is already in
the Visited.
Data Structures

Figure 4: Moving

Step 4: C is moved into the Visited category, and


then the neighbor of C, which is G, is placed in
the Queue. Once more, the addition of A to the
Queue is not permitted.

Figure 5: Moving
Step 5: D is moved into the Visited, and
subsequently, the neighbors of D that are not
already in the Visited, namely H and I, are
placed in the Queue.

Figure 6: Moving
Step 6: We are at E now. It is important to note
that although E has a neighbor B, once it is
marked as Visited, B can no longer be added to
the Queue. Consequently, E can be moved
into the Visited category.

Figure 7: Moving
Step 7: Moving F into the Visited category in a
similar manner. The neighbor of F, which is B,
cannot be added to the Queue as it has already
been visited.

Figure 8: Moving
Step 8: Moving G into the Visited.

Figure G: Moving

Step 9: Moving H into the Visited.


Data Structures

Figure 10: Moving

Step 10: Moving I into the Visited.

Figure 11: Moving

In this instance, all nodes are depicted in red, indicating that the entire graph has been
traversed and the process has been finalized. The Visited path encompasses the sequence
A, B, C, D, E, F, G, H, and I, progressing from the upper level to the middle level, and
ultimately to the lower level.

The following code can be used to implement this process. It is important to note that in
an undirected graph, the key-value pairs are symmetric to each other. To be more precise, if
the value of key A contains B, then the value of key B must also contain A. I also
recommend experimenting with various graphs and initial points.

Algorithm:

The steps involved in the BFS algorithm to explore a graph are given as follows -

Step 1: SET STATUS = 1 (ready state) for each node in G

Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)

Step 3: Repeat Steps 4 and 5 until QUEUE is empty

Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).

Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS = 1)
and set

their STATUS =
2 (waiting state)
[END OF
LOOP] Step 6:
EXIT
Data Structures

2. Depth-First Search (DFS)

 Explores as far as possible along a branch before backtracking.

 Implemented using a stack (or recursion).

 Time Complexity: O(V+E)

 Space Complexity: O(V) (for recursion stack)

Step 1: Reviewing A.

Figure 13: Reviewing

Step 2: Moving A into the Visited and


subsequently adding the neighbors of A (B, C,
and D) to the Stack. It is important to note
that D is marked as green, while B and C are
marked as purple, reflecting the current
position at D.

Step 3: Moving D into the Visited, and subsequently


placing its neighbors, H and I, in the Stack. Similar to
BFS, while A is a neighbor of D, it cannot be added to
the Stack because it is already in the Visited.

Step 4: Moving the element I into the Visited. The


neighbor of I, D, cannot be placed in the Stack as
it has already been visited.

Step 5: Moving H into the Visited in a similar manner.


Once more, the addition of D to the Stack is not
possible as D is already present in the Visited.
Data Structures

Step 6: C is moved into the Visited, and then the


neighbors of C, which are G, are placed in
the Stack.

Step 7:

Moving G into the Visited.

Step 8: B is transferred to the Visited, and


subsequently, the neighboring nodes of B,
namely E and F, are placed in the Stack.
Figure 20: Moving

Step G: Moving F into the Visited.

Step 10: Moving E into the Visited.

At this point, all nodes have been visited,


signifying the completion of the process.
The sequence of traversal
is A, D, I, H, C, G, B, F, and E.
Alternatively,
it can be described as moving through the right branch initially, followed by the middle
branch, and ultimately the left branch.

Once again, the code is provided below. You can observe that BFS and DFS would
employ nearly identical code, with the only difference being the use of pop(0) for the
queue in BFS and pop(-1) for the stack in DFS.
Data Structures

Algorithm

Step 1: SET STATUS = 1 (ready state) for each node in G

Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)

Step 3: Repeat Steps 4 and 5 until STACK is empty

Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)

Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS
= 1) and set their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT

Difference between BFS and DFS

Key BFS DFS

Definition BFS stands for Breadth First DFS stands for Depth First Search.
Search.

Data structure BFS uses a Queue to find the DFS uses a Stack to find the shortest path.
shortest path.

Source BFS is better when target is DFS is better when target is far from source.
closer to Source.

Suitability As BFS considers all DFS is more suitable for decision tree. As
fo neighbor so it is not suitable for with one decision, we need to traverse
r decision tree decision tree used in puzzle further to augment the decision. If we reach
games. the conclusion, we won.

Speed BFS is slower than DFS. DFS is faster than BFS.

Time Time Complexity of BFS = Time Complexity of DFS is also O(V+E)


Complexity O(V+E) where V is vertices where V is vertices and E is edges.
and E is edges.

Memory BFS requires more memory DFS requires less memory space.
space.

Tapping In BFS, there is no problem of In DFS, we may be trapped into infinite loops.
i trapping into finite loops.
n loops
Principle BFS is implemented using DFS is implemented using LIFO (Last In First
FIFO (First In First Out) Out) principle.
principle.
Data Structures

Sorting Techniques

1. Bubble Sort
Bubble sort is a simple sorting algorithm. The algorithm starts at the beginning of the data
set. It compares the first two elements, and if the first is greater than the second, it swaps
them. It continues doing this for each pair of adjacent elements to the end of the data set.
It then starts again with the first two elements, repeating until no swaps have occurred on
the last pass

Time Complexity: Best: O(n)O(n) (Already sorted) C Worst C Average: O(n2)O(n^2)

Bubble Sort Algorithm:


Step 1: Begin BubbleSort (list)

Step 2: for all elements of list

Step 3: if list [i] > list [i+1]


Swap (list [i], list [i+1]

[End of if]

[End for]

return list

Step 4: End Bubblesort

2. Selection Sort
 Selection sort is a simple sorting algorithm which finds the smallest element in
the array and exchanges it with the element in the first position. Then finds the
second smallest element and exchanges it with the element in the second position
and continues until the entire array is sorted.
Data Structures

• In the above diagram, the smallest


element is found in first pass that is 9
and it is placed at the first position.
• In second pass, smallest element is
searched from the rest of the element
excluding first element. Selection sort
keeps doing this, until the array is
sorted.

Algorithm
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted

Pseudocode for Selection Sort


SelectionSort(array)
for i from 0 to length(array) - 1 do
min_index = i
for j from i + 1 to length(array) do

if array[j] < array[min_index] then


min_index = j
swap(array[i], array[min_index])
Data Structures

Selection sort complexity

Now, let's see the time complexity of selection sort in best case, average case, and in
worst case. We will also see the space complexity of the selection sort.

1. Time Complexity

Case Time Complexity


Best Case O(n2)
Average Case O(n2)
Worst Case O(n2)
o Best Case Complexity - It occurs when there is no sorting required, i.e. the array
is already sorted. The best-case time complexity of selection sort is O(n2 ).
o Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of selection sort is O(n2).
o Worst Case Complexity - It occurs when the array elements are required to be
sorted in reverse order. That means suppose you have to sort the array elements in
ascending order, but its elements are in descending order. The worst-case time
complexity of selection sort is O(n2 ).

2. Space Complexity
Space O(1)
Complexity
Stable YES
o The space complexity of selection sort is O(1). It is because, in selection sort, an
extra variable is required for swapping.

Searching

In computer science, a searching is an algorithm for finding an item with specified


properties among a collection of items. The items may be stored individually as records
in a database; or may be elements of a search space defined by a mathematical formula or
procedure, such as the roots of an equation with integer variables; or a combination of the
two, such as the Hamiltonian circuits of a graph.

Searching is the process of finding a given value position in a list of values.


It decides whether a search key is present in the data or not.
It is the algorithmic process of finding a particular item in a collection of items.
It can be done on internal data structure or on external data structure.
Data Structures

Searching Techniques

1. Linear Search or Sequential Search


Linear search is a very simple search algorithm. In this type of search, a sequential search
is made over all items one by one. Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till the end of the data
collection.
Sequential Search or Linear Search
Sequential search is also called as Linear Search.
Sequential search starts at the beginning of the list and checks every element of
the list.
Sequential search compares the element with all the other elements given in the
list. If the element is matched, it returns the value index, else it returns -1.

 The above figure shows how sequential search works.


 It searches an element or value from an array till the desired element or value is
not found.
 If we search the element 20, it will go step by step in a sequence order.
 It searches in a sequence order. Sequential search is applied on the unsorted or
unordered list when there are fewer elements in a list.

Algorithm
Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

 Time Complexity: O(n)O(n)

 Space Complexity: O(1)O(1)


Data Structures

Algorithm:

def linear_search(arr, target): for


i in range(len(arr)):

if arr[i] == target:
return i
return -1

2. Binary Search

Binary search looks for a particular item by comparing the middle most item of the
collection. If a match occurs, then the index of item is returned. If the middle item is
greater than the item, then the item is searched in the sub-array to the left of the middle
item. Otherwise, the item is searched for in the sub-array to the right of the middle item.
This process continues on the sub-array as well until the size of the subarray reduces to
zero.

How Binary Search Works?


For a binary search to work, it is mandatory for the target array to be sorted. We shall learn
the process of binary search with a pictorial example. The following is our sorted array
and let us assume that we need to search the location of value 31 using binary search.

First, we shall determine half of the array by using this formula −


mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.

Now we compare the value stored at location 4, with the value being searched, i.e. 31.
We find that the value at location 4 is 27, which is not a match. As the value is greater
than 27 and we have a sorted array, so we also know that the target value must be in the
upper portion of the array.
Data Structures

We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.

The value stored at location 7 is not a match, rather it is more than what we are looking for.
So, the value must be in the lower part from this location.

Hence, we calculate the mid again. This time it is 5.

We compare the value stored at location 5 with our target value. We find that it is a match.

We conclude that the target value 31 is stored at location 5.


Binary search halves the searchable items and thus reduces the count of comparisons to
be made to very less numbers.
Binary Search complexity

Now, let's see the time complexity of Binary search in the best case, average case, and
worst case. We will also see the space complexity of Binary search.
Data Structures

1. Time Complexity

Case Time Complexity

Best Case O(1)

Average Case O(logn)

Worst Case O(logn)

o Best Case Complexity - In Binary search, best case occurs when the element to
search is found in first comparison, i.e., when the first middle element itself is the
element to be searched. The best-case time complexity of Binary search is O(1).
o Average Case Complexity - The average case time complexity of Binary
search is O(logn).
o Worst Case Complexity - In Binary search, the worst case occurs, when we have
to keep reducing the search space till it has only one element. The worst -case time
complexity of Binary search is O(logn).

2. Space Complexity

Space Complexity O(1)

o The space complexity of binary search is O(1).

Binary Search Algorithm

Step 1 : Find the centre element of array.


centre = first_index + last_index / 2 ;
Step 2 : If centre = element, return 'element found' and index.
Step 3 : if centre > element, call the function with last_index = centre - 1 .
Step 4 : if centre < element, call the function with first_index = centre + 1 .
Step 5 : exit.
Data Structures

Heap Sort

Heapsort is a popular terminology that belongs to the heap data structure family. It
is a comparison-based sorting method based on Binary Heap data structure. In
heapsort, we generally prefer the heapify method to sort the elements.

To learn Heapsort in-depth, we need to learn about arrays and trees first.

The connection between Array Pointers and Tree Elements


A complete binary tree is allowing us to find the children and parents of any node.

The above picture shows the strong relationship between arrays and trees and how
we can sort an array in a form of heap.

What is Heap Data Structure?

Heap is a special data structure. It has a tree structure. A heap data should
follow some properties, then only we can consider it a genuine heap data
structure. The properties are:

 It should be a complete binary tree.

 It should be either max-heap or min-heap. In short, it should follow all the


properties of a binary heap tree.

 Max Heap: Parent is greater than or equal to its children.

 Min Heap: Parent is smaller than or equal to its children.


Data Structures

How to “heapify” a tree?

When it comes to creating a heap tree( Min Heap or Max Heap), the heapify
method is most suitable, because it takes less time as compared to the other
methods like Insert key one by one in the given order.

This is the case of Max heap because in this above picture the parent node is
greater than the child node. And to perform the heapify method, we have to
compare the nodes and swap them if required.

How to heapify root element when its subtrees are already max heaps?

Now we need to heapify the above tree, here we go:


Data Structures

To maintain the max-heap property in a tree where both sub-trees are max-heaps,
we need to run heapify on the root element repeatedly until it is larger than its
children or it becomes a leaf node.

Working of Heap Sort


1. If it is about the max heap, then the highest element is stored at the root node.

2. Swap: Extract the root element and we need to place the last element of the tree
(heap) at the vacant place.

3. Remove: Reduce the size of the heap by 1.

4. Heapify: Heapify the root element again so that we have the highest element at
root.
5. The process is repeated until all the items of the list are sorted.

In the above picture, we are representing all the operations. And we are trying to
balance a tree by heapify method.
Data Structures

Algorithm
Applications of Heap
1. HeapSort(arr)
 Priority Queues
2. BuildMaxHeap(arr)
 Dijkstra’s Algorithm (Shortest
3. for i = length(arr) to 2 Path)

4. swap arr[1] with arr[i]  Heap Sort

5. heap_size[arr] = heap_size[arr] ? 1  Scheduling Algorithms

6. MaxHeapify(arr,1)

7. End

Heap sort complexity

Now, let's see the time complexity of Heap sort in the best case, average case, and worst
case. We will also see the space complexity of Heapsort.

1. Time Complexity
Case Time Complexity

Best Case O(n logn)

Average Case O(n log n)

Worst Case O(n log n)

o Best Case Complexity - It occurs when there is no sorting required, i.e. the array
is already sorted. The best-case time complexity of heap sort is O(n logn).
o Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of heap sort is O(n log n).

o Worst Case Complexity - It occurs when the array elements are required to be
sorted in reverse order. That means suppose you have to sort the array elements in
ascending order, but its elements are in descending order. The worst-case time
complexity of heap sort is O(n log n).

The time complexity of heap sort is O(n logn) in all three cases (best case, average case,
and worst case). The height of a complete binary tree having n elements is logn.

2. Space Complexity: The space complexity of Heap sort is O(1).


Space Complexity O(1)

Stable N0
Data Structures

Applications of Heap

 Priority Queues

 Dijkstra’s Algorithm (Shortest Path)

 Heap Sort

 Scheduling Algorithms

Advantages of Heap Sort


There are some advantages of using heap sort. These are as follows.

1. There is no quadratic worst case at run time.

2. It is also a type of in-place sorting algorithm. So, its space complexity is O(1).

3. In comparison to quick sort, heap sort has a better worst-case complexity. So, its
time complexity is O(log n). The best-case complexity is the same for both quick
sort and heap sort — O(log n).

4. There is no need for extra space, similar to merge sort.

5. The input data being completely or almost sorted doesn’t make the complexities
suffer.
6. The average-case complexity is the same as that of merge sort and quicksort.

Disadvantages of Heap Sort

There are some disadvantages to using heap sort. These are as follows.

1. It is not stable because the operations on the heap can change the relative order of
equal key items. It’s typically an unstable sorting algorithm.

You might also like