Skip to content

Commit 38d5497

Browse files
committed
feat: 将代码面板改为Java实现,添加debug效果和变量监视功能
- 将伪代码改为完整的Java实现 - 添加变量监视区域,显示cur、left、right、action的实时值 - 添加调用栈显示,展示递归调用层次 - 代码高亮与算法步骤精确绑定 - 添加断点标记和DEBUG徽章 - 使用深色主题,模拟IDE调试界面
1 parent 4ad4aa7 commit 38d5497

File tree

9,638 files changed

+1949810
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

9,638 files changed

+1949810
-0
lines changed
Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
# Design Document
2+
3+
## Overview
4+
5+
本设计文档描述了括号生成算法可视化应用的技术架构和实现方案。该应用使用 TypeScript + React + D3.js 构建,通过动画演示回溯算法生成有效括号组合的过程。应用采用单页面布局,所有组件在一个视口内展示,端口使用 37871。
6+
7+
## Architecture
8+
9+
```mermaid
10+
graph TB
11+
subgraph "Frontend Application"
12+
App[App Component]
13+
14+
subgraph "UI Layer"
15+
InputPanel[Input Panel]
16+
ControlPanel[Control Panel]
17+
TreeVisualization[Tree Visualization]
18+
StatePanel[State Panel]
19+
CodePanel[Code Panel]
20+
ResultsPanel[Results Panel]
21+
end
22+
23+
subgraph "Logic Layer"
24+
AlgorithmEngine[Algorithm Engine]
25+
AnimationController[Animation Controller]
26+
TreeBuilder[Tree Builder]
27+
end
28+
29+
subgraph "Data Layer"
30+
TreeState[Tree State]
31+
AnimationState[Animation State]
32+
GenerationHistory[Generation History]
33+
end
34+
end
35+
36+
App --> InputPanel
37+
App --> ControlPanel
38+
App --> TreeVisualization
39+
App --> StatePanel
40+
App --> CodePanel
41+
App --> ResultsPanel
42+
43+
InputPanel --> AlgorithmEngine
44+
ControlPanel --> AnimationController
45+
AnimationController --> AlgorithmEngine
46+
AlgorithmEngine --> TreeBuilder
47+
TreeBuilder --> TreeState
48+
TreeVisualization --> TreeState
49+
StatePanel --> AnimationState
50+
CodePanel --> AnimationState
51+
ResultsPanel --> GenerationHistory
52+
```
53+
54+
## Components and Interfaces
55+
56+
### Core Components
57+
58+
#### 1. App Component
59+
主应用组件,负责整体布局和状态管理。
60+
61+
```typescript
62+
interface AppProps {}
63+
64+
interface AppState {
65+
n: number;
66+
isRunning: boolean;
67+
currentStep: number;
68+
speed: number;
69+
treeData: TreeNode | null;
70+
results: string[];
71+
currentPath: string;
72+
leftCount: number;
73+
rightCount: number;
74+
highlightedLine: number;
75+
}
76+
```
77+
78+
#### 2. InputPanel Component
79+
用户输入面板,接收 n 值。
80+
81+
```typescript
82+
interface InputPanelProps {
83+
onSubmit: (n: number) => void;
84+
disabled: boolean;
85+
}
86+
```
87+
88+
#### 3. ControlPanel Component
89+
动画控制面板。
90+
91+
```typescript
92+
interface ControlPanelProps {
93+
isRunning: boolean;
94+
onPlay: () => void;
95+
onPause: () => void;
96+
onStep: () => void;
97+
onReset: () => void;
98+
speed: number;
99+
onSpeedChange: (speed: number) => void;
100+
disabled: boolean;
101+
}
102+
```
103+
104+
#### 4. TreeVisualization Component
105+
D3.js 树形可视化组件。
106+
107+
```typescript
108+
interface TreeVisualizationProps {
109+
treeData: TreeNode | null;
110+
currentNodeId: string | null;
111+
width: number;
112+
height: number;
113+
}
114+
```
115+
116+
#### 5. StatePanel Component
117+
当前算法状态显示面板。
118+
119+
```typescript
120+
interface StatePanelProps {
121+
currentPath: string;
122+
leftCount: number;
123+
rightCount: number;
124+
totalSteps: number;
125+
currentStep: number;
126+
}
127+
```
128+
129+
#### 6. CodePanel Component
130+
算法代码展示面板。
131+
132+
```typescript
133+
interface CodePanelProps {
134+
highlightedLine: number;
135+
callStackDepth: number;
136+
}
137+
```
138+
139+
#### 7. ResultsPanel Component
140+
结果列表展示面板。
141+
142+
```typescript
143+
interface ResultsPanelProps {
144+
results: string[];
145+
}
146+
```
147+
148+
### Algorithm Engine
149+
150+
```typescript
151+
interface AlgorithmEngine {
152+
initialize(n: number): void;
153+
getNextStep(): GenerationStep | null;
154+
reset(): void;
155+
getAllSteps(): GenerationStep[];
156+
}
157+
158+
interface GenerationStep {
159+
id: string;
160+
action: 'add_left' | 'add_right' | 'backtrack' | 'complete';
161+
currentString: string;
162+
leftRemaining: number;
163+
rightRemaining: number;
164+
nodeId: string;
165+
parentNodeId: string | null;
166+
isValid: boolean;
167+
codeLine: number;
168+
callStackDepth: number;
169+
}
170+
```
171+
172+
### Tree Builder
173+
174+
```typescript
175+
interface TreeBuilder {
176+
buildFromSteps(steps: GenerationStep[]): TreeNode;
177+
getNodeById(id: string): TreeNode | null;
178+
}
179+
```
180+
181+
## Data Models
182+
183+
### TreeNode
184+
185+
```typescript
186+
interface TreeNode {
187+
id: string;
188+
value: string; // 当前节点的括号字符 '(' 或 ')'
189+
path: string; // 从根到当前节点的完整路径
190+
children: TreeNode[];
191+
status: 'pending' | 'exploring' | 'valid' | 'pruned' | 'complete';
192+
leftRemaining: number;
193+
rightRemaining: number;
194+
}
195+
```
196+
197+
### AnimationState
198+
199+
```typescript
200+
interface AnimationState {
201+
isPlaying: boolean;
202+
isPaused: boolean;
203+
currentStepIndex: number;
204+
totalSteps: number;
205+
speed: number; // milliseconds between steps
206+
}
207+
```
208+
209+
### GenerationHistory
210+
211+
```typescript
212+
interface GenerationHistory {
213+
n: number;
214+
steps: GenerationStep[];
215+
results: string[];
216+
startTime: number;
217+
endTime: number | null;
218+
}
219+
```
220+
221+
222+
223+
## Correctness Properties
224+
225+
*A property is a characteristic or behavior that should hold true across all valid executions of a system-essentially, a formal statement about what the system should do. Properties serve as the bridge between human-readable specifications and machine-verifiable correctness guarantees.*
226+
227+
### Property 1: Input Validation Range
228+
*For any* integer value n, the input validator SHALL accept n if and only if 1 ≤ n ≤ 8, rejecting all other values with an error state.
229+
**Validates: Requirements 1.1, 1.2**
230+
231+
### Property 2: State Reset on New Input
232+
*For any* valid n value submission, the application state SHALL reset to initial values (empty tree, empty results, step 0) before starting new generation.
233+
**Validates: Requirements 1.3**
234+
235+
### Property 3: Tree Structure Integrity
236+
*For any* generation step that adds a bracket, a new child node SHALL be created with the correct parent reference, and the node's path SHALL equal parent's path concatenated with the new bracket.
237+
**Validates: Requirements 2.2**
238+
239+
### Property 4: Node Status Correctness
240+
*For any* tree node, its status SHALL be 'valid' if and only if the path is a complete valid parentheses combination (length = 2n and balanced), and 'pruned' if the path violates validity constraints (more right than left brackets at any point).
241+
**Validates: Requirements 2.4, 2.5**
242+
243+
### Property 5: Step Advancement Consistency
244+
*For any* animation state, clicking step forward SHALL increase currentStepIndex by exactly 1 (unless already at the final step).
245+
**Validates: Requirements 3.3**
246+
247+
### Property 6: Reset State Equivalence
248+
*For any* animation state after reset, the state SHALL be equivalent to the initial state after first loading with the same n value.
249+
**Validates: Requirements 3.4**
250+
251+
### Property 7: Speed Bounds Enforcement
252+
*For any* speed value set by the user, the actual animation interval SHALL be clamped to the range [100, 2000] milliseconds.
253+
**Validates: Requirements 3.5**
254+
255+
### Property 8: Step State Display Synchronization
256+
*For any* generation step, the displayed current string, left bracket count, and right bracket count SHALL exactly match the corresponding values in the step data.
257+
**Validates: Requirements 4.1, 4.2**
258+
259+
### Property 9: Results Collection Completeness
260+
*For any* completed algorithm run with input n, the results list SHALL contain exactly the Catalan number C(n) valid combinations, where C(n) = (2n)! / ((n+1)! * n!).
261+
**Validates: Requirements 4.3, 4.4**
262+
263+
### Property 10: Code Panel State Synchronization
264+
*For any* generation step, the highlighted code line and displayed call stack depth SHALL exactly match the step's codeLine and callStackDepth values.
265+
**Validates: Requirements 6.2, 6.3**
266+
267+
## Error Handling
268+
269+
### Input Errors
270+
- Invalid n value (non-integer, out of range): Display error message, disable generation button
271+
- Empty input: Show placeholder hint, disable generation button
272+
273+
### Runtime Errors
274+
- D3.js rendering failure: Display fallback message, log error to console
275+
- Animation timer issues: Auto-pause animation, show retry option
276+
277+
### State Errors
278+
- Inconsistent tree state: Reset to last known good state
279+
- Step index out of bounds: Clamp to valid range [0, totalSteps - 1]
280+
281+
## Testing Strategy
282+
283+
### Unit Testing
284+
使用 Vitest 进行单元测试:
285+
286+
1. **Algorithm Engine Tests**
287+
- Test generateParentheses function produces correct results for n = 1 to 8
288+
- Test step generation produces valid sequence of operations
289+
- Test backtracking logic correctly prunes invalid paths
290+
291+
2. **Input Validation Tests**
292+
- Test valid inputs (1-8) are accepted
293+
- Test invalid inputs are rejected with appropriate errors
294+
295+
3. **Tree Builder Tests**
296+
- Test tree construction from steps
297+
- Test node lookup by ID
298+
299+
### Property-Based Testing
300+
使用 fast-check 进行属性测试:
301+
302+
1. **Input Validation Property Tests**
303+
- Property 1: Generate random integers, verify acceptance/rejection matches range criteria
304+
305+
2. **Algorithm Correctness Property Tests**
306+
- Property 9: For any n in [1,8], verify result count equals Catalan number
307+
- Property 4: For any generated node, verify status matches validity criteria
308+
309+
3. **State Management Property Tests**
310+
- Property 5: For any state, verify step advancement increments by exactly 1
311+
- Property 7: For any speed input, verify clamping to [100, 2000]
312+
313+
4. **Synchronization Property Tests**
314+
- Property 8: For any step, verify display values match step data
315+
- Property 10: For any step, verify code panel state matches step data
316+
317+
### Test Configuration
318+
- Property tests: minimum 100 iterations per property
319+
- Each property test tagged with: `**Feature: parentheses-generator-visualization, Property {number}: {property_text}**`

0 commit comments

Comments
 (0)