Data Structures and Algorithms - Visual Guide

Array Structures

Static Array

graph LR A[Index 0
Value: 10] --> B[Index 1
Value: 20] B --> C[Index 2
Value: 30] C --> D[Index 3
Value: 40] D --> E[Index 4
Value: 50] style A fill:#e1f5fe style B fill:#e1f5fe style C fill:#e1f5fe style D fill:#e1f5fe style E fill:#e1f5fe

Dynamic Array Growth

graph TB subgraph "Initial Array (Size 4)" A1[10] --- A2[20] --- A3[30] --- A4[40] end subgraph "After Adding Element (Size 8)" B1[10] --- B2[20] --- B3[30] --- B4[40] --- B5[50] --- B6[null] --- B7[null] --- B8[null] end A1 -.->|Resize & Copy| B1 style B5 fill:#4caf50 style B6 fill:#f5f5f5 style B7 fill:#f5f5f5 style B8 fill:#f5f5f5

Linked List Structures

Singly Linked List

graph LR HEAD --> A[Data: 10
Next: *] A --> B[Data: 20
Next: *] B --> C[Data: 30
Next: *] C --> D[Data: 40
Next: null] style HEAD fill:#ff9800 style A fill:#2196f3,color:#fff style B fill:#2196f3,color:#fff style C fill:#2196f3,color:#fff style D fill:#2196f3,color:#fff

Doubly Linked List

graph LR HEAD --> A[Prev: null
Data: 10
Next: *] A --> B[Prev: *
Data: 20
Next: *] B --> C[Prev: *
Data: 30
Next: null] A -.-> B B -.-> A B -.-> C C -.-> B style HEAD fill:#ff9800 style A fill:#4caf50,color:#fff style B fill:#4caf50,color:#fff style C fill:#4caf50,color:#fff

Circular Linked List

graph LR HEAD --> A[Data: 10] A --> B[Data: 20] B --> C[Data: 30] C --> A style HEAD fill:#ff9800 style A fill:#9c27b0,color:#fff style B fill:#9c27b0,color:#fff style C fill:#9c27b0,color:#fff

Stack and Queue

Stack (LIFO)

graph TB subgraph "Stack Operations" TOP[TOP] --> E4[Element 4] E4 --> E3[Element 3] E3 --> E2[Element 2] E2 --> E1[Element 1] E1 --> BOTTOM[BOTTOM] end PUSH[PUSH] -.->|Add to top| TOP POP[POP] -.->|Remove from top| TOP style TOP fill:#f44336,color:#fff style BOTTOM fill:#f44336,color:#fff style E4 fill:#ffeb3b style E3 fill:#ffeb3b style E2 fill:#ffeb3b style E1 fill:#ffeb3b style PUSH fill:#4caf50,color:#fff style POP fill:#ff5722,color:#fff

Queue (FIFO)

graph LR REAR[REAR] --> E4[Element 4] E4 --> E3[Element 3] E3 --> E2[Element 2] E2 --> E1[Element 1] E1 --> FRONT[FRONT] ENQUEUE[ENQUEUE] -.->|Add to rear| REAR DEQUEUE[DEQUEUE] -.->|Remove from front| FRONT style REAR fill:#2196f3,color:#fff style FRONT fill:#2196f3,color:#fff style E4 fill:#e1f5fe style E3 fill:#e1f5fe style E2 fill:#e1f5fe style E1 fill:#e1f5fe style ENQUEUE fill:#4caf50,color:#fff style DEQUEUE fill:#ff5722,color:#fff

Tree Structures

Binary Tree

graph TB A[1] --> B[2] A --> C[3] B --> D[4] B --> E[5] C --> F[6] C --> G[7] D --> H[8] E --> I[9] style A fill:#ff9800,color:#fff style B fill:#2196f3,color:#fff style C fill:#2196f3,color:#fff style D fill:#4caf50,color:#fff style E fill:#4caf50,color:#fff style F fill:#4caf50,color:#fff style G fill:#4caf50,color:#fff style H fill:#9c27b0,color:#fff style I fill:#9c27b0,color:#fff

Binary Search Tree

graph TB A[50] --> B[30] A --> C[70] B --> D[20] B --> E[40] C --> F[60] C --> G[80] D --> H[10] E --> I[35] style A fill:#ff9800,color:#fff style B fill:#2196f3,color:#fff style C fill:#2196f3,color:#fff style D fill:#4caf50,color:#fff style E fill:#4caf50,color:#fff style F fill:#4caf50,color:#fff style G fill:#4caf50,color:#fff style H fill:#9c27b0,color:#fff style I fill:#9c27b0,color:#fff

Tree Traversals

graph TB subgraph "Traversal Types" direction TB A[Root] --> B[Left] A --> C[Right] B --> D[Left-Left] B --> E[Left-Right] PRE["Preorder: Root → Left → Right
A → B → D → E → C"] IN["Inorder: Left → Root → Right
D → B → E → A → C"] POST["Postorder: Left → Right → Root
D → E → B → C → A"] LEVEL["Level Order: Top to Bottom
A → B → C → D → E"] end style A fill:#ff9800,color:#fff style B fill:#2196f3,color:#fff style C fill:#2196f3,color:#fff style D fill:#4caf50,color:#fff style E fill:#4caf50,color:#fff

Heap Structures

Max Heap

graph TB A[100] --> B[80] A --> C[90] B --> D[70] B --> E[75] C --> F[85] C --> G[60] D --> H[65] D --> I[50] style A fill:#f44336,color:#fff style B fill:#ff5722,color:#fff style C fill:#ff5722,color:#fff style D fill:#ff9800,color:#fff style E fill:#ff9800,color:#fff style F fill:#ff9800,color:#fff style G fill:#ff9800,color:#fff style H fill:#ffc107 style I fill:#ffc107

Min Heap

graph TB A[10] --> B[20] A --> C[15] B --> D[30] B --> E[25] C --> F[18] C --> G[40] D --> H[35] D --> I[45] style A fill:#4caf50,color:#fff style B fill:#8bc34a,color:#fff style C fill:#8bc34a,color:#fff style D fill:#cddc39 style E fill:#cddc39 style F fill:#cddc39 style G fill:#cddc39 style H fill:#ffeb3b style I fill:#ffeb3b

Heap Operations

graph TB subgraph "Heap Insert Process" direction TB A1[Insert 95] --> A2[Add at bottom] A2 --> A3[Compare with parent] A3 --> A4{Parent < Child?} A4 -->|Yes| A5[Swap with parent] A4 -->|No| A6[Done] A5 --> A3 end subgraph "Extract Max Process" direction TB B1[Remove root] --> B2[Move last to root] B2 --> B3[Compare with children] B3 --> B4{Parent < Largest Child?} B4 -->|Yes| B5[Swap with largest child] B4 -->|No| B6[Done] B5 --> B3 end style A1 fill:#4caf50,color:#fff style A6 fill:#4caf50,color:#fff style B1 fill:#f44336,color:#fff style B6 fill:#f44336,color:#fff

Hash Table Structure

Hash Table with Chaining

graph TB subgraph "Hash Table (Size 7)" H0["Index 0"] --> L0["Key: 7, Val: A"] --> L01["Key: 14, Val: B"] H1["Index 1"] --> L1["Key: 1, Val: C"] H2["Index 2"] --> L2["Key: 9, Val: D"] H3["Index 3"] --> N3["NULL"] H4["Index 4"] --> L4["Key: 4, Val: E"] --> L41["Key: 11, Val: F"] H5["Index 5"] --> L5["Key: 5, Val: G"] H6["Index 6"] --> N6["NULL"] end HASH["Hash Function
h(k) = k % 7"] -.-> H0 style H0 fill:#2196f3,color:#fff style H1 fill:#2196f3,color:#fff style H2 fill:#2196f3,color:#fff style H3 fill:#2196f3,color:#fff style H4 fill:#2196f3,color:#fff style H5 fill:#2196f3,color:#fff style H6 fill:#2196f3,color:#fff style L0 fill:#4caf50,color:#fff style L01 fill:#4caf50,color:#fff style L1 fill:#4caf50,color:#fff style L2 fill:#4caf50,color:#fff style L4 fill:#4caf50,color:#fff style L41 fill:#4caf50,color:#fff style L5 fill:#4caf50,color:#fff style N3 fill:#f5f5f5 style N6 fill:#f5f5f5 style HASH fill:#ff9800,color:#fff

Graph Structures

Undirected Graph

graph TB A((A)) --- B((B)) A --- C((C)) B --- D((D)) C --- D C --- E((E)) D --- E style A fill:#2196f3,color:#fff style B fill:#4caf50,color:#fff style C fill:#ff9800,color:#fff style D fill:#f44336,color:#fff style E fill:#9c27b0,color:#fff

Directed Graph

graph TB A((A)) --> B((B)) A --> C((C)) B --> D((D)) C --> D C --> E((E)) D --> E E --> A style A fill:#2196f3,color:#fff style B fill:#4caf50,color:#fff style C fill:#ff9800,color:#fff style D fill:#f44336,color:#fff style E fill:#9c27b0,color:#fff

Weighted Graph

graph TB A((A)) -->|5| B((B)) A -->|3| C((C)) B -->|7| D((D)) C -->|2| D C -->|4| E((E)) D -->|1| E style A fill:#2196f3,color:#fff style B fill:#4caf50,color:#fff style C fill:#ff9800,color:#fff style D fill:#f44336,color:#fff style E fill:#9c27b0,color:#fff

Algorithm Visualizations

Binary Search Process

graph TB subgraph "Binary Search for Target = 7" A["Array: 1, 3, 5, 7, 9, 11, 13"] B["Step 1: left=0, right=6, mid=3, arr[3]=7"] C["Found! Target at index 3"] A --> B B --> C end subgraph "Search Space Reduction" S1["1, 3, 5, 7, 9, 11, 13"] --> S2["Compare with middle element"] S2 --> S3["Target found at middle"] end style B fill:#4caf50,color:#fff style C fill:#ff9800,color:#fff

Merge Sort Process

graph TB A[38, 27, 43, 3, 9, 82, 10] --> B[38, 27, 43, 3] A --> C[9, 82, 10] B --> D[38, 27] B --> E[43, 3] C --> F[9] C --> G[82, 10] D --> H[38] D --> I[27] E --> J[43] E --> K[3] G --> L[82] G --> M[10] H --> N[27, 38] I --> N J --> O[3, 43] K --> O L --> P[10, 82] M --> P N --> Q[3, 27, 38, 43] O --> Q F --> R[9, 10, 82] P --> R Q --> S[3, 9, 10, 27, 38, 43, 82] R --> S style A fill:#f44336,color:#fff style S fill:#4caf50,color:#fff

Quick Sort Partitioning

graph TB subgraph "Partitioning Process" A["3, 6, 8, 10, 1, 2, 1"] --> B["Pivot = 1 (last element)"] B --> C["Move elements < pivot to left"] C --> D["Move elements > pivot to right"] D --> E["1, 1, 2, 3, 6, 8, 10"] end style A fill:#ff9800,color:#fff style B fill:#2196f3,color:#fff style E fill:#4caf50,color:#fff

DFS vs BFS Traversal

graph TB subgraph "Graph" A((A)) --> B((B)) A --> C((C)) B --> D((D)) C --> E((E)) D --> F((F)) E --> F end subgraph "DFS Order" DFS[A → B → D → F → E → C] end subgraph "BFS Order" BFS[A → B → C → D → E → F] end style A fill:#ff9800,color:#fff style B fill:#2196f3,color:#fff style C fill:#2196f3,color:#fff style D fill:#4caf50,color:#fff style E fill:#4caf50,color:#fff style F fill:#9c27b0,color:#fff style DFS fill:#f44336,color:#fff style BFS fill:#4caf50,color:#fff

Dijkstra's Algorithm

graph TB subgraph "Shortest Path from A" A((A-0)) -->|4| B((B-4)) A -->|2| C((C-2)) B -->|3| D((D-7)) C -->|1| D C -->|5| E((E-7)) D -->|2| E end subgraph "Algorithm Steps" STEP1["Start from A with distance 0"] STEP2["Update neighbors of A"] STEP3["Visit unvisited node with min distance"] STEP4["Update neighbors of visited node"] STEP5["Repeat until all nodes visited"] STEP1 --> STEP2 STEP2 --> STEP3 STEP3 --> STEP4 STEP4 --> STEP5 end style A fill:#4caf50,color:#fff style B fill:#2196f3,color:#fff style C fill:#ff9800,color:#fff style D fill:#f44336,color:#fff style E fill:#9c27b0,color:#fff

Dynamic Programming Visualization

Fibonacci with Memoization

graph TB subgraph "Recursive Calls for fib(5)" F5["fib(5)"] --> F4["fib(4)"] F5 --> F3a["fib(3)"] F4 --> F3b["fib(3)"] F4 --> F2a["fib(2)"] F3a --> F2b["fib(2)"] F3a --> F1a["fib(1)"] F3b --> F2c["fib(2)"] F3b --> F1b["fib(1)"] end subgraph "Memoization Table" M1["fib(1) = 1"] M2["fib(2) = 1"] M3["fib(3) = 2"] M4["fib(4) = 3"] M5["fib(5) = 5"] end style F5 fill:#f44336,color:#fff style F4 fill:#ff9800,color:#fff style F3a fill:#2196f3,color:#fff style F3b fill:#2196f3,color:#fff,stroke-dasharray: 5 5 style M1 fill:#4caf50,color:#fff style M2 fill:#4caf50,color:#fff style M3 fill:#4caf50,color:#fff style M4 fill:#4caf50,color:#fff style M5 fill:#4caf50,color:#fff

0/1 Knapsack DP Table

graph TB subgraph "Items: weight/value" I1[Item 1: 2/1] I2[Item 2: 3/4] I3[Item 3: 4/5] I4[Item 4: 5/7] end subgraph "DP Table (Capacity = 8)" H["Capacity: 0 1 2 3 4 5 6 7 8"] R0["Item 0: 0 0 0 0 0 0 0 0 0"] R1["Item 1: 0 0 1 1 1 1 1 1 1"] R2["Item 2: 0 0 1 4 4 5 5 5 5"] R3["Item 3: 0 0 1 4 5 5 6 9 9"] R4["Item 4: 0 0 1 4 5 7 7 9 12"] H --> R0 R0 --> R1 R1 --> R2 R2 --> R3 R3 --> R4 end style I1 fill:#ff9800,color:#fff style I2 fill:#2196f3,color:#fff style I3 fill:#4caf50,color:#fff style I4 fill:#9c27b0,color:#fff style H fill:#e0e0e0 style R0 fill:#f5f5f5 style R1 fill:#f5f5f5 style R2 fill:#f5f5f5 style R3 fill:#f5f5f5 style R4 fill:#f5f5f5

Trie (Prefix Tree)

graph TB ROOT((root)) --> A((a)) ROOT --> B((b)) ROOT --> C((c)) A --> A1((n)) A --> A2((p)) A1 --> A11((d)) A11 --> A111((🏁)) A2 --> A21((p)) A21 --> A211((l)) A211 --> A2111((e)) A2111 --> A21111((🏁)) B --> B1((a)) B1 --> B11((t)) B11 --> B111((🏁)) C --> C1((a)) C1 --> C11((r)) C11 --> C111((🏁)) C1 --> C12((t)) C12 --> C121((🏁)) style ROOT fill:#ff9800,color:#fff style A111 fill:#4caf50,color:#fff style A21111 fill:#4caf50,color:#fff style B111 fill:#4caf50,color:#fff style C111 fill:#4caf50,color:#fff style C121 fill:#4caf50,color:#fff

Segment Tree

graph TB A[0-7: 36] --> B[0-3: 10] A --> C[4-7: 26] B --> D[0-1: 3] B --> E[2-3: 7] C --> F[4-5: 11] C --> G[6-7: 15] D --> H[0: 1] D --> I[1: 2] E --> J[2: 3] E --> K[3: 4] F --> L[4: 5] F --> M[5: 6] G --> N[6: 7] G --> O[7: 8] style A fill:#f44336,color:#fff style B fill:#ff9800,color:#fff style C fill:#ff9800,color:#fff style D fill:#2196f3,color:#fff style E fill:#2196f3,color:#fff style F fill:#2196f3,color:#fff style G fill:#2196f3,color:#fff style H fill:#4caf50,color:#fff style I fill:#4caf50,color:#fff style J fill:#4caf50,color:#fff style K fill:#4caf50,color:#fff style L fill:#4caf50,color:#fff style M fill:#4caf50,color:#fff style N fill:#4caf50,color:#fff style O fill:#4caf50,color:#fff

Algorithm Complexity Comparison

graph TB subgraph "Time Complexity Growth" O1["O(1) - Constant"] OLOG["O(log n) - Logarithmic"] ON["O(n) - Linear"] ONLOG["O(n log n) - Linearithmic"] ON2["O(n²) - Quadratic"] O2N["O(2^n) - Exponential"] ONF["O(n!) - Factorial"] O1 --> OLOG OLOG --> ON ON --> ONLOG ONLOG --> ON2 ON2 --> O2N O2N --> ONF end style O1 fill:#4caf50,color:#fff style OLOG fill:#8bc34a,color:#fff style ON fill:#cddc39 style ONLOG fill:#ffeb3b style ON2 fill:#ff9800,color:#fff style O2N fill:#f44336,color:#fff style ONF fill:#9c27b0,color:#fff

These MermaidJS diagrams provide comprehensive visual representations of:

  1. Data Structure layouts - Arrays, linked lists, trees, heaps, hash tables, graphs
  2. Algorithm processes - Sorting steps, search procedures, traversal orders
  3. Dynamic programming - Memoization tables, recursive call trees
  4. Advanced structures - Tries, segment trees, specialized trees
  5. Complexity comparisons - Growth rates and performance characteristics

Each diagram uses color coding to highlight different components and relationships, making it easier to understand the structure and flow of algorithms and data structures. The visual approach helps in:

  • Understanding memory layout
  • Following algorithm execution steps
  • Seeing relationships between elements
  • Comparing different approaches
  • Identifying patterns and optimizations