Hello,
I have a data structures problem, and since I am using Three.js for the program, I thought I would ask here first.
Given the root of a complete binary tree and the height, how can I position the nodes on an XY Grid so that there is no overlap?
The idea is that each node has an XY coordinate and can be displayed as a box on the screen.
I tried doing an inorder print, but the nodes on the left would overlap the nodes on the right.
function inOrderPrint(node, x, y, depth, maxDepth) {
if (node === undefined) {
return;
}
console.log("(X,Y): " + x + ", " + y)
let boxMesh = new three.Mesh(new BoxGeometry(1, 1, 1), new three.MeshBasicMaterial())
boxMesh.position.x = x;
boxMesh.position.y = y;
scene.add(boxMesh)
inOrderPrint(node.getLeft(), <Some decrease of x>, y - 2, depth + 1, maxDepth);
inOrderPrint(node.getRight(), <Some increase of x>,y - 2, depth + 1, maxDepth)
}
The root is at 0,0, and nodes to the left decrease in x, while nodes to the right increase in x.
If the tree is complete and the height is known ahead of time, what equation should I use to set the exact x value of a given node?
Should I be using breadth search instead?