public int layout(Node node) {
node.y = node.depth * ySep;
if (node.isLeaf()) {
leafCount += 1;
node.x = leafCount * xSep;
return node.x;
} else {
int min = 0;
int max = 0;
for (Node child : node.children) {
int childCenter = layout(child);
if (min == 0) {
min = childCenter;
}
max = childCenter;
}
if (min == max) {
node.x = min;
} else {
node.x = min + (max - min) / 2;
}
return node.x;
}
}
basically, it lays out the leaves first, then returns their centers. Each non-leaf node uses the min/max values of its children to position itself. If the min and max are the same, it only has one child, so it is laid above. Otherwise, it is put at the center of the range.
Simple, probably nothing new, but does the job.
edit: Heh. Although, with large trees...


0 comments:
Post a Comment