A modern, interactive bus/railway seat booking system powered by Segment Tree with Lazy Propagation data structure. This project demonstrates efficient seat management across journey segments with real-time visualization.
- Efficient Seat Management: O(log M) time complexity per operation using Segment Trees
- Real-time Visualization: Interactive UI showing segment tree structure and bookings
- Range-based Booking: Book seats for specific journey segments (from station to station)
- Availability Queries: Quickly find available seats for any route
- Visual Journey Tracking: See your journey path visually
- Booking History: Track all bookings with detailed information
- A modern web browser (Chrome, Firefox, Safari, or Edge)
- No additional dependencies or installations required!
- Clone or download this repository
- Open
index.htmlin your web browser - Start booking seats!
The system uses a Segment Tree to manage seat availability efficiently. Each seat has its own segment tree tracking which journey segments are occupied.
Example: With 10 stations, we have 9 segments: [0,1), [1,2), [2,3), ..., [8,9)
Station: 0 -------- 1 -------- 2 -------- 3 -------- 4
Segments: [0,1) [1,2) [2,3) [3,4)
The segment tree is a binary tree where:
- Leaf nodes represent individual segments
- Internal nodes represent ranges of segments
- Each node stores the maximum occupancy in its range
[0,4) Max=1 โ Root covers all segments
/ \
[0,2) Max=1 [2,4) Max=0
/ \ / \
[0,1) M=0 [1,2) M=1 [2,3) M=0 [3,4) M=0 โ Leaves
- Query: Check if max occupancy in range
[L, R)is 0 - Update: If available, add +1 to all segments in range
[L, R) - Propagate: Tree nodes update lazily to reflect new maximum
- Find: Locate the booking record
- Update: Subtract -1 from the booked range
[L, R) - Restore: Seat becomes available for new bookings
- Query each seat's segment tree for the desired route
- If
max == 0in the range: seat is available - If
max > 0: seat has conflicting bookings
Instead of updating all nodes immediately:
- Mark parent nodes with a "lazy" value
- Push lazy values down only when needed
- Makes range updates O(log M) instead of O(M)
- Select Journey: Choose "From Station" and "To Station"
- Pick a Seat: Click on an available seat (gray)
- Book: Click the "Book Seat" button
- Confirm: See your booking appear in the log
- Find your booking in the Booking Log
- Click the "Cancel" button next to it
- The seat becomes available again for that route
- Select your desired route
- Click "Find Available" button
- All available seats will be highlighted
- Book Seat: O(log M) per seat
- Cancel Booking: O(log M) per seat
- Query Availability: O(log M) per seat
- Find All Available: O(N ร log M) where N = number of seats
- Per Seat: O(4M) for segment tree
- Overall: O(N ร M) where N = seats, M = stations
.
โโโ index.html # Main HTML structure
โโโ index.css # Styling and animations
โโโ index.js # Segment tree implementation and logic
โโโ README.md # This file
- Control Panel: Station selection, seat grid, and action buttons
- Algorithm Card: Real-time segment tree visualization
- Booking Log: List of all active bookings
- Journey Visual: Interactive journey path display
- Status Messages: Toast notifications for user feedback
- Number of Seats: 20 seats (configurable)
- Number of Stations: 10 stations (configurable)
- Journey Segments: 9 segments
- Passenger A books Seat 1 from Station 0 โ 3
- Passenger B books Seat 1 from Station 5 โ 8
- โ Both bookings succeed (no overlap)
- Passenger A books Seat 2 from Station 2 โ 6
- Passenger B tries Seat 2 from Station 4 โ 8
- โ Booking fails (segments [4,6) overlap)
- Passenger A books Seat 3 from Station 0 โ 4
- Passenger B books Seat 3 from Station 4 โ 8
- โ Both succeed (boundaries don't overlap)
This project demonstrates:
- Segment Tree data structure implementation
- Lazy Propagation optimization technique
- Range queries and updates
- Real-world application of advanced data structures
- Interactive visualization of algorithms
This is a DSA lab project. Suggestions and improvements are welcome!
This project is created for educational purposes as part of DSA Lab coursework.
Created for DSA Lab - College Coursework
Note: This implementation prioritizes clarity and educational value. For production systems, additional features like persistent storage, user authentication, and server-side validation would be required.