如何计算树的子节点 [英] How to count child node of a tree

查看:46
本文介绍了如何计算树的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子:

userId LeftChild RightChild

1006 1007 1008

1007 1009 1011

1008 1014 1012

1009 1013 0

1011 0 0

1012 0 0

1013 1015 0

1014 0 0

1015 0 0

其中如何计算userId的子节点= 1006

plz一个一个帮助我

i have a table:
userId LeftChild RightChild
1006 1007 1008
1007 1009 1011
1008 1014 1012
1009 1013 0
1011 0 0
1012 0 0
1013 1015 0
1014 0 0
1015 0 0
in which how to calculate the child node of userId=1006
plz Some one One Help me

推荐答案

GetChildForIDs(List<int> IDs)
{
   List<int> IdsToReturn = new List<int>();
   List<int> LIds = TableRepository.GetAll().where(x=>IDs.Contains(x.ID) & x.ID > 0).select(x=>x.LeftChild).ToList();
   List<int> RIds = TableRepository.GetAll().where(x=>IDs.Contains(x.ID) & x.ID > 0).select(x=>x.RightChild).ToList();
   
   IdsToReturn.AddRange(LIds);
   IdsToReturn.AddRange(RIds); 
   
   if(IdsToReturn.Count()>0)
   {
      GetChildForIDs(IdsToReturn);
      
      //Here Count is Class Level Static Variable
      Count = Count + IdsToReturn.Count();

   }
 
}





现在调用此方法后打印计数变量。你会得到计数。



Now After Calling this method print Count Variable.You will get the count.


这篇关于如何计算树的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆