matlab求树的节点数s=struct('parent',{'A','B','C','D','E
matlab求树的节点数
s=struct('parent',{'A','B','C','D','E','F','G'},'children',{'BC','DE','F','G','','',''})
function [nodesnum]=numofnodes(s,num,node)%s为树的结构体,node为子树的根节点
syms A
for i=1:size(s,2)
if isequal(s(i).parent,node)
t=i
end
end
if isempty(s(t).children)
num=num
else
A=s(t).children
num=size(A,2)+num
end
for j=1:size(A,2)
for k=t:size(s,2)
if isequal(s(k).parent,A(j))
numofnodes(s,num,s(k).parent)
end
end
end
上面的程序是求树的节点数,求高手给我看看为什么出来的结果不正确,谢谢大家啦!
Jueast
13 years, 11 months ago