www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/@mctree/leaves.m

    function List= leaves(T, varargin)
%LEAVES Operate on leaves of the tree
%
%   LIST = LEAVES(T) returns the "leaves" of the tree - those nodes that
%   have no children.  If there is only a single leaf node it will be
%   returned as an object. If there are many then a cell array of nodes
%   will be returned.
%
%   LIST = LEAVES(T,func,varargin) evaluates function 'func' for all leaves
%   and returns a cell array that contains the output from the function for
%   each leaf node found.  If there is only a single leaf node then the
%   lone result is returned without the cell array wrapper.
%
%   See also, MCTREE/PREORDER, MCTREE/POSTORDER, MCTREE/CHILDREN

%  Copyright 2000-2005 The MathWorks, Inc. and Ford Global Technologies, Inc.



if ~isnull(address(T))
    List = i_Leaf(address(T), varargin);
    if iscell(List) && length(List)==1
        List = List{1};
    end
else
    error(message('mbc:mctree:InvalidPointerReference1'));
end



function List = i_Leaf(p,Args)

% collect outputs in a cell array
T = p.info;
ch = T.Children;
if isempty(ch)
    % p is a leaf
    if ~isempty(Args)
        % First element of varargin argument is function name
        % other elements are additional inputs
        if length(Args)>1
            List= {feval(Args{1},T,Args{2:end})};
        else
            List= {feval(Args{1},T)};
        end
    else
        % Just return the node
        List= {T};
    end
elseif ~any(isnull(ch))
    List = cell(length(ch),1);
    for i = 1:length(ch)
        % preorder each of the children
        List{i} = i_Leaf(ch(i), Args);
    end
    List = cat(1,List{:});
else
    List = {};
end