Here, we will try to break a "simple binary tree" using one million random elements.
You will see, it's impossible. Which is counterintuitive, against the "statistical theory".
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Binary Tree Balance Experiment</title>
<style>
body { font-family: sans-serif; padding: 20px; line-height: 1.6; max-width: 600px; }
#results { margin-top: 20px; padding: 10px; background: #f4f4f4; border-radius: 5px; height: 200px; overflow-y: auto; font-family: monospace; }
.stat { font-weight: bold; color: #2c3e50; }
</style>
</head>
<body>
<h2>The Superintelligent Game: Tree Stability</h2>
<p>How many times to run our attempt to break a 'simple binary tree'?</p>
<input type="number" id="attempts" value="5" min="1">
<button onclick="runExperiment()">Run Experiment</button>
<div id="results">Results will appear here...</div>
<script>
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
function insert(root, val) {
let curr = root;
let depth = 1;
while (true) {
depth++;
if (val < curr.val) {
if (!curr.left) { curr.left = new Node(val); return depth; }
curr = curr.left;
} else {
if (!curr.right) { curr.right = new Node(val); return depth; }
curr = curr.right;
}
}
}
async function runExperiment() {
const iterations = document.getElementById('attempts').value;
const output = document.getElementById('results');
output.innerHTML = "Starting experiment...<br>";
for (let i = 1; i <= iterations; i++) {
// Let the UI breathe between heavy loops
await new Promise(r => setTimeout(r, 10));
let root = new Node(Math.random());
let maxDepth = 1;
const numElements = 1000000;
for (let j = 0; j < numElements; j++) {
const d = insert(root, Math.random());
if (d > maxDepth) maxDepth = d;
}
output.innerHTML += `Attempt ${i}: Max Depth = <span class="stat">${maxDepth}</span><br>`;
output.scrollTop = output.scrollHeight;
}
output.innerHTML += "<strong>Experiment Complete.</strong>";
}
</script>
</body>
</html>