Spaces:
Runtime error

File size: 2,886 Bytes
cf69cfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<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>