Skip to content

Commit dae0fd1

Browse files
committed
binarySearch node x cordinate auto-calcualtion based on mid index
1 parent 740709d commit dae0fd1

2 files changed

Lines changed: 10 additions & 18 deletions

File tree

src/context/VisualizationContext.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export type VisualizationContextType = {
1818
left: number,
1919
right: number,
2020
y: number,
21-
minX: number,
22-
maxX: number,
2321
) => void;
2422
setAbort: (bool: boolean) => void;
2523
canGoNext: boolean;

src/context/VisualizationProvider.tsx

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,25 @@ function buildBinarySearchSteps(
115115
left: number,
116116
right: number,
117117
y: number,
118-
minX: number,
119-
maxX: number,
120118
parentId?: string,
121119
) {
122-
const x = (minX + maxX) / 2;
123120
const text = `binarySearch(${left}, ${right}, arr)`;
124121

122+
const xCord = left + right === 0 ? 0 : (left + right) / 2 * 50; // Scale x position based on left and right indices
123+
125124
const rootStep = createStepNode({
126125
nodeId: id,
127126
text,
128127
action: "create",
129-
position: { x, y },
128+
position: { x: xCord, y },
130129
parentId,
131130
});
132131

133132
let tail = rootStep;
134133

135134
if (left <= right) {
136135
const mid = Math.floor((left + right) / 2);
136+
console.log(mid);
137137
const midValue = arr[mid];
138138

139139
if (target < midValue) {
@@ -144,8 +144,6 @@ function buildBinarySearchSteps(
144144
left,
145145
mid - 1,
146146
y + 100,
147-
minX,
148-
x - 50,
149147
id,
150148
);
151149
tail.next = leftBranch.head;
@@ -159,8 +157,6 @@ function buildBinarySearchSteps(
159157
mid + 1,
160158
right,
161159
y + 100,
162-
x + 50,
163-
maxX,
164160
id,
165161
);
166162
tail.next = rightBranch.head;
@@ -173,7 +169,7 @@ function buildBinarySearchSteps(
173169
nodeId: id,
174170
text,
175171
action: "delete",
176-
position: { x, y },
172+
position: { x: xCord, y },
177173
parentId,
178174
});
179175

@@ -212,7 +208,7 @@ export function VisualizationProvider({ children }: { children: ReactNode }) {
212208
id: nodeId,
213209
position,
214210
data: { label: text, },
215-
style: { width: 60 },
211+
style: { width: (selectedAlgorithm === "binarySearch" ? 120 : 60) },
216212
},
217213
]);
218214

@@ -245,7 +241,7 @@ export function VisualizationProvider({ children }: { children: ReactNode }) {
245241
),
246242
);
247243
setCenter(position.x, position.y, { zoom: 1, duration: 1000 });
248-
}, [setCenter, setEdges, setNodes]);
244+
}, [setCenter, setEdges, setNodes, selectedAlgorithm]);
249245

250246
const revertSimulationStep = useCallback((step: SimulationStepNode) => {
251247
const { nodeId, text, position, parentId, action } = step.data;
@@ -268,7 +264,7 @@ export function VisualizationProvider({ children }: { children: ReactNode }) {
268264
id: nodeId,
269265
position,
270266
data: { label: text },
271-
style: { width: 60 },
267+
style: { width: (selectedAlgorithm === "binarySearch" ? 120 : 60) },
272268
},
273269
]);
274270

@@ -290,7 +286,7 @@ export function VisualizationProvider({ children }: { children: ReactNode }) {
290286
}
291287

292288
setCenter(position.x, position.y, { zoom: 1, duration: 1000 });
293-
}, [setCenter, setEdges, setNodes]);
289+
}, [setCenter, setEdges, setNodes, selectedAlgorithm]);
294290

295291
const canGoPrevious = currentStep !== null;
296292
const canGoNext = stepListHead !== null && (currentStep ? currentStep.next !== null : true);
@@ -373,10 +369,8 @@ export function VisualizationProvider({ children }: { children: ReactNode }) {
373369
left: number,
374370
right: number,
375371
y: number,
376-
minX: number,
377-
maxX: number,
378372
) => {
379-
const steps = buildBinarySearchSteps(arr, target, id, left, right, y, minX, maxX);
373+
const steps = buildBinarySearchSteps(arr, target, id, left, right, y);
380374
clearTimer();
381375
setStepListHead(steps.head);
382376
setCurrentStep(null);

0 commit comments

Comments
 (0)