0% found this document useful (0 votes)
5 views18 pages

Data Structure Short Notes

The document provides an overview of various data structures, including arrays, linked lists, stacks, queues, trees, and heaps, detailing their properties, operations, and time complexities. It discusses specific types of matrices such as sparse, lower triangular, upper triangular, and tridiagonal matrices, as well as different tree structures like binary trees, AVL trees, and heaps. Additionally, it covers the concepts of insertion, deletion, and traversal methods for these 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)
5 views18 pages

Data Structure Short Notes

The document provides an overview of various data structures, including arrays, linked lists, stacks, queues, trees, and heaps, detailing their properties, operations, and time complexities. It discusses specific types of matrices such as sparse, lower triangular, upper triangular, and tridiagonal matrices, as well as different tree structures like binary trees, AVL trees, and heaps. Additionally, it covers the concepts of insertion, deletion, and traversal methods for these 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/ 18

DATA STRUCTURE

GATE फर्रे

Array Sparse Matrix


• Array: A collection of homogeneous elements A sparse matrix is a matrix in which most of the
stored in contiguous memory. elements are zero.
• Indexing: In C, starts from 0. If the number of zero elements > number of non-zero
int arr[10]; → arr[0] is the first element, arr[9] is elements, the matrix is sparse.
the last.

o
Types:
1D Array: Linear structure, e.g. int a [10];
Lower Triangular Matrix
o 2D Array: Matrix-like, e.g. int a [2][3]; A lower triangular matrix is a square matrix where
int a[2][3] = { {0,0,0}, {1,1,1} }; all elements above the main diagonal are zero.
o Multi-dimensional: int a [3][2][4]; (3D array) A[i][j] = 0 for all i < j
Must be square (n × n).
Memory & Access Non-zero elements are on or below the main
• Base Address: Address of the first element.
diagonal.
• Address Calculation:
o 1D: a[k] = base + k * w Upper Triangular Matrix
o 2D Row-Major: a[i][j] = base + ((i * n) + j) An upper triangular matrix is a square matrix in
*w which all elements below the main diagonal are
o 2D Column-Major: a[i][j] = base + ((i) + j * zero.
m) * w A[i][j] = 0 for all i > j
Where: Must be a square matrix (n × n)
o m = rows, n = cols, w = size of element Only elements on or above the main diagonal can be
Fixed size (static memory allocation) non-zero
Lower Bound (L.B): 0 in C
Upper Bound (U.B): n-1 Elements below the diagonal are always zero
Range: U.B - L.B + 1
operation on the 1d array.

Operation Time Explanation


Complexity
Access O (1) Direct access using index:
arr[i] → CPU calculates the
address directly using
formula Base + i * size

Insertion O (n) If insertion is at beginning


or middle, all subsequent
elements must be shifted
right

Deletion O (n) If deleting from start or


middle, elements must be
shifted left to fill the gap

Page No:- 01
DATA STRUCTURE
GATE फर्रे

Tridiagonal matrix
A tridiagonal matrix is a square matrix where non-
zero elements exist only on the main diagonal, just
above it, and just below it.
A[i][j] ≠ 0 only if i == j, i == j+1, or i == j–1
Else, A[i][j] = 0
Sum of all the element is 3n-2.

A linked list is a linear data structure where elements


(called nodes) are stored in non-contiguous memory
locations and connected using pointers.
Each node contains:
• Data
• Pointer (next) to the next node
Advantages:
Node structure in the C • Dynamic size (unlike arrays)
struct Node { • Efficient insertions/deletions (at
int data; beginning/middle)
struct Node* next; Disadvantages:
}; • No random access (O(n) access time)
• Extra memory for pointers

Stack
A stack is a linear data structure that follows the LIFO
principle
Last In, First Out
The last inserted element is the first to be removed.

Page No:- 02
DATA STRUCTURE
GATE फर्रे

struct Stack {
int arr [10];
int top;
};
arr []: stores the stack elements
Operation Description Time top: points to the topmost element (initially -1)
Complexity
push(x) Inserts element x at O(1)
the top
pop() Removes and returns O(1)
top element
peek() / Returns top element O(1)
top() without removing
Queue
A Queue is a linear data structure that follows the
isEmpty() Checks if the stack is O(1)
FIFO principle:
empty First In, First Out
The first element inserted is the first to be removed.
Real-World Examples:
• Ticket line
Implementation Methods: • Print queue
1. Using Array (Fixed size, static memory) CPU task scheduling
2. Using Linked List (Dynamic size)

Applications of Stack:
• Expression Evaluation & Conversion

(Infix ↔ Postfix)
• Balancing symbols (brackets,
parentheses)
• Function call tracking (recursion)
• DFS traversal (graph)
• Undo functionality
• Backtracking (like maze, Sudoku)
• Number of possible stack permutations
2𝑛𝑛𝐶𝐶𝑛𝑛
=
𝑛𝑛+1

Page No:- 03
DATA STRUCTURE
GATE फर्रे

Operation Description Time Tree Type Description


Complexity
Binary Tree Each node has ≤ 2 children
enqueue(x) Insert O (1) (array
element x at or LL) Full Binary All non-leaf nodes have 2
the rear Tree children
dequeue() Remove O (1)
Complete All levels filled except possibly
and return
Binary Tree the last (left to right)
element
from front Perfect Binary All internal nodes have 2
Tree children & all leaves are at the
same level
• Type • Description Balanced Tree Height ≈ log(n), e.g. AVL tree
• Simple • Basic FIFO queue
Binary Search Left < root < right
Queue (insertion at rear, deletion Tree (BST)
at front)
• Circular • Last position connects AVL Tree Height-balanced BST
Queue back to first (solves Heap Complete binary tree (used in
overflow in array) PQ)
• Deque • Insertion/deletion
(Double- possible from both ends B-Tree / B+ Multi-way trees for disk-based
Tree search
Ended
Queue)
• Priority • Elements served based on
Queue priority, not position
Priority queue
In a simple/linear queue using array, after a few A priority queue is a type of abstract data structure
enqueue and dequeue operations: in which each element is associated with a priority,
• The front moves ahead and elements are served based on their priority, not
• But rear reaches the end of array just insertion order.
• Even though free space exists at the beginning,
we can't use it
This leads to a false overflow.
Tree data Structure
A tree is a non-linear, hierarchical data structure
Solution:
consisting of nodes, with a single root node and zero
In a circular queue, we connect the rear back to
or more child nodes, forming a parent-child
front, forming a circle.
relationship.

Condition Formula

Empty front == -1

Full (rear + 1) % SIZE == front

Enqueue rear = (rear + 1) % SIZE

Dequeue front = (front + 1) % SIZE

Page No:- 04
DATA STRUCTURE
GATE फर्रे

Term Meaning Binary tree


Root Topmost node (no parent)
Node Element in the tree
Edge Connection between nodes
Parent Node with children
Child Node with a parent
Leaf Node with no children
Degree Number of children of a node
Height Max level from root to leaf
• Maximum Nodes in a Binary Tree of Height
Depth Distance from root to a node
'h' = 2ℎ+1 − 1
Subtree Tree formed from any node and its • Minimum nodes in a Binary tree= h+1
descendants • The minimum possible height for N nodes
is ⌊𝐥𝐥𝐥𝐥𝐥𝐥 𝟐𝟐 N⌋
• Total number of unlabelled binary tree with n
2𝑛𝑛𝐶𝐶𝑛𝑛
node =
𝑛𝑛+1
2𝑛𝑛𝐶𝐶𝑛𝑛
• Total number of labelled binary tree = ×
𝑛𝑛+1
𝑛𝑛!
• Total number of the binary tree with given
2𝑛𝑛𝐶𝐶𝑛𝑛
inorder/ preorder/ postorder =
𝑛𝑛+1
• Number of the tree with inorder+ preorder =1,
this is unique
• Number of the tree with inorder + postorder =
1, this is unique
• Number of the tree with preorder + postorder
= many possible

K-ary Tree:
A K-ary tree is a tree in which every internal node has
either 0 or exactly K children.
Let:
• N = Total number of nodes
• L = Number of leaf nodes
• I = Number of internal nodes
• K = Maximum number of children per internal
node
Relationship:
Each internal node has exactly K children:
N=K⋅I+1
Also, total number of nodes is the sum of internal and
leaf nodes:
N=L+ I
So, equating both expressions for N
L+I=K⋅I+1

Page No:- 05
DATA STRUCTURE
GATE फर्रे

Complete Binary tree


This is the binary tree which is filled at second last
level, and the insertion happens from the left to right.
For complete binary tree
• Minimum number of nodes is 2ℎ
• Maximum number of nodes is = 2ℎ+1 − 1 Three Cases of Deletion:
This is not the CBT, because it doesn’t follow 1. Case 1: Node has no children (Leaf
the properties.
Node)
o Simply remove the node.
o No tree structure change.
2. Case 2: Node has one child
o Replace the node with its only child.
o Maintain the link with the parent.
3. Case 3: Node has two children
Binary Search Tree o Replace the node with its:
Left < root < right  Inorder Successor (smallest in
Every structure of the n node has unique binary search right subtree) or
tree.  Inorder Predecessor (largest
i.e. if we have 3 keys, then then we have the 5 in left subtree)
structures o Then delete the
successor/predecessor recursively.

Time Complexity:

• Best/Average Case: O(logn)— for balanced


BST
• Worst Case: O(n) — for skewed BST

This are the structure with five nodes. Now we have


the only 1 binary search tree with every structure.
2𝑛𝑛𝐶𝐶𝑛𝑛
So total binary search tree with n keys =
𝑛𝑛+1
! Binary search tree is not the complete binary tree.
Insertion in a BST
Average case, O(logn)
Worst case, O(n)

Page No:- 06
DATA STRUCTURE
GATE फर्रे

AVL Tree Inorder


Left Subtree → Node → Right Subtree
• A self-balancing binary search tree (BST) In BST, it gives sorted order of elements.
where the difference in heights of the left
and right subtrees (called balance factor)
of every node is -1, 0, or +1. inorder(node) {
Balance Factor: if (node == NULL) return;
Balance Factor (BF)=Height of Left Subtree−Height inorder(node->left);
of Right Subtree visit(node);
• Valid values: -1, 0, +1 inorder(node->right);
}
• If the balance factor becomes less than -1
or more than +1, rotation is needed to
restore balance.
Properties:
Height of AVL Tree: O(logn) Inorder: 20 10 30
Search/Insert/Delete Time Complexity: O(logn)
space Complexity: O(n)
minimum number of the nodes in the AVL tree n(h)
= n(h-1) + n(h-2) +1
where the n(h)= number of the node at height h
LL, RR are single rotations
LR, RL are double rotations.
We check the balance factor from bottom to top, if
we find any node not following the properties then
we do the rotations.
Preorder
Node → Left Subtree → Right Subtree
Tree traversal
preorder(node) {
if (node == NULL) return;
visit(node);
preorder(node->left);
preorder(node->right);
}

Preorder: 10 20 30

Page No:- 07
DATA STRUCTURE
GATE फर्रे

Root = Minimum element

Postorder
Left Subtree → Right Subtree → Node

postorder(node) {
if (node == NULL) return;
postorder(node->left);
postorder(node->right);
visit(node);
}

Postorder: 20 30 10

Heap
A Heap is a special Complete Binary Tree where
every level is completely filled except possibly the last
level, and nodes are as far left as possible.

Min Heap

The value of each node is less than or equal to its


children.
|𝒂𝒂𝒌𝒌 | < | left tree, right tree|

Page No:- 08
DATA STRUCTURE
GATE फर्रे

Max Heap Applications:


The value of each node is greater than or equal to its
children.
|𝒂𝒂𝒌𝒌 | > | left tree, right tree|
• Priority Queue
Root = Maximum element • Heap Sort
• Scheduling algorithms
• Graph algorithms

• Access root (min/max): O(1)


• Inserting an element into the heap requires
O(log n) time.
• If we insert n elements one by one, the total
time will be O(n log n).
• If we build a heap and then heapify it, the
total time complexity is O(n).
• Deletion of an element from the heap takes
O(log n) time.
• Searching in a heap takes O(n) time, as it
is not a sorted structure.
• In a Min Heap, the maximum element will
be found in the leaf nodes, so the time
complexity to find it is O(n).
• The number of leaf nodes in a heap is
ceil(n/2).
• The minimum element in a Max Heap will
be found in the leaf nodes, so the time
complexity is also O(n).
• Heap Sort repeatedly deletes the root
element and re-heapifies the remaining
heap, so the total time complexity is O(n
log n).
• The number of distinct binary heaps (Min
or Max) with n distinct elements is:
Properties:
T(n) = T(k). T(n-k-1). 𝒏𝒏 − 𝟏𝟏𝑪𝑪𝒌𝒌
• Implemented using arrays.
• For node at index i: k is the number of the element in left
o Left child = 2i + 1 subtree.
o Right child = 2i + 2 T(n) = number of the heap with n nodes.
o Parent = (i - 1) / 2

Page No:- 09
DATA STRUCTURE
GATE फर्रे

Deletion is mostly done at the root node (i.e.,


the max in Max Heap or min in Min Heap).
Steps: Heap Sort uses a Max Heap (for ascending order
1. Remove the root node (i.e., element at sorting).
index 0 in array representation). Steps:
2. Replace it with the last element in the 4. Build a Max Heap from the input array
heap. (takes O(n) time).
3. Reduce heap size by 1.
5. Repeat until heap size > 1:
4. Heapify (percolate down) from the root
to restore the heap property. o Swap the root (maximum) with the
Time Complexity: O(log n) last element.
o Reduce the heap size by 1.

Heap Sort uses a Max Heap (for ascending order o Heapify the root element to restore
sorting). the max heap.
Steps: 6. The array will be sorted in ascending
1. Build a Max Heap from the input array order.
(takes O(n) time).
Time Complexity:
2. Repeat until heap size > 1: • Build Heap: O(n)
o Swap the root (maximum) with the • Heapify (n times): O(n log n)
last element.
• Overall: O(n log n)
o Reduce the heap size by 1.
o Heapify the root element to restore
the max heap.
3. The array will be sorted in ascending
order. Graph
Time Complexity: A graph is a collection of vertices (nodes) and edges
• Build Heap: O(n) (connections) that represent relationships between
pairs of objects.
• Heapify (n times): O(n log n) G= (V, E)
• Overall: O(n log n) Where:
• V = set of vertices
• E = set of edges (unordered pair for
undirected, ordered for directed)

Page No:- 10
DATA STRUCTURE
GATE फर्रे

Graph Representations Graph Traversals


Graph traversal refers to visiting all the vertices (and
optionally edges) of a graph in a systematic way.
Adjacency Matrix • BFS (Queue): Level-order, shortest path in
unweighted graph
• DFS (Stack/Recursion): Deepest first, used in
• 2D array G[V][V] cycle detection, topological sort
• G[i][j] = 1 if edge exists, else
0 Breadth-First Search (BFS)
• Space: O(V²) Level-wise traversal
Uses a queue (FIFO) data structure
Visits all immediate neighbours before going
deeper
Algorithm
Adjacency List • Mark the starting node as visited
• Enqueue it
• Array of lists • While queue not empty:
• Each list contains neighbours of • Dequeue node
• Visit all unvisited neighbours
a vertex
• Mark them visited and enqueue
• Space: O (V + E) Time Complexity:
• Preferred for sparse graphs • O (V + E) (V = vertices, E = edges)
Applications:
• Shortest path in unweighted graphs
• Bipartite graph check
• Connected components in undirected graph

Depth-First Search (DFS)


• Explores as deep as possible
• Uses stack (explicit or recursion)
• Backtracks when no unvisited neighbours

Algorithm:
1. Mark current node visited
2. Recursively visit all unvisited neighbours

Time Complexity:
• O (V + E)
Applications:
• Topological sorting (DAG)
• Cycle detection
• Strongly Connected Components
• Maze/path solving

Page No:- 11
DATA STRUCTURE
GATE फर्रे

Hashing Linear probing


Problem: Causes primary clustering
Hashing is a technique to map data of arbitrary size → Consecutive blocks of filled slots form, making
to fixed-size values using a hash function, for collisions more frequent
efficient search, insert, and delete operations. Pros: Simple implementation
Hash Table: Cons: Slower as clustering grows
• Stores key-value pairs Quadratic Probing
• Access time is O(1) (average case), O(n) (worst Solves primary clustering
case due to collisions) Problem: May lead to secondary clustering
Properties: Can fail to insert even when space exists if not
carefully designed
In double hashing h2(k) should not be 0, otherwise it
becomes the linear probing.

Deletion Problem in Open Addressing (Linear,


Quadratic, Double Hashing)
In open addressing, when you delete an element,
simply marking the slot as empty can break the
search chain for other elements inserted due to
collisions.
Why It’s a Problem:
• Open addressing relies on probing sequences.
• In open addressing (e.g., linear probing),
suppose n = 10, and we insert 14 and 24. Both
hash to index 4, so 14 goes to 4, and 24 goes
• Should be fast, uniform, and deterministic to 5.
• Maps key k to an index: • If we delete 14, index 4 becomes empty. Now,
h(k)=k mod n searching for 24 starts at 4 and stops there,
where n is the table size thinking it's not present, even though 24 is at
Good n: index 5.
• Should be a prime number to reduce • This happens because deletion breaks the
collisions probing chain, causing search failure.
Collisions • This may require the rehashing.
. Open Addressing (Store in same array) •

Collision Resolution Techniques

Technique Rule

Linear Probing Try next slot: (h(k) + i) % m


Separate chaining
Quadratic Probing (h(k) + i²) % m
A collision resolution technique where each slot in
Double Hashing (h1(k) + i × h2(k)) % m the hash table stores a linked list (or chain) of
elements.
Insertion: Insert the element at the head (or tail) of
the linked list at the hashed index.

Page No:- 12
DATA STRUCTURE
GATE फर्रे

Search: Hash the key to find the index, then


linearly search the linked list at that index.
Deletion: Hash the key, search the linked list, and
remove the node if found.
No Clustering: Since elements are in separate lists,
primary/secondary clustering does not occur.
Load Factor (λ): λ = n / m
(n = total elements, m = table size)
Performance depends on λ.

If load factor λ ≤ 1, then open addressing methods


(like linear probing, quadratic probing, or double
hashing) are efficient.
If λ > 1, then separate chaining is preferred, since
open addressing works best only when the table is
sparsely filled.

Page No:- 13

You might also like