A full-stack project that builds and visualizes Binary Search Trees (BST) and AVL-balanced trees from user-provided numbers. Submissions are persisted to an embedded H2 database and can be retrieved for review.
- Backend: Spring Boot 3.5, Java 21, Spring Web, Spring Data JPA, H2 (file-based)
- Frontend: React 19, TypeScript, Vite 7
backend/: Spring Boot application and H2 storagefrontend/: React + Vite client
-
Java 21
-
Node.js 18+ and npm
-
Vite dev server defaults to
http://localhost:5173. -
The frontend reads
VITE_API_BASE_URLto call the backend.
-
Backend jar:
cd backend ./mvnw clean package java -jar target/bstdb-0.0.1-SNAPSHOT.jar -
Defaults to port
8080. -
H2 database is stored at
backend/data/bstdb.mv.db. -
H2 console can be enabled via
application.properties(already enabled):spring.h2.console.enabled=true. -
Frontend static build:
cd frontend npm run dev
Base URL: http://localhost:8080
- Query params:
balanced(boolean, defaultfalse) — iftrue, builds an AVL-balanced tree; otherwise a standard BST.
- Body (JSON):
{ "numbers": "10, 5, 20, 15" } - Response (200):
{ "id": 1, "inputNumbers": "10, 5, 20, 15", "balanced": false, "createdAt": "2025-01-01T00:00:00Z", "tree": { "nodes": [{ "id": 1, "value": 10, "x": 0, "y": 0 }], "edges": [{ "from": 1, "to": 2 }], "layout": { "hSpacing": 80, "vSpacing": 80 } } }
Example:
curl -X POST "http://localhost:8080/api/process-numbers?balanced=false" \
-H "Content-Type: application/json" \
-d '{"numbers":"10,5,20,15"}'- Query params:
limit(int, default50)
- Response (200):
[ { "id": 1, "inputNumbers": "10, 5, 20, 15", "balanced": false, "createdAt": "2025-01-01T00:00:00Z", "tree": { "nodes": [], "edges": [], "layout": { "hSpacing": 80, "vSpacing": 80 } } } ]
Example:
curl "http://localhost:8080/api/previous-trees?limit=25"- Env variable: The client uses
VITE_API_BASE_URLto reach the backend. - Where to set it: Create
frontend/.env.local(not committed) based onfrontend/.env.example. - Value: During local dev, set it to your Spring Boot URL (default
http://localhost:8080).
Example frontend/.env.local:
VITE_API_BASE_URL=http://localhost:8080Notes on Vite env files:
- Files are loaded by mode and precedence:
.env,.env.local,.env.development,.env.production, etc. Local variants (.env.local) are ignored by git. - Only variables prefixed with
VITE_are exposed to the client. - To use a different backend URL temporarily, you can export it inline:
VITE_API_BASE_URL=http://localhost:8081 npm run dev.
- Backend allows localhost origins by pattern (e.g.,
http://localhost:*,http://127.0.0.1:*). Seebackend/src/main/java/com/example/bstdb/config/CorsConfig.javafor details.
- H2 file is stored at
backend/data/bstdb.mv.db. - To reset all stored trees, stop the backend and delete that file.
- Verify Java 21 and Node 18+ are installed and on PATH.
- Ensure
VITE_API_BASE_URLis correctly set infrontend/.env.local. - Backend logs output to console; check
backend/server.logif configured. - If the frontend shows network errors (e.g.,
Failed to fetch), confirm the backend is running on8080or updateVITE_API_BASE_URLaccordingly.