Main Content

noleaves

Determine nonterminal nodes

    Description

    noleaves is a tree-management utility.

    N = noleaves(T) returns the indices of the nonterminal nodes of the tree T in the column vector N. The nodes are ordered from left to right and from top to bottom. The root index is 0.

    N = noleaves(T,"dp") returns the depths and positions of the nonterminal nodes. N(i,1) is the depth and N(i,2) is the position of the ith nonterminal node.

    example

    Examples

    collapse all

    Create a binary tree of depth 3.

    ord = 2;
    t = ntree(ord,3);

    Merge the nodes with indices 4 and 5. Plot the tree.

    t = nodejoin(t,5);
    t = nodejoin(t,4);
    fig = plot(t);

    Figure contains an axes object and an object of type uimenu. The axes object with title Tree Decomposition contains 21 objects of type line, text.

    Programmatically, in Node Label, change the selected item from Depth_Position to Index.

    plot(t,"setNodeLabel",fig,"Index")

    Figure contains an axes object and an object of type uimenu. The axes object with title Tree Decomposition contains 21 objects of type line, text.

    List the indices of the nonterminal nodes.

    noleaves(t)
    ans = 5×1
    
         0
         1
         2
         3
         6
    
    

    List the depth-position of the nonterminal nodes.

    noleaves(t,"dp")
    ans = 5×2
    
         0     0
         1     0
         1     1
         2     0
         2     3
    
    

    Input Arguments

    collapse all

    Tree, specified as a dtree, ntree, or wptree object.

    Output Arguments

    collapse all

    Nonterminal node description, returned as a column vector or matrix. The nodes are numbered from left to right as in the tree T. The root index is 0.

    • If N = noleaves(T), N is a P-by-1 column vector, where P is the number of nonterminal nodes in the tree. N(i) is the index of the ith nonterminal node.

    • If N = noleaves(T,"dp"), N is a P-by-2 matrix. N(i,1) is the depth and N(i,2) is the position of the ith nonterminal node.

    Version History

    Introduced before R2006a

    See Also