From 4f4c5a38bd57ac7f384c33bd0c9cde076efec922 Mon Sep 17 00:00:00 2001 From: Muhammed Nihas Date: Fri, 2 Jan 2026 00:30:15 +0530 Subject: [PATCH 1/2] feat(heaps): extend heap visualizer and logic to support both min and max heaps --- src/components/dsa/HeapVisualizer.jsx | 6 +++--- src/lib/dsaAdvancedUtils.js | 10 +++++---- src/pages/DsaVisualization.jsx | 31 ++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/components/dsa/HeapVisualizer.jsx b/src/components/dsa/HeapVisualizer.jsx index ae5aac7..8b369d4 100644 --- a/src/components/dsa/HeapVisualizer.jsx +++ b/src/components/dsa/HeapVisualizer.jsx @@ -5,7 +5,7 @@ import { Play, Pause, Plus, ArrowDown } from 'lucide-react'; import { cn } from '@/lib/utils'; import { generateHeapSteps } from '@/lib/dsaAdvancedUtils'; -const HeapVisualizer = ({ darkMode }) => { +const HeapVisualizer = ({ darkMode, heapType }) => { const [heap, setHeap] = useState([10, 20, 15, 30, 40]); const [inputValue, setInputValue] = useState(''); const [history, setHistory] = useState([]); @@ -27,7 +27,7 @@ const HeapVisualizer = ({ darkMode }) => { const handleInsert = () => { if (!inputValue) return; const val = parseInt(inputValue); - const steps = generateHeapSteps(heap, 'insert', val); + const steps = generateHeapSteps(heap, 'insert', val, heapType); setHistory(steps); setCurrentStep(-1); setIsPlaying(true); @@ -42,7 +42,7 @@ const HeapVisualizer = ({ darkMode }) => { const displayHeap = (currentStep >= 0 && history[currentStep].heap) ? history[currentStep].heap : heap; const activeIdx = currentStep >= 0 ? history[currentStep].activeIdx : null; const compareIndices = currentStep >= 0 && history[currentStep].type === 'compare' ? history[currentStep].indices : []; - const message = currentStep >= 0 ? history[currentStep].message : "Heap Ready (Max Heap)"; + const message = currentStep >= 0 ? history[currentStep].message : `Heap Ready (${heapType})`; // Render as tree const renderTree = (idx, x, y, offset) => { diff --git a/src/lib/dsaAdvancedUtils.js b/src/lib/dsaAdvancedUtils.js index 83f91a3..1af9bca 100644 --- a/src/lib/dsaAdvancedUtils.js +++ b/src/lib/dsaAdvancedUtils.js @@ -78,10 +78,12 @@ export const generateBSTSteps = (root, operation, value) => { return { steps, finalRoot: newRoot }; }; -// Heap Logic (Max Heap) -export const generateHeapSteps = (heap, operation, value) => { +// Heap Logic +export const generateHeapSteps = (heap, operation, value, heapType = 'Max Heap') => { const steps = []; let newHeap = [...heap]; + const comparator = heapType === 'Max Heap' ? (a, b) => a > b : (a, b) => a < b; + const comparisonText = heapType === 'Max Heap' ? 'larger' : 'smaller'; if (operation === 'insert') { newHeap.push(value); @@ -93,9 +95,9 @@ export const generateHeapSteps = (heap, operation, value) => { let parentIdx = Math.floor((idx - 1) / 2); steps.push({ type: 'compare', heap: [...newHeap], indices: [idx, parentIdx], message: `Comparing ${newHeap[idx]} with parent ${newHeap[parentIdx]}` }); - if (newHeap[idx] > newHeap[parentIdx]) { + if (comparator(newHeap[idx], newHeap[parentIdx])) { [newHeap[idx], newHeap[parentIdx]] = [newHeap[parentIdx], newHeap[idx]]; - steps.push({ type: 'swap', heap: [...newHeap], indices: [idx, parentIdx], message: 'Child is larger, swapping' }); + steps.push({ type: 'swap', heap: [...newHeap], indices: [idx, parentIdx], message: `Child is ${comparisonText}, swapping` }); idx = parentIdx; } else { break; diff --git a/src/pages/DsaVisualization.jsx b/src/pages/DsaVisualization.jsx index 5caefa3..cba950a 100644 --- a/src/pages/DsaVisualization.jsx +++ b/src/pages/DsaVisualization.jsx @@ -408,6 +408,7 @@ const DsaVisualization = () => { const [darkMode, setDarkMode] = useState(true); const [customInput, setCustomInput] = useState(''); const [searchTarget, setSearchTarget] = useState(42); // Default target + const [heapType, setHeapType] = useState('Max Heap'); const [array, setArray] = useState([]); @@ -455,7 +456,7 @@ const DsaVisualization = () => { graphs: ['BFS', 'DFS', 'Dijkstra', 'AStar'], linkedlist: [], trees: [], - heaps: [], + heaps: ['Max Heap', 'Min Heap'], dp: [] }; @@ -586,6 +587,30 @@ const DsaVisualization = () => { )} )} + {activeTab === 'heaps' && ( +
+

Heap Type

+ +
+ )} {(activeTab === 'sorting' || activeTab === 'searching') && (
@@ -693,9 +718,9 @@ const DsaVisualization = () => {
-

Heap Visualizer (Max)

+

Heap Visualizer ({heapType})

- +
) : activeTab === 'dp' ? ( From 6c017f1c0d46138eb1e9dd71ee4ba5c857ba9081 Mon Sep 17 00:00:00 2001 From: Muhammed Nihas Date: Thu, 8 Jan 2026 23:01:19 +0530 Subject: [PATCH 2/2] feat: Add Nihas to contributors list --- src/data/contributors.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/data/contributors.js b/src/data/contributors.js index 1e75b78..d65aeee 100644 --- a/src/data/contributors.js +++ b/src/data/contributors.js @@ -29,5 +29,11 @@ export const dsaContributors = [ role: "Backend Architecture", github: "https://github.com/jamesw", avatar: "https://images.unsplash.com/photo-1599566150163-29194dcaad36?w=150&h=150&fit=crop&crop=faces" + }, + { + name: "Muhammed Nihas", + role: "AI Engineer", + github: "https://github.com/mhdnihas", + avatar: "https://avatars.githubusercontent.com/u/144945271?v=4" } ];