Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views32 pages

DS Complete Notes

Data structures are methods for organizing and storing data to perform operations efficiently, categorized into primitive (single value types) and non-primitive (linear and non-linear structures). They can be static (fixed size) or dynamic (variable size), with abstract data types providing high-level descriptions of operations. Asymptotic analysis measures algorithm efficiency using notations like Big O, while specific data structures like arrays and linked lists have distinct characteristics and complexities for various operations.

Uploaded by

aniketdupare1123
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)
13 views32 pages

DS Complete Notes

Data structures are methods for organizing and storing data to perform operations efficiently, categorized into primitive (single value types) and non-primitive (linear and non-linear structures). They can be static (fixed size) or dynamic (variable size), with abstract data types providing high-level descriptions of operations. Asymptotic analysis measures algorithm efficiency using notations like Big O, while specific data structures like arrays and linked lists have distinct characteristics and complexities for various operations.

Uploaded by

aniketdupare1123
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/ 32

Data Structure

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.

Types of Data Structures


1. Primitive Data Structures
holds a single value

Integer

Float

Character

Boolean

2. Non-Primitive Data Structures

Linear Data Structures


data in a sequential manner

Arrays

Linked Lists

Stacks

Queues

Non-Linear Data Structures


data in a non sequential manner

Trees

Graphs

Hash Tables

Data Structures Diagram

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:

Static Data Structures


Static data structures have a fixed size that is determined at compile time.

Memory is allocated at compile time

Size cannot be changed during runtime

Example: Arrays in most programming languages

Dynamic Data Structures


Dynamic data structures can grow or shrink in size during program execution.

Memory is allocated at runtime

Size can be modified during program execution

Examples: Linked lists, dynamic arrays, trees

Abstract Data Type (ADT)


An Abstract Data Type is a high-level description of a data structure and the operations that can be performed on it,
without specifying the implementation details.

Defines data and operations but not implementation

Provides an interface for the data structure

Examples: Stack, Queue, List

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:

1. Big O Notation (O) [Worst Case]


Represents the upper bound or worst-case scenario of an algorithm's growth rate.

2. Omega Notation (Ω) [Best Case]


Represents the lower bound or best-case scenario of an algorithm's growth rate.

3. Theta Notation (Θ) [Average Case]


Represents both the upper and lower bounds, giving a tight bound on the growth rate.

4. Little O Notation (o)


Provides a strict upper bound, tighter than Big O.

5. Little Omega Notation (ω)


Provides a strict lower bound, tighter than Omega.
These notations help in comparing and analyzing the efficiency of algorithms, especially as the input size grows.

Asymptotic Notations Diagram

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.

Common Time Complexities


O1 Constant Time

O(log n) Logarithmic Time

O(n) Linear Time

O(n log n) Linearithmic Time

O(n²) Quadratic Time

O 2ⁿ) Exponential Time

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.

Common Space Complexities


O1 Constant Space:

O(n) Linear Space

O(n²) Quadratic Space

O(log n) Logarithmic Space

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 numbers[5] = {1, 2, 3, 4, 5};

2. Two-Dimensional Array: A table or matrix of elements

int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

3. Three-Dimensional Array: A cube-like structure of elements

int cube[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};

Array Types Diagram

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

Random Access: Elements can be accessed directly using their index

Homogeneous Elements: All elements in an array must be of the same data type

Contiguous Memory: Elements are stored in adjacent memory locations

Address of the ith element in 1D, 2D, and 3D arrays:

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

Always assume size 4 bytes unless specified

Always solve in brackets first

Array starts from index 0

📊 ARRAY TIME COMPLEXITY


1. BASIC OPERATIONS
Operation Average Case Worst Case Example/Note

Access O1 O1 arr[5]

Insert End O1 O1 arr.push(x)

Insert Start O(n) O(n) Need to shift all

Delete End O1 O1 arr.pop()

Delete Start O(n) O(n) Shift remaining

2. SPECIAL OPERATIONS
Operation Average Case Worst Case When Used

Reverse O(n) O(n) In-place

Rotate O(n) O(n) In-place

Find Min/Max O(n) O(n) Single pass

Find Duplicate O(n) O(n) With marking

Space Complexity: O(n)

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.

Types of Linked Lists

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

Linked List Types Diagram

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]

Characteristics of Linked Lists


Dynamic Size: Can grow or shrink in size during execution

Efficient Insertion/Deletion: Adding or removing elements is efficient, especially at the beginning

No Random Access: Elements must be accessed sequentially

More Memory Usage: Requires extra memory for storing pointers

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

Access/Search O(n) O(n) First vs Last element

Insert at Head O1 O1 No traversal needed

Insert at Tail O1* O1* *With tail pointer

Insert at Middle O(n) O(n) Need traversal

Delete from Head O1 O1 Just move head

Delete from Tail O(n) O(n) Need full traverse

Delete from Middle O(n) O(n) Need traversal

Space Complexity: O(n)

2. DOUBLY LINKED LIST OPERATIONS


Operation Time Average) Time Worst) Note

Access/Search O(n) O(n) Same as singly

Insert at Head O1 O1 Update prev ptr

Insert at Tail O1 O1 With tail ptr

Insert at Middle O(n) O(n) After finding pos

Delete from Head O1 O1 Update head

Delete from Tail O1 O1 With tail ptr

Delete from Middle O(n) O(n) After finding node

Space Complexity: O(n)

3. CIRCULAR LINKED LIST OPERATIONS


Operation Time Best) Time Average) Time Worst) Special Case

Access/Search O1 O(n) O(n) Check for loop

Insert O1 O(n) O(n) Update last→first

Delete O1 O(n) O(n) Handle circular ref

Space Complexity: O(n)

4. SPECIAL OPERATIONS
Operation Time Notes

Reverse List O(n) In-place reversal

Find Middle O(n) Slow-Fast pointers

Detect Loop O(n) Floyd's algorithm

Merge Lists O(n+m) n,m = list lengths

Sort List O(n log n) Using Merge Sort

5. COMPARISON WITH ARRAYS


Operation Array Linked List Winner

Access O1 O(n) Array

Insert Start O(n) O1 Linked List

Insert End O1 O1* Tie

Data Structure 7
Delete Start O(n) O1 Linked List

Memory Fixed Dynamic 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.

Operation Best Case Average Case Worst Case

Search O1 O(log n) O(n)

Insert O1 O(log n) O(n)

Delete O1 O(log n) O(n)

Space Complexity O1 O(n) O(n log n)

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

PROCEDURE Push(stack, item)


IF stack is full THEN

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

earch Fin l m nt O(n) (n)

Clear R m v all l m nt O(n) (n)

Clo e Cr at ta y O(n) (n)

i ax Fin min/max l m nt O(n) (n)

Co tai s C l m nt xi t O(n) (n)

ex f Fin l m nt in x O(n) (n)

Space Complexity: O(n)

Applications:
Application Description

Evaluating Postfix Expressions Efficient expression evaluation

Parsing Syntax analysis in compilers

Undo/Redo Functionality Reversible actions in editors

Browser History Navigation management

Real-World Examples

Example Description

Browser Back/Forward Buttons Navigation management

Text Editor Undo/Redo Reversible editing actions

Calculator Expression Evaluation Efficient calculation

Data Structure 9
Queue
Description: A queue is a linear data structure that follows the First-In-First-Out FIFO principle.

Enqueue Operation

PROCEDURE Enqueue(queue, item)


IF queue is full THEN
ERROR: "Queue Overflow"
ELSE
REAR = REAR + 1
queue[REAR] = item
END IF
END PROCEDURE

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)

Remove front queue.dequeue()


Dequeue O1 O1 O1 O(n)
element

Return front queue.peek()


Peek element O1 O1 O1 O(n)

IsEmpty Check if empty O1 O1 O1 O(n) queue.is_empty()

Return element queue.size()


Size O1 O1 O1 O(n)
count

Check element queue.contains(3)


Contains O(n) O(n) O(n) O(n)
exists
IndexOf Find element index O(n) O(n) O(n) O(n) queue.index_of(2)

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

Job Scheduling Fair job management

Print Queue Ordered print management

Network Buffer Data transmission management

Real-World Examples

Example Description

Amazon Order Processing Efficient order management

Google Search Queries Quick query processing

Bank ATM Transactions Secure transactions

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

FIFO order First-In-First-Out order

Circular array implementation Efficient use of memory

Operations on Circular Queue


Front: It is used to get the front element from the Queue.

Rear: It is used to get the rear element from the Queue.

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:

1. Enqueue (Add an item)


If you enqueue element 5 , the queue will look like this:

Front Rear
| |
V V
+-------+ +-------+ +-------+ +-------+ +-------+
| 1 | | 2 | | 3 | | 4 | | 5 |
+-------+ +-------+ +-------+ +-------+ +-------+
^ |
|-------------------------------------------------------|

The Rear pointer moves to index 4, where the element 5 is added.

2. Dequeue (Remove an item)


If you dequeue (remove) the element at the front (which is 1 ), the queue now looks like this:

Front Rear
| |
V V
+-------+ +-------+ +-------+ +-------+ +-------+
| - | | 2 | | 3 | | 4 | | 5 |
+-------+ +-------+ +-------+ +-------+ +-------+
^ |
|-------------------------------------------------------|

The Front pointer moves to index 1.

Checking Full and Empty Queue:


Full Queue The queue is full if:

(rear + 1) % capacity == front

Empty Queue The queue is empty if:

front == rear

Applications of Circular Queue


Memory management

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.

If same priority then arranged with FIFO principle.

Characteristics Description

Priority-based ordering Higher priority items first

Used in scheduling, resource allocation Efficient resource management

Types of Priority Queue


There are two types of priority queue:

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.

Characteristics of a Priority Queue:


Priority-Based Each element in a priority queue has a priority assigned to it. Elements with higher priority are
dequeued before those with lower priority.

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:

Binary Heaps (most common)

Balanced Binary Search Trees

Unordered or Ordered Lists

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.

Change Priority Change the priority of an existing element.

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.

Analysis of complexities using different implementations

Implementation add Remove peek

Linked list O1 O(n) O(n)

Binary heap O(logn) O(logn) O1

Binary search tree O(logn) O(logn) O1

Applications of Priority Queue

App on D p on

1 D Sho P hA o hm Fin rt t at tw nn in a a

2 m A o hm Fin mini m annin t in a a

3 Hu m Co ng D Comp on) ign varia l -l n t c t c a act

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

4. Double-Ended Queue (Deque)


Characteristics Description

Two inputs, two outputs Enqueue/dequeue at both ends

Used in browser history, undo/redo Flexible data structure

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.

Therefore, we can say that there are two types of heaps:

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.

Complexity of Heap Operations

Operation Time Complexity

Insertion O(log n)

Deletion Remove Max/Min) O(log n)

Peek Find Max/Min) O1

Building a Heap O(n)

Heap Sort O(n 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.

Root The top node of a tree.

Leaf A node with no children.

Internal Node A non-leaf node.

Edge The connection between two nodes.

Path A sequence of nodes and edges from one node to another.

Depth The length of the path from the root to a node.

Height The length of the longest path from a node to a leaf.

Subtree A tree formed by a node and its descendants.

Degree The number of children a node has.

Types of Trees:

Binary Tree A tree in which each node has at most two children 0,1 or 2 children).

Properties of a Binary Tree


Property Formula / Description

Max nodes at level i 2i


Max nodes at height h N 2^(h+1) 1

Min nodes at height h h 1

Min height for n nodes h = log2 (n + 1) − 1


Max height for n nodes h=n 1

Diagram of a Complete Binary Tree (Height = 4)

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

Max nodes at height 4 2^ 4 1 15

Min nodes at height 4: h 1 5

Time Complexity:

Binary Tree Search O(n) O(n)

Insertion O(n) O(n)

Deletion O(n) O(n)

Binary Search Tree BST A tree in which each node has at most two children.

Binary Search Tree Search O(log n) O(n)

Insertion O(log n) O(n)

Deletion O(log n) O(n)

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.

Property Formula / Description

Maximum number of nodes 2^(h+1) 1

Minimum number of nodes 2h


Minimum height for n nodes log2 (n + 1) − 1
Maximum height for n nodes log2 n

Full/Strict/Proper Binary Tree A binary tree where every node has 0 or 2 children

Data Structure 18
Property Formula / Description

Number of leaf nodes Equal to the number of internal nodes 1

Maximum number of nodes 2^(h+1) 1

Minimum number of nodes 2h − 1


Minimum height for n nodes log2 (n + 1) − 1
Maximum height for n nodes h = (n 1 /2

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.

A red-black tree satisfies the following properties:

Red/Black Property: Every node is colored, either red or black.

Root Property: The root is black.

Leaf Property: Every leaf NIL is black.

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:

Red-Black Tree Search O(log n) O(log n)

Insertion O(log n) O(log n)

Deletion O(log n) O(log n)

Tree Traversal
Tree traversal is the process of visiting all nodes in a tree in a specific order. The two main types are:

1. Depth-First Traversal (DFT)


Inorder Left, Root, Right) Visits nodes in sorted order (for BSTs).

Preorder Root, Left, Right) Used for copying trees and obtaining prefix expressions.

Postorder Left, Right, Root) Useful for deleting trees and obtaining postfix expressions.

2. Breadth-First Traversal (BFT)


Level Order Visits nodes level by level from top to bottom and left to right.

Complexity
Time Complexity O(n) for all methods (where n is the number of nodes).

Space Complexity:

DFT O(h) (h = height of the tree).

BFT O(w) (w = maximum width of the tree).

Feature Depth-First Traversal DFT Breadth-First Traversal BFT

Method Explores deep before backtracking. Explores level by level.

Implementation Uses recursion or stack. LIFO Uses a queue. FIFO

Types Inorder, Preorder, Postorder. Level Order.

Memory Usage O(h) space (height of tree). O(w) space (width of tree).

Use Cases Searching, copying trees. Shortest path in unweighted graphs.

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)

There are three ways of calculating the hash function:

Division method

Folding method

Mid square 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.

Closed Hashing: It is also known as open addressing. Techniques:

Linear probing

Quadratic probing

Double Hashing technique

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.

Loop An edge that connects a node to itself.

Adjacent Nodes Two nodes that are directly connected by an 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 (or, Adjacency matrix representation)

Linked list representation (or, Adjacency list representation)

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

Breadth-First Search (BFS) and Depth-First Search (DFS)


1. Breadth-First Search (BFS)
Description BFS is an algorithm for traversing or searching through a graph or tree data structure. It starts at the root
(or an arbitrary node) and explores the neighbor nodes at the present depth prior to moving on to nodes at the next
depth level.
Algorithm:

Use a queue to keep track of nodes to visit.

Enqueue the starting node, mark it as visited.

While the queue is not empty:

Dequeue a node, process it.

Enqueue all unvisited adjacent nodes, marking them as visited.

Example:

Graph:
A
/ \
B C
/ \
D E

BFS starting from A: A, B, C, D, E

Complexity:

Time: O V E where V is the number of vertices and E is the number of edges.

Space: O V for the queue.

2. Depth-First Search (DFS)


Description DFS is an algorithm for traversing or searching through a graph or tree data structure by exploring as far
as possible along each branch before backtracking.
Algorithm:

Use a stack (or recursion) to keep track of nodes to visit.

Push the starting node onto the stack, mark it as visited.

While the stack is not empty:

Pop a node, process it.

Push all unvisited adjacent nodes onto the stack, marking them as visited.

Example:

Data Structure 23
Graph:
A
/ \
B C
/ \
D E

DFS starting from A: A, B, D, E, C

Complexity:

Time: O V E where V is the number of vertices and E is the number of edges.

Space: O V in the worst case for the stack (or recursion stack).

Differences between BFS and DFS


Feature BFS DFS

Traversal Order Level by level Deep into the tree/graph first

Data Structure Uses a Queue Uses a Stack or Recursion

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)

What is a Spanning Tree?


A spanning tree is a subgraph of an undirected, connected graph that includes all the vertices and the least possible
number of edges. It consists of n-1 edges, where n is the number of vertices. A spanning tree has the following
properties:

It includes all vertices.

It is acyclic (contains no cycles).

It is connected (removing any single edge would disconnect it).

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.

Applications of Spanning Trees:


Cluster Analysis

Civil Network Planning

Computer Network Routing Protocols

Example of a Spanning Tree:

Data Structure 24
Properties of Spanning Trees:
More than one spanning tree can exist for a connected graph.

A spanning tree does not have any cycles or loops.

A spanning tree is minimally connected; removing an edge will disconnect it.

A spanning tree is maximally acyclic; adding an edge creates a loop.

A spanning tree has \( n-1 \) edges.

What is a Minimum Spanning Tree (MST)?


A minimum spanning tree is a type of spanning tree that has the smallest sum of edge weights. The weight can
represent distance, traffic, or other metrics.

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.

Applications of Minimum Spanning Trees:


Designing water supply networks, telecommunications, and electrical grids.

Finding efficient paths on maps.

Algorithms for Finding Minimum Spanning Trees:


Prim's Algorithm This is a greedy algorithm that starts with an empty tree and gradually adds edges while
minimizing 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:

Start with a chosen vertex.

Add the edge with the smallest weight connecting to the tree.

Repeat until all vertices are included.

Applications: Network design, electrical wiring, creating cycles.


Time Complexity:

Adjacency matrix, Linear Searching: O |V|²)

Adjacency list with binary heap: O |E| log |V|

Adjacency list with Fibonacci heap: O |E| |V| log |V|

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

cost of the MST is AB DE BC CD 1 2 3 4 10.


Concepts:

Spanning Tree A tree that includes all vertices of a graph.

Minimum Spanning Tree MST A spanning tree with the smallest total edge weight.

Algorithm Steps:

Sort edges by weight (ascending).

Initialize an empty MST.

Add edges from the sorted list:

Include the edge if it connects two different trees (no cycles).

Discard the edge if it forms a cycle.

Repeat until you have V 1 edges V is the number of vertices).

Complexity:

Time Complexity O E log V or O V log V

Applications:

Network design (e.g., electrical grids, computer networks).

Searching and Sorting

Searching Algorithms

Algorithm Best Case Average Case Worst Case Time Complexity Space Complexity

Linear Search O1 O(n) O(n) O(n) O1

Binary Search O1 O(log n) O(log n) O(log n) O1

Sorting Algorithms

Algorithm Best Case Average Case Worst Case Time Complexity Space Complexity Stability

Bubble Sort O(n) O(n2 ) O(n2 ) O(n2 ) O(1) Stable

Selection Sort O(n2 ) O(n2 ) O(n2 ) O(n2 ) O(1) Unstable


2 2 2
Insertion Sort O(n) O(n ) O(n ) O(n ) O(1) Stable

Merge Sort O(nlogn) O(nlogn) O(nlogn) O(nlogn) O(n) Stable

Quick Sort O(nlogn) O(nlogn) O(n2 ) O(nlogn) O(logn) Unstable

Heap Sort O(nlogn) O(nlogn) O(nlogn) O(nlogn) O(1) Unstable

Radix Sort Ω(n + k) θ(nk) O(nk) O(nk) O(n + k) Stable

Data Structure 27
Counting Sort O(n + k) O(n + k) O(n + k) O(n + k) O(n + k) Stable

Bucket Sort O(n + k) O(n + k) O(n2 ) O(n + k) O(n + k) Stable

Linear Search:

Binary Search:

Multiple-Choice Questions on Data Structures and Algorithms (DSA)


Which of the following is a linear data structure?

A Tree

B Graph

Data Structure 28
C Array

D Hash Table

What is the time complexity of accessing an element in an array?

A O(n)

B O(log n)

C O1

D O(n^2)

Which of the following data structures can be used to implement a recursion?

A Stack

B Queue

C Array

D Hash Table

In a singly linked list, which pointer is used to traverse the list?

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

What does the term “Big Oˮ notation represent?

A The memory used by the algorithm

B The runtime efficiency of the algorithm

C The type of data structure used

D None of the above

Which data structure is best suited for implementing a queue?

A Stack

B Array

C Linked List

D Hash Table

What is a characteristic of a binary search tree BST ?

A Every node has at most two children.

B Left child values are greater than the nodeʼs value.

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

In a hash table, what is a collision?

A When two keys hash to the same index

B When the hash table is full

C When data is lost

D When two hash functions are used

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

Which of the following data structures is not dynamic in size?

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

In which of the following data structures is searching the least efficient?

A Array

B Linked List

C Binary Search Tree

D Hash Table

What is a disadvantage of a linked list compared to an array?

A Dynamic size

B Easy insertion/deletion

C Random access to elements

D Flexibility in memory usage

Which data structure is used for implementing Depth-First Search DFS ?

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

Which of the following is a characteristic of a circular queue?

A It does not eliminate any element

B It has a fixed capacity

C It allows efficient utilization of space

D Both B and C

What is the purpose of the "enqueue" operation in a queue?

A Remove an element

B Add an element

C Peek at the front element

D Check if the queue is empty

Answers
C Array

C O1

A Stack

A Head Pointer

A 2

B Quick Sort

B The runtime efficiency of the algorithm

C Linked List

A Every node has at most two children.

B O(log n)

A When two keys hash to the same index

A Dijkstra's Algorithm

A Array

C Counting Sort

B Linked List

C Random access to elements

B Stack

A log(n)

D Both B and C

B Add an element

Data Structure 31

You might also like