Main Content

leaves

Determine terminal nodes

Syntax

N = leaves(T)
[N,K] = leaves(T,'sort')
N = leaves(T,'dp')
[N,K] = leaves(T,'sortdp')
[N,K] = leaves(T,'sdp')

Description

N = leaves(T) returns the indices of terminal nodes of the tree T where N is a column vector.

The nodes are ordered from left to right as in tree T.

[N,K] = leaves(T,'s') or [N,K] = leaves(T,'sort') returns sorted indices. M = N(K) are the indices reordered as in tree T, from left to right.

N = leaves(T,'dp') returns a matrix N, which contains the depths and positions of terminal nodes.

N(i,1) is the depth of the i-th terminal node, and N(i,2) is the position of the i-th terminal node.

[N,K] = leaves(T,'sortdp') or [N,K] = leaves(T,'sdp') returns sorted nodes.

Examples

% Create initial tree.
ord = 2; 
t = ntree(ord,3);        % binary tree of depth 3.
t=nodejoin(t,5);
t=nodejoin(t,4);
plot(t)

% List terminal nodes (index).
tnodes_ind = leaves(t)
tnodes_ind =
     7
     8
     4
     5
    13
    14

% List terminal nodes (sorted on index).
[tnodes_ind,Ind] = leaves(t,'sort')
tnodes_ind =
     4
     5
     7
     8
    13
    14

Ind =
     3
     4
     1
     2
     5
     6

% List terminal nodes (Depth_Position).
tnodes_depo = leaves(t,'dp')
tnodes_depo =
     3     0
     3     1
     2     1
     2     2
     3     6
     3     7

% List terminal nodes (sorted on Depth_Position).
[tnodes_depo,Ind] = leaves(t,'sortdp')
tnodes_depo =
     2     1
     2     2
     3     0
     3     1
     3     6
     3     7

Ind =
     3
     4
     1
     2
     5
     6

Version History

Introduced before R2006a

See Also

|