The AVL tree at the heart of this project is managed entirely by the Python backend. The front end is responsible for one thing only: receiving the balanced tree structure that the server produces and turning it into an interactive SVG diagram using D3.js. This clean separation means all rotation, height-balancing, and traversal logic lives in Python while the JavaScript layer stays focused on presentation.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/avl_tree_car_front/llms.txt
Use this file to discover all available pages before exploring further.
AVL model
AVL.model.mjs is an intentionally thin class. Its only instance property is root, which starts as null and is replaced with the tree data whenever the backend sends an update. The insert, delete, and to_dict method declarations are present as stubs to document the interface, but contain no logic — the real implementations are on the server:
Never call
avl.insert() or avl.delete() directly from the front end. Use TreeService.emit_insert_obstacle() or TreeService.emit_remove_obstacle() instead and let the backend perform the operation, then wait for the avl_tree_balanced event to redraw the tree.Node model
Node.model.mjs extends NodeI, the interface defined in interfaces/Node.interface.mjs. The interface declares the four core AVL-node fields:
avl_tree_balanced event carry additional properties. After AVLController.__formatNodes() runs, each node in the tree also carries:
| Property | Source | Description |
|---|---|---|
id | Backend | Unique obstacle identifier (string) |
key | Backend | AVL tree key |
left | Backend | Left child node (or null) |
right | Backend | Right child node (or null) |
height | Backend | Node height in the balanced tree |
children | Backend (to_dict) | Array of child nodes used by D3 hierarchy |
x | __formatNodes() | Obstacle’s pixel X coordinate on the road |
y | __formatNodes() | Obstacle’s pixel Y coordinate on the road |
type | __formatNodes() | Obstacle type string (e.g. "Cone", "Rock") |
How tree data arrives
When the backend finishes balancing or traversing the tree it emits theavl_tree_balanced Socket.IO event on the AVLTree namespace. TreeService.on_avl_tree_balanced() receives this event and immediately bridges it into the DOM as a CustomEvent:
AVLController.listenGetTreeFromBackend() registers a document listener for this custom event and routes the payload to initDataTree():
Node formatting
Before the tree can be rendered, each node needs its visual properties (x, y, type) filled in from the client-side Road model. The backend sends only structural information (IDs, children, height); the coordinates and type are looked up locally.
AVLController.__formatNodes(current) walks the tree recursively. For each node it:
- Calls
road.getObstacleById(current.id)to find the matchingObstacleobject in the road’s obstacle array. - Calls
obstacle.format_values(true)to resolve the type string. - Copies
x,y, andtypefrom the obstacle onto the current node. - Recurses into
current.childrenif they exist.
D3.js rendering
AVLController.__render(data) is the sole rendering function. It clears the existing SVG content, builds a D3 hierarchy, and draws three layers: links (paths), node circles, and node decorations (image + text label).
Dynamic sizing
The SVG dimensions grow with the number of nodes to prevent overlapping. The minimum dimensions are 800 × 600 pixels:Tree layout
D3’stree() layout calculates node positions. The separation callback gives sibling nodes a tighter gap (1.4) than nodes that share only a common ancestor (2.2), avoiding collisions across subtrees:
Link drawing
Edges between parent and child nodes are drawn as smooth vertical curves usingd3.linkVertical():
Node decoration
Each node group receives three child elements:- Circle – a 25-pixel-radius
<circle>whoseidis_<obstacle-id>(prefixed with_for valid CSS selector use). - Image – an SVG
<image>that loads the obstacle’s icon from./app/assets/img/svg/<type-lowercase>.svg. - Text – a
<text>label showing the obstacle’s road coordinates as(x, y).