NEC Cheatsheet - Data Structures, Sorting, Searching & Graphs
DATA STRUCTURE BASICS
- Data Types: int, float, char, pointer, etc.
- Data Structures: Array, Linked List, Stack, Queue, Tree, Graph
- ADT: Abstract representation of data and operations
- Big-O: Worst-case | Big-Omega: Best-case | Big-Theta: Average-case
LINEAR STRUCTURES
- Array: Fixed size, direct access, O(1) lookup
- Stack: LIFO, push/pop - used in recursion, expression evaluation
- Queue: FIFO, enqueue/dequeue - scheduling, buffers
- Infix to Postfix: Use stack to rearrange
- Postfix Evaluation: Use stack to evaluate expression
LINKED LISTS
- Singly: One-way links | Doubly: Two-way | Circular: Loop back
- Operations: Insert (head/tail/mid), Delete (head/tail/mid)
- Dynamic memory: malloc/free (C), new/delete (C++)
TREES
- Binary Tree: Max 2 children | BST: Left<root<Right
- Traversals: In-order (LNR), Pre-order (NLR), Post-order (LRN)
- Height = longest path to leaf | Depth = root to node
- AVL Tree: Balanced BST using rotations after insert/delete
SORTING ALGORITHMS
- Selection Sort: O(n^2) | Bubble: O(n^2) | Insertion: O(n^2)
- Merge Sort: O(n log n) | Quick Sort: Avg O(n log n), Worst O(n^2)
- Heap Sort: Uses binary heap, O(n log n)
- Radix Sort: Non-comparison, O(kn) | Shell Sort: Improved insertion
HASHING
- Hash Function: Converts key to index
- Collision Handling: Chaining, Linear Probing, Double Hashing
- Load Factor: n/table size - keep < 0.7
- Perfect Hash: No collisions (ideal)
GRAPHS
- Representation: Adj. Matrix (O(V^2)), Adj. List (O(V+E))
- DFS: Stack or recursion | BFS: Queue
- Transitive Closure: Warshall's Algorithm (O(V^3))
- Topological Sort: DAG only, DFS/BFS based
MST & SHORTEST PATH
- Prim's: Greedy, grows from a node | Kruskal: Greedy, edge-based
- Dijkstra: No negative weights | Bellman-Ford: Handles negatives
- Spanning Tree: Connects all nodes, min total edge cost
- Shortest Path Tree: Min cost path from source to all