Main Content

depo2ind

Node depth-position to node index

    Description

    depo2ind is a tree-management utility.

    example

    n = depo2ind(ord,[d,p]) returns, for a tree of order ord, the indices n of the nodes whose depths and positions are encoded within d and p.

    The nodes are numbered from left to right and from top to bottom. The root index is 0.

    Examples

    collapse all

    Create a binary tree of depth 3.

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

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

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

    Use the allnodes function to obtain the depth-position of the tree nodes.

    aln_depo = allnodes(t,"deppos");

    Convert every depth-position to a node index.

    aln_ind = depo2ind(ord,aln_depo);
    [aln_depo aln_ind]
    ans = 11×3
    
         0     0     0
         1     0     1
         1     1     2
         2     0     3
         2     1     4
         2     2     5
         2     3     6
         3     0     7
         3     1     8
         3     6    13
          ⋮
    
    

    Input Arguments

    collapse all

    Order of a tree, specified as a positive integer.

    Example: A binary tree is a tree of order 2.

    Depths and positions (at those depths) for nodes in a tree of order ord, specified as a pair of column vectors with nonnegative integer entries. The vectors d and p must have the same length.

    The values of depths D and positions P must be such that D ≥0 and 0≤ PordD-1.

    Output Arguments

    collapse all

    Node indices, returned as a column vector with nonnegative integer entries.

    The values of the indices N are such that 0 ≤ N < (ordmax(d)+1-1)/(ord–1), where d are the depths.

    Note

    For a column vector X, we have depo2ind(ord,X) = X.

    Version History

    Introduced before R2006a

    See Also