Loading...
Loading...
Data Structure ek systematic tarika hai data ko computer memory mein organize, store, aur manage karne ka — taaki efficiently access aur modify kiya ja sake.
Kyon zaroori hai?
Classification:
Array ek contiguous memory block hai jisme same type ke elements store hote hain.
int arr[5] = {10, 20, 30, 40, 50};
// arr[0] = 10, arr[4] = 50
// Address of arr[i] = Base + i * sizeof(type)
Operations & Complexity:
| Operation | Time | Space | |---|---|---| | Access arr[i] | O(1) | O(1) | | Search (unsorted) | O(n) | O(1) | | Search (sorted) | O(log n) | O(1) | | Insert at end | O(1) amortized | O(1) | | Insert at middle | O(n) | O(1) | | Delete | O(n) | O(1) |
Types:
int a[10]int a[3][4] — Row major orderImportant Patterns:
Two Pointer: i=0, j=n-1 — pair sum, palindrome check
Sliding Window: fixed/variable window — max subarray sum
Prefix Sum: pre[i] = pre[i-1] + a[i] — range queries O(1)
Kadane's Algorithm: max subarray sum O(n)
Node structure: Data + Pointer(s) to next node. Memory not contiguous.
struct Node {
int data;
struct Node* next;
};
Types:
| Type | Pointers | Traversal | |---|---|---| | Singly LL | next only | Forward only | | Doubly LL | prev + next | Both directions | | Circular LL | last→first | Loops |
Key Operations:
Insert at head: new→next = head; head = new; O(1)
Insert at tail: traverse to end, tail→next = new; O(n)
Delete node: prev→next = curr→next; free(curr); O(n)
Reverse LL: 3 pointer technique (prev, curr, next) O(n)
Important Interview Problems:
LIFO — Last In First Out. Think of a stack of plates.
// Operations:
push(x) // Insert x at top O(1)
pop() // Remove from top O(1)
peek() // View top element O(1)
isEmpty() // Check if empty O(1)
Implementations: Array-based (fixed size) or Linked List (dynamic)
Real Applications:
Classic Problem — Balanced Parentheses:
Input: "{[()]}" → Valid
Input: "{[(]}" → Invalid
Algorithm: Push open brackets, pop on close, check match
FIFO — First In First Out. Think of a line at a counter.
Enqueue: Add at rear O(1)
Dequeue: Remove from front O(1)
Front/Rear access O(1)
Types:
Applications:
A Tree is a hierarchical non-linear DS with a root node and subtrees.
Terminology:
- Root: Top node (no parent)
- Leaf: Node with no children
- Height: Longest path root→leaf
- Depth: Distance from root to node
- Degree: Number of children
Every node has at most 2 children (left, right).
Traversals:
Inorder (L-Root-R): Gives sorted output for BST
Preorder (Root-L-R): Used to copy/serialize tree
Postorder(L-R-Root): Used to delete tree, evaluate expressions
Level Order (BFS): Uses queue, level by level
Property: Left subtree < Root < Right subtree (at every node)
Search: Compare and go left/right O(h)
Insert: Find correct position O(h)
Delete: 3 cases — leaf, one child, two children (inorder successor) O(h)
h = O(log n) for balanced BST
h = O(n) for skewed BST
Keeps height difference (Balance Factor) = |h_left - h_right| ≤ 1
Rotations on imbalance:
Complete Binary Tree where parent is always greater (Max-Heap) or smaller (Min-Heap) than children.
Insert: Add at end, bubble up (heapify up) O(log n)
Delete: Remove root, bring last, bubble down O(log n)
Build heap from array: O(n)
Heap Sort: O(n log n)
Graph G = (V, E) — Set of Vertices (nodes) and Edges (connections).
Types:
- Directed / Undirected
- Weighted / Unweighted
- Cyclic / Acyclic
- Connected / Disconnected
- DAG (Directed Acyclic Graph)
Representations:
- Adjacency Matrix: O(V²) space, O(1) edge lookup
- Adjacency List: O(V+E) space, O(degree) edge lookup
BFS (Breadth First Search):
Use Queue. Visit all neighbors before going deeper.
Time: O(V+E) | Applications: Shortest path (unweighted), level order, connected components
DFS (Depth First Search):
Use Stack (or recursion). Go deep before backtracking.
Time: O(V+E) | Applications: Topological sort, cycle detection, path finding, SCC
Shortest Path Algorithms:
| Algorithm | Graph Type | Complexity | |---|---|---| | BFS | Unweighted | O(V+E) | | Dijkstra | Weighted, no -ve | O((V+E) log V) | | Bellman-Ford | Weighted, -ve allowed | O(V·E) | | Floyd-Warshall | All pairs | O(V³) |
Minimum Spanning Tree:
Hash Function: Maps a key to a bucket index. Goal: Uniform distribution.
index = key % table_size
Good hash function properties:
- Fast to compute
- Uniform distribution
- Minimize collisions
Collision Handling:
| Method | Idea | Pros | Cons | |---|---|---|---| | Chaining | LL at each bucket | Easy to implement | Extra memory | | Linear Probing | Check next slot | Cache friendly | Clustering | | Quadratic Probing | Check i² slots ahead | Less clustering | May miss slots | | Double Hashing | Use 2nd hash for step | Best distribution | 2 hash functions |
Average case: O(1) for insert/search/delete (with good hash + low load factor)
Key Sorting Algorithms:
Bubble Sort: Compare adjacent, swap if wrong order O(n²) O(n) best Stable
Selection Sort: Find min, put at correct position O(n²) O(n²) always Unstable
Insertion Sort: Build sorted array one by one O(n²) O(n) best Stable
Merge Sort: Divide & Conquer, merge sorted halves O(n log n) always Stable
Quick Sort: Pivot, partition, recurse O(n log n) avg O(n²) worst Unstable
Heap Sort: Build max-heap, extract one by one O(n log n) always Unstable
Counting Sort: Count frequencies (integers only) O(n+k) Stable
When to use what?
| DS | Access | Search | Insert | Delete | Space | |---|---|---|---|---|---| | Array | O(1) | O(n) | O(n) | O(n) | O(n) | | Stack | O(n) | O(n) | O(1) | O(1) | O(n) | | Queue | O(n) | O(n) | O(1) | O(1) | O(n) | | Linked List | O(n) | O(n) | O(1) | O(1) | O(n) | | BST (avg) | O(log n) | O(log n) | O(log n) | O(log n) | O(n) | | Hash Table | N/A | O(1) | O(1) | O(1) | O(n) | | Heap | N/A | O(n) | O(log n) | O(log n) | O(n) |
Arrays & Strings:
Linked List:
Stack & Queue:
Trees:
Graphs:
Full Data Structures notes for B.Tech CS Sem 3 — Arrays, Linked Lists, Stacks, Queues, Trees, Graphs, Hashing, Sorting, and Complexity Analysis with diagrams and interview questions.
48 pages · 2.4 MB · Updated 2026-03-11
Array mein O(1) random access hota hai lekin size fixed hoti hai. Linked List dynamic hai, O(n) access, par insertion/deletion O(1) at head.
Stack: function calls (call stack), undo/redo, browser back. Queue: CPU scheduling, printer queue, BFS traversal.
Jab elements sorted order mein insert ho — tree skewed ban jata hai. Balanced BST (AVL, Red-Black) is problem solve karta hai.
Chaining (linked list in each bucket) ya Open Addressing (linear probing, quadratic probing, double hashing) se.
General purpose ke liye Merge Sort (O(n log n) guaranteed). In-place chahiye to Quick Sort (average O(n log n)). Already sorted ke liye Insertion Sort O(n).
Algorithms — Design, Analysis, and Techniques
Algorithms
Object Oriented Programming (OOP) — Complete Notes with Java & C++
Object Oriented Programming
DBMS Complete Notes — B.Tech CS Sem 4
Database Management Systems
Compiler Design — Complete Notes CS Sem 6
Compiler Design
Machine Learning Complete Notes — B.Tech CS Sem 6
Machine Learning
Your feedback helps us improve notes and tutorials.