DS Complete Notes
DS Complete Notes
A data structure is a way of organizing and storing data to efficiently perform operations on it. It is not a programming
language. It is a set of algorithms.
Integer
Float
Character
Boolean
Arrays
Linked Lists
Stacks
Queues
Trees
Graphs
Hash Tables
graph TD
A[Data Structures] --> B[Primitive] A -->
C[Non-Primitive]
B --> D[Integer] B -->
E[Float]
B --> F[Character] B -->
G[Boolean]
C --> H[Linear]
C --> I[Non-Linear] H -->
J[Arrays]
H --> K[Linked Lists] H -->
L[Stacks]
H --> M[Queues] I -->
N[Trees] I -->
O[Graphs]
I --> P[Hash Tables]
Data Structure 1
Static vs Dynamic Data Structures
Data structures can be categorized as static or dynamic based on their memory allocation:
ADTs are implemented using concrete data structures, but the user of an ADT doesn't need to know the underlying
implementation to use it effectively.
ADT tells what is to be done and Data Structures tells how it is to be done.
Asymptotic Analysis
Asymptotic analysis uses different notations to describe the time and space complexity of algorithms. The main types
of notations are:
graph TD
A[Asymptotic Notations] --> B[Big O Notation O]
A --> C[Omega Notation Ω]
A --> D[Theta Notation Θ]
Data Structure 2
A --> E[Little O Notation o]
A --> F[Little Omega Notation ω]
B --> G[Upper Bound]
C --> H[Lower Bound]
D --> I[Tight Bound]
E --> J[Strict Upper Bound]
F --> K[Strict Lower Bound]
Time Complexity
Time complexity is a measure of the amount of time an algorithm takes to run as a function of the length of the input.
It's typically expressed using Big O notation.
Space Complexity
Space complexity is a measure of the amount of memory an algorithm uses relative to the size of its input. Like time
complexity, it's often expressed using Big O notation.
Auxiliary Space
Auxiliary space refers to the extra space used by an algorithm, not including the space taken by the inputs. It's
sometimes used to give a more precise measure of the algorithm's efficiency in terms of memory usage.
Note: Pointer: A pointer is a variable that stores the memory address of another variable, allowing direct manipulation
of memory and efficient handling of data structures.
int x = 5;
int *ptr = &x;
*ptr = 10;
printf("%d", x); // Outputs: 10
Structures: A structure is a user-defined data type that groups related variables of different types under a single name,
e.g., struct Person { char name[50]; int age; float height; };
Arrays
An array is a linear data structure that stores elements of the same data type in contiguous memory locations.
They are finite, ordered, homogenous.
Data Structure 3
Types of Arrays with Examples
1. One-Dimensional Array: A simple list of elements
int cube[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
graph TD
A[Types of Arrays] --> B[One-Dimensional]
A --> C[Two-Dimensional]
A --> D[Three-Dimensional]
B --> E[Linear List]
C --> F[Table/Matrix]
D --> G[Cube Structure]
Characteristics of Arrays
Fixed Size: In most languages, arrays have a fixed size once declared
Homogeneous Elements: All elements in an array must be of the same data type
1D ARRAY
Formula = BA + (i × size)
where: i = index, size = 4 bytes
Example:
Base Address = 100
Find A[2]
= 100 + (2 × 4)
= 100 + 8
= 108
2D ARRAY
Formula = BA + (i × N + j) × size
where: i = row, j = column, N = total columns
Example:
Base Address = 200
Array[3][4]
Find A[1][2]
Data Structure 4
= 200 + (1 × 4 + 2) × 4
= 200 + 6 × 4
= 200 + 24
= 224
3D ARRAY
Formula = BA + (i × M × N + j × N + k) × size
where: i = layer, j = row, k = column
M = total rows, N = total columns
Example:
Base Address = 300
Array[2][3][4]
Find A[1][1][2]
= 300 + (1 × 3 × 4 + 1 × 4 + 2) × 4
= 300 + (12 + 4 + 2) × 4
= 300 + 18 × 4
= 300 + 72
= 372
🎯 EXAM TIPS
Access O1 O1 arr[5]
2. SPECIAL OPERATIONS
Operation Average Case Worst Case When Used
Linked Lists
A linked list is a linear data structure where elements are stored in nodes. Each node contains a data field and a
reference (or link) to the next node in the sequence.
Data Structure 5
1. Singly Linked List: Each node has data and a pointer to the next node
struct Node {
int data;
struct Node* next;
};
2. Doubly Linked List: Each node has data and pointers to both the next and previous nodes
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
3. Circular Linked List: The last node points back to the first node, forming a circle
graph TD
A[Types of Linked Lists] --> B[Singly Linked List]
A --> C[Doubly Linked List]
A --> D[Circular Linked List]
B --> E[Next Pointer]
C --> F[Next and Previous Pointers]
D --> G[Last Node Points to First]
Linked lists are particularly useful when the size of the data is unknown in advance or when frequent insertions and
deletions are required.
Data Structure 6
📝 LINKED LIST COMPLEXITY GUIDE
1. SINGLY LINKED LIST OPERATIONS
Operation Time Average) Time Worst) Example
4. SPECIAL OPERATIONS
Operation Time Notes
Data Structure 7
Delete Start O(n) O1 Linked List
Arrays:
- Direct access O(1)
- Contiguous memory
- Fixed size
Linked Lists:
- Sequential access O(n)
- Scattered memory
- Dynamic size
Skip List
A Skip List is a probabilistic data structure that combines the benefits of arrays and linked lists to achieve efficient
search, insertion, and deletion operations.
Example:
Level 2 3 12 21
Level 1 3 7 12 19 25
Level 0 3 6 7 9 12 17 19 21 25 26
Stack
Definition: A stack is a linear data structure that follows the Last-In-First-Out LIFO principle.
Push Operation
Data Structure 8
ERROR: "Stack Overflow"
ELSE
TOP = TOP + 1
stack[TOP] = item
END IF
END PROCEDURE
Pop Operation
PROCEDURE Pop(stack)
IF stack is empty THEN
ERROR: "Stack Underflow"
ELSE
item = stack[TOP]
TOP = TOP - 1
RETURN item
END IF
END PROCEDURE
Variables:
stack : the stack data structure
item : the element to be pushed or popped
TOP : the index of the top element in the stack
pe on D p on A C se W st C se mple
Push l m nt t t O 1
Po R m v t l m nt O 1
Pee R t rn t l m nt O 1
sEm t C i m ty O 1
i e R t rn l m nt nt O 1
Applications:
Application Description
Real-World Examples
Example Description
Data Structure 9
Queue
Description: A queue is a linear data structure that follows the First-In-First-Out FIFO principle.
Enqueue Operation
Dequeue Operation
PROCEDURE Dequeue(queue)
IF queue is empty THEN
ERROR: "Queue Underflow"
ELSE
item = queue[FRONT]
FRONT = FRONT + 1
RETURN item
END IF
END PROCEDURE
Variables:
queue : the queue data structure
item : the element to be enqueued or dequeued
FRONT : the index of the front element in the queue
REAR : the index of the rear element in the queue
Space
Operation Description Best Case Average Case Worst Case Example
Complexity
Enqueue Add element to end O1 O1 O1 O(n) queue.enqueue(5)
Data Structure 10
Remove all queue.clear()
Clear O(n) O(n) O(n) O(n)
elements
Clone Create queue copy O(n) O(n) O(n) O(n) queue.clone()
Applications
Application Description
Real-World Examples
Example Description
Types of Queues
1. Simple Queue
Ch e s D p on
or er Fir t In Fir t O t r r
ei ut o e t t En at r ar, at r nt
2. Circular Queue
A circular queue is similar to a linear queue as it is also based on the FIFO First In First Out) principle except that the
last position is connected to the first position in a circular queue that forms a circle. It is also known as a Ring Buffer.
Characteristics Description
Data Structure 11
enQueue(value): This function is used to insert the new value in the Queue. The new element is always inserted
from the rear end.
deQueue(): This function deletes an element from the Queue. The deletion in a Queue always takes place from the
front end.
Queue Operations:
Front Rear
| |
V V
+-------+ +-------+ +-------+ +-------+ +-------+
| 1 | | 2 | | 3 | | 4 | | 5 |
+-------+ +-------+ +-------+ +-------+ +-------+
^ |
|-------------------------------------------------------|
Front Rear
| |
V V
+-------+ +-------+ +-------+ +-------+ +-------+
| - | | 2 | | 3 | | 4 | | 5 |
+-------+ +-------+ +-------+ +-------+ +-------+
^ |
|-------------------------------------------------------|
front == rear
CPU Scheduling
Traffic system
3. Priority Queue
A priority queue is an advanced data structure in which each element is associated with a priority. In a priority queue,
elements are removed based on their priority rather than their order in the queue. The main operations of a priority
Data Structure 12
queue include inserting an element and removing the element with the highest (or lowest) priority. Elements are arranged in ascending
or descending order.
Characteristics Description
Ascending order priority queue: In ascending order priority queue, a lower priority number is given as a higher
priority in a priority. For example, we take the numbers from 1 to 5 arranged in an ascending order like 1,2,3,4,5;
therefore, the smallest number, i.e., 1 is given as the highest priority in a priority queue.
Descending order priority queue: In descending order priority queue, a higher priority number is given as a higher
priority in a priority. For example, we take the numbers from 1 to 5 arranged in descending order like 5, 4, 3, 2, 1;
therefore, the largest number, i.e., 5 is given as the highest priority in a priority queue.
Order of Deletion Unlike standard queues (which follow First-In-First-Out, or FIFO , priority queues follow specific
rules based on priority. An element with a higher priority will be served before an element with lower priority.
Implementation Priority queues can be implemented using various data structures, such as:
Basic Operations:
Insert (enqueue) Add an element to the priority queue with a specified priority.
Remove (dequeue) Remove and return the element with the highest priority.
Peek Return the element with the highest priority without removing it from the queue.
1, 3, 4, 8, 14, 22
Data Structure 13
All the values are arranged in ascending order. Now, we will observe how the priority queue will look after performing
the following operations:
poll(): This function will remove the highest priority element from the priority queue. In the above priority queue,
the '1' element has the highest priority, so it will be removed from the priority queue.
add(2): This function will insert '2' element in a priority queue. As 2 is the smallest element among all the numbers
so it will obtain the highest priority.
poll(): It will remove '2' element from the priority queue as it has the highest priority queue.
add(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it will obtain the third highest
priority in a priority queue.
App on D p on
1 D Sho P hA o hm Fin rt t at tw nn in a a
He p So E i i nt tin al it m in i it
5 pe Sy m
o S he u ng l ta a n i it
Lo d n ng i tri t l a ac m lti l c
Heap
A heap is a tree-based data structure that forms a complete binary tree, and satisfies the heap property. If A is a parent
node of B, then A is ordered with respect to the node B for all nodes A and B in a heap. It means that the value of the
parent node could be more than or equal to the value of the child node, or the value of the parent node could be less
than or equal to the value of the child node.
Max heap: The max heap is a heap in which the value of the parent node is greater than the value of the child
nodes.
Min heap: The min heap is a heap in which the value of the parent node is less than the value of the child nodes.
Both the heaps are the binary heap, as each has exactly two child nodes.
Data Structure 14
Max-Heap Diagram
A Max-Heap ensures that for any given node, the value of that node is greater than or equal to the values of its
children. Here is an example:
10
/ \\
9 8
/ \\ / \\
7 6 5 4
In this Max-Heap, the root node 10 is the largest element. Each parent node 10, 9, 8 is greater than or equal to its
children.
Min-Heap Diagram
A Min-Heap ensures that for any given node, the value of that node is less than or equal to the values of its children.
Here is an example:
1
/ \\
3 2
/ \\ / \\
7 6 5 4
In this Min-Heap, the root node 1 is the smallest element. Each parent node 1, 3, 2 is less than or equal to its
children.
Insertion O(log n)
Applications of Heaps
Priority Queues Heaps can efficiently manage priorities in applications like scheduling.
Heap Sort An O(n log n) sorting algorithm that uses the heap data structure.
Graph Algorithms Dijkstra's shortest path algorithm and Prim's algorithm for Minimum Spanning Tree often utilize
heaps.
Tree
A tree is a nonlinear hierarchical data structure that consists of nodes connected by edges.
Data Structure 15
Node An element of a tree.
Types of Trees:
Binary Tree A tree in which each node has at most two children 0,1 or 2 children).
Data Structure 16
graph TB
1[1]
1 --> 2[2]
1 --> 3[3]
2 --> 4[4]
2 --> 5[5]
3 --> 6[6]
3 --> 7[7]
4 --> 8[8]
4 --> 9[9]
5 --> 10[10]
5 --> 11[11]
6 --> 12[12]
6 --> 13[13]
7 --> 14[14]
7 --> 15[15]
In this example:
Height h 4
Time Complexity:
Binary Search Tree BST A tree in which each node has at most two children.
Balanced Tree / AVL Tree: A tree in which each node has at most two children. Has both properties of binary tree
as well as the binary search tree.
AVL tree is a self-balancing binary search tree in which each node maintains extra information called a balance
factor whose value is either 1, 0 or 1.
Data Structure 17
AVL ee ar O(l g n) (l n)
In rti n O(l g n) (l n)
l ti n O(l g n) (l n)
Complete Binary Tree A binary tree in which all levels are completely filled except possibly for the last level, which
is filled from left to right.
Full/Strict/Proper Binary Tree A binary tree where every node has 0 or 2 children
Data Structure 18
Property Formula / Description
B Tree B-tree is a special type of self-balancing search tree in which each node can contain more than one key
and can have more than two children. Max of m children.
MIN leaf node has 0 children, root has min 2 children and internal node has m/2 children.
Eg: m=5, 5 children and internal node can max 3 children.
Red-Black Tree A balanced binary search tree with an extra bit for denoting the color of a node, ensuring that the
tree remains approximately balanced during insertions and deletions. Max of 2 rotations are required.
Red Property: If a red node has children then, the children are always black.
Depth Property: For each node, any simple path from this node to any of its descendant leaf has the same black-
depth (the number of black nodes).
Data Structure 19
Time Complexity:
Tree Traversal
Tree traversal is the process of visiting all nodes in a tree in a specific order. The two main types are:
Preorder Root, Left, Right) Used for copying trees and obtaining prefix expressions.
Postorder Left, Right, Root) Useful for deleting trees and obtaining postfix expressions.
Complexity
Time Complexity O(n) for all methods (where n is the number of nodes).
Space Complexity:
Memory Usage O(h) space (height of tree). O(w) space (width of tree).
Node Visit Order Depth-first (deep branches first). Level-order (top to bottom).
Hash Table
A Hash table is a data structure that stores some information, and the information has basically two main components,
i.e., key and value.
Data Structure 20
Hash(key)= index;
Drawback of Hash function
A Hash function assigns each value with a unique key. Sometimes hash table uses an imperfect hash function that
causes a collision because the hash function generates the same key of two different values.
Hashing
Hashing is one of the searching techniques that uses a constant time. The time complexity in hashing is O 1 . The main
idea behind the hashing is to create the (key/value) pairs. Index = hash(key)
Division method
Folding method
Collision
When the two different values have the same value, then the problem occurs between the two values, known as a
collision.
The following are the collision techniques:
Open Hashing: It is also known as closed addressing. Also known as chaining technique.
Linear probing
Quadratic probing
Graph
A graph can be defined as group of vertices and edges that are used to connect these vertices.
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.
Data Structure 21
Path A sequence of nodes that connects an initial node to a terminal node.
Closed Path A path where the starting node and the ending node are the same.
Simple Path A path where all nodes are distinct, except for the starting and ending nodes being the same.
Cycle A path that does not repeat any edges or vertices, except for the first and last vertices being the same.
Connected Graph A graph where there is a path between every pair of vertices, meaning there are no isolated
nodes.
Complete Graph A graph where every node is connected to every other node; Contains n(n − 1)/2 edges.
Weighted Graph A graph where each edge has a weight or cost associated with it, indicating the cost to move
from one node to another.
Digraph Directed Graph) A graph where each edge has a direction, meaning you can only move along the
direction specified by the edge.
Degree of the Node The number of edges connected to a node. A node with no connections (degree 0 is called
an isolated node.
Graph representation
There are two ways to store Graphs into the computer's memory:
Sequential representation
In sequential representation, there is a use of an adjacency matrix to represent the mapping between vertices and
edges of the graph.
Data Structure 22
Linked list representation
Example:
Graph:
A
/ \
B C
/ \
D E
Complexity:
Push all unvisited adjacent nodes onto the stack, marking them as visited.
Example:
Data Structure 23
Graph:
A
/ \
B C
/ \
D E
Complexity:
Space: O V in the worst case for the stack (or recursion stack).
Space Generally higher O V due to Can be better O V or O(h) for recursion, where h is the height of the
Complexity queue tree
Usage Finding shortest path, level order Topological sorting, pathfinding in puzzles
Completeness Complete for finite graphs Complete for finite graphs (if backtracking is used)
A complete undirected graph with n vertices can have n^{n-2} spanning trees. For example, if n 5, the maximum
number of spanning trees would be 5^ 5 2 125.
Data Structure 24
Properties of Spanning Trees:
More than one spanning tree can exist for a connected graph.
Data Structure 25
Example of a Minimum Spanning Tree:
From a graph with weighted edges (where the total weight is 16 , a minimum spanning tree is selected from the
possible spanning trees to minimize the total weight.
Kruskal's Algorithm This is another greedy approach that sorts the edges by weight and adds them one by one,
ensuring that no cycles are formed.
Prim's Algorithm
Definition: Prim's Algorithm is a greedy method for finding the Minimum Spanning Tree MST of a connected,
undirected graph, ensuring the minimum sum of edge weights.
Steps:
Add the edge with the smallest weight connecting to the tree.
Prim's algorithm efficiently constructs the MST by progressively adding the lowest-weight edges without forming
cycles.
Kruskal's Algorithm
The weight of the edges of the above graph is given in the below table -
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
Data Structure 26
Now, sort the edges given above in the ascending order of their weights.
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Minimum Spanning Tree MST A spanning tree with the smallest total edge weight.
Algorithm Steps:
Complexity:
Applications:
Searching Algorithms
Algorithm Best Case Average Case Worst Case Time Complexity Space Complexity
Sorting Algorithms
Algorithm Best Case Average Case Worst Case Time Complexity Space Complexity Stability
Data Structure 27
Counting Sort O(n + k) O(n + k) O(n + k) O(n + k) O(n + k) Stable
Linear Search:
Binary Search:
A Tree
B Graph
Data Structure 28
C Array
D Hash Table
A O(n)
B O(log n)
C O1
D O(n^2)
A Stack
B Queue
C Array
D Hash Table
A Head Pointer
B Tail Pointer
C Middle Pointer
D End Pointer
What is the maximum number of children a binary tree node can have?
A 2
B 3
C Variable
D 4
Which of the following sorting algorithms has the best average-case time complexity?
A Bubble Sort
B Quick Sort
C Insertion Sort
D Selection Sort
A Stack
B Array
C Linked List
D Hash Table
C Right child values are less than or equal to the nodeʼs value.
Data Structure 29
D All of the above.
What is the time complexity of searching for an element in a balanced binary search tree?
A O(n)
B O(log n)
C O(n log n)
D O1
Which of the following algorithms can be used to find the shortest path in a graph?
A Dijkstra's Algorithm
B Kruskal's Algorithm
C Merge Sort
D Binary Search
A Array
B Linked List
C Stack
D Queue
Which algorithm is used to sort elements in O(n) time under certain conditions?
A Merge Sort
B Heap Sort
C Counting Sort
D Quick Sort
A Array
B Linked List
D Hash Table
A Dynamic size
B Easy insertion/deletion
A Queue
B Stack
C Array
D Hash Table
Data Structure 30
What is the height of a balanced binary tree with n nodes?
A log(n)
B n
C n^2
D 2^n
D Both B and C
A Remove an element
B Add an element
Answers
C Array
C O1
A Stack
A Head Pointer
A 2
B Quick Sort
C Linked List
B O(log n)
A Dijkstra's Algorithm
A Array
C Counting Sort
B Linked List
B Stack
A log(n)
D Both B and C
B Add an element
Data Structure 31