Data Structure Short Notes
Data Structure Short Notes
GATE फर्रे
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.
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 फर्रे
Condition Formula
Empty front == -1
Page No:- 04
DATA STRUCTURE
GATE फर्रे
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 फर्रे
Time Complexity:
Page No:- 06
DATA STRUCTURE
GATE फर्रे
Preorder: 10 20 30
Page No:- 07
DATA STRUCTURE
GATE फर्रे
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
Page No:- 08
DATA STRUCTURE
GATE फर्रे
Page No:- 09
DATA STRUCTURE
GATE फर्रे
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 फर्रे
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 फर्रे
Technique Rule
Page No:- 12
DATA STRUCTURE
GATE फर्रे
Page No:- 13