Spaces:
Runtime error
Runtime error
Download Trees/templates/tree.html from swetha-coding/dsahub: direct link, hf CLI and curl.
- Browser
- Download file 2.89 kB
-
https://huggingface.co/spaces/swetha-coding/dsahub/resolve/main/Trees/templates/tree.html
- Command line
-
hf download hf://spaces/swetha-coding/dsahub/Trees/templates/tree.html
-
curl -L -o tree.html https://huggingface.co/spaces/swetha-coding/dsahub/resolve/main/Trees/templates/tree.html
2.89 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>BST Visualizer</title> | |
| <script src="https://d3js.org/d3.v7.min.js"></script> | |
| <style> | |
| body { font-family: Arial; text-align: center; margin-top: 20px; } | |
| svg { border: 1px solid #ccc; margin-top: 20px; } | |
| input, button { padding: 8px; margin: 5px; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>🌳 Binary Search Tree Visualizer</h1> | |
| <!-- Insert Form --> | |
| <form id="insertForm"> | |
| <input type="number" name="value" id="valueInput" placeholder="Enter number" required> | |
| <button type="submit">Insert Node</button> | |
| </form> | |
| <!-- Tree Visualization --> | |
| <div id="tree"></div> | |
| <script> | |
| const form = document.getElementById("insertForm"); | |
| form.addEventListener("submit", async (e) => { | |
| e.preventDefault(); | |
| const value = document.getElementById("valueInput").value; | |
| await fetch("/trees/insert", { | |
| method: "POST", | |
| headers: {"Content-Type": "application/x-www-form-urlencoded"}, | |
| body: "value=" + value | |
| }); | |
| document.getElementById("valueInput").value = ""; | |
| updateTree(); | |
| }); | |
| async function updateTree() { | |
| const res = await fetch("/trees/tree"); | |
| const data = await res.json(); | |
| d3.select("#tree").html(""); // clear old tree | |
| if (!data) return; | |
| const width = 600, height = 400; | |
| const svg = d3.select("#tree").append("svg") | |
| .attr("width", width).attr("height", height); | |
| const root = d3.hierarchy(data); | |
| const treeLayout = d3.tree().size([width - 100, height - 100]); | |
| treeLayout(root); | |
| // Links | |
| svg.selectAll("line") | |
| .data(root.links()) | |
| .enter().append("line") | |
| .attr("x1", d => d.source.x + 50) | |
| .attr("y1", d => d.source.y + 50) | |
| .attr("x2", d => d.target.x + 50) | |
| .attr("y2", d => d.target.y + 50) | |
| .attr("stroke", "black"); | |
| // Nodes | |
| svg.selectAll("circle") | |
| .data(root.descendants()) | |
| .enter().append("circle") | |
| .attr("cx", d => d.x + 50) | |
| .attr("cy", d => d.y + 50) | |
| .attr("r", 20) | |
| .attr("fill", "lightblue"); | |
| // Labels | |
| svg.selectAll("text") | |
| .data(root.descendants()) | |
| .enter().append("text") | |
| .attr("x", d => d.x + 50) | |
| .attr("y", d => d.y + 50) | |
| .attr("dy", 5) | |
| .attr("text-anchor", "middle") | |
| .text(d => d.data.name); | |
| } | |
| updateTree(); // initial load | |
| </script> | |
| </body> | |
| </html> | |