treelayout

Computes node coordinates for drawing a tree.

Category: Mathematics > Sparse Matrices

Syntax

[x, y] = treelayout(p)

Returns the coordinates of each node of the tree described by the parent vector p.

[x, y, h] = treelayout(p)

Also returns the height of the tree.

Description

Leaves are spread evenly from left to right, and each parent is centered above its children. The result can be given directly to plot or gplot for custom drawings. treeplot and etreeplot use this layout.

Input arguments

p
Parent vector: p(j) is the parent of node j, and 0 marks a root. A parent must come before its children, so p(j) < j.

Output arguments

x, y
Row vectors with the horizontal and vertical position of each node, in the unit square, with the root at the top.
h
Height of the tree, in levels below the root.

Example

Layout of a small tree

p = [0 1 1 2 2];
[x, y, h] = treelayout(p)
Output
x = 1×5

    0.5000    0.3750    0.7500    0.2500    0.5000
y = 1×5

    0.7500    0.5000    0.2500    0.2500    0.2500
h = 2

See also

treeplot etreeplot gplot etree