Data Structures
"Data Structures are the containers for your algorithms. A good engineer knows that picking the right structure is 90% of the optimization battle. Arrays, Trees, Graphs - let's sort them out."
1. Linear Structures π
Arrays
Contiguous memory. O(1) access by index. Fixed size (usually). Expensive insertion/deletion O(n).
Linked Lists
Nodes with pointers. Dynamic size. O(1) insert/delete (if at head/tail). O(n) access (sequential).
Stacks (LIFO)
Last In, First Out. Push/Pop. Used for recursion, undo features, expression parsing.
Queues (FIFO)
First In, First Out. Enqueue/Dequeue. Used for printer spooling, task scheduling, BFS.
2. Trees and Heaps π³
Hierarchical structures. Essential for database indexing and file systems.
- Binary Tree: Each node has max 2 children.
- BST (Binary Search Tree): Left < Root < Right. Allows O(log n) search/insert/delete if balanced.
- Heap: Complete binary tree. Max-Heap (Parent > Child) or Min-Heap. Foundation of Priority Queues and Heapsort.
- AVL / Red-Black: Self-balancing BSTs. Guarantees O(log n) even in worst case.
3. Graphs πΈοΈ
The ultimate versatile structure. Nodes (Vertices) connected by Edges.
- Adjacency Matrix: 2D array. Good for dense graphs. O(1) edge check. O(V^2) space.
- Adjacency List: Array of lists. Good for sparse graphs. O(V+E) space.
- BFS (Breadth-First Search): Uses Queue. Shortest path in unweighted graph.
- DFS (Depth-First Search): Uses Stack/Recursion. Path finding, cycle detection.
- Dijkstra: Shortest path in weighted graph (no negative weights).
4. Hashing π
The magic of O(1) lookup. Key $\to$ Hash Function $\to$ Index.
Collision Handling: What if two keys hash to the same index?
1. Chaining: Store a list at that index.
2. Open Addressing: Probe for next available slot (Linear, Quadratic, Double Hashing).
Test Your Knowledge! π§
Ready ka na ba? Take the practice quiz for Data Structures to reinforce what you just learned.
Start Practice Quiz π