Skip to content

treelayout

sparse · scripts/sparse/treelayout.m

: [x, y] = treelayout (tree)
: [x, y] = treelayout (tree, permutation)
: [x, y, h, s] = treelayout (…)

Compute node coordinates for drawing a tree or forest.

tree is a vector of parent indices: element k is the index of the parent of node k, or 0 for a root. permutation, if given, is a postorder permutation of the nodes that fixes the order in which children are laid out.

x and y are the coordinates of the nodes, with leaves spread one unit apart and every parent centred over its children. h is the height of the forest (the number of edges on the longest root-to-leaf path) and s is the number of nodes on the top chain of the layout that have exactly one child.

Time and memory grow linearly with the number of nodes.