我有一个名为Device
. A Device
可以有一位家长 Device
. A Device
还可以有n child Devices
.
我有一个下拉列表,显示所有可选的Devices
。我可以获得所有Devices
很容易在数据库中 -db.Devices
.
层次结构可以无限深。
我需要得到all Devices
不高于或低于给定值Device
在树上。本质上我要的是Devices
与给定无关Device
(既不是父母/祖父母/曾祖父母/等,也不是孩子/孙子/曾孙/等)。我还需要排除给定的Device
从列表中。
做这个的最好方式是什么?我应该使用递归吗?
(我使用 C# 和实体框架以及 SQL Server 数据库,因此我可以使用 Linq To SQL 或使用模型本身。)
我的方法是首先获取设备的所有兄弟姐妹D
:
P = parent of the device
sibs = {all children of P that are not D}
任何的任何后代d in sibs
与以下无关D
。继续沿着家谱往上走:
G = grandparent of the device
sibs = sibs union {all children of G that are not P}
继续这样,设置sibs
他们的所有后代就是你所追求的集合。
在伪代码中:
D = device;
siblings = {};
while (D has parent) {
P = parent(D);
siblings = siblings union (children(P) \ D);
D = P;
}
return descendants(siblings);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)