c#中的递归树遍历问题
Issue with recursive tree traversal in c#
我有以下格式的树结构数据:
H1
H1 - 1
H1 - 1 - 1
H1 - 1 - 1 - 1
H1 - 1 - 1 - 1 -1
H1 - 1 - 1 - 1 -1 - 1
H1 - 1 - 2
H1 - 1 - 3
H1 - 2
H1 - 2 - 1
H1 - 2 - 2
H2
H3
H3 - 1
H3 - 1 - 1
H3 - 1 - 1 - 1
H3 - 1 - 1 - 1 - 1
H3 - 1 - 1 - 2
H3 - 1 - 2
H3 - 1 - 3
H3 - 2
我必须通过传递子项目的 ID
来检查项目是否存在于上述树中的任何位置,因此我需要遍历上述树中的每个项目。到目前为止,我已经编写了以下递归方法:
public bool CheckIfChildItemExists(Item parentItem, long childItemId)
{
var isChildExisting = false;
foreach (Item item in parentItem.Children)
{
if (item == context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault() || item.Children.Contains(context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault()))
{
isChildExisting = true;
return isChildExisting;
}
else
{
return CheckIfChildItemExists(item, childItemId);
}
}
return isChildExisting;
}
使用上面的方法:
- 根项目
H1, H2, H3
可访问。
H1
的所有分支(H1 -1
、H1 - 1 - 1
、H1 - 1 - 1 -1
等)均可访问。
H1 - 2 - 1
和 H1 - 2 - 2
不可访问,它们没有被遍历。他们的父项 H1 - 2
是可以访问的。
H3
的所有子项都不可访问。
我的方法哪里做错了?
你提前回来了。你所做的实际上是从根到叶最多检查一个分支,但你永远不会检查其他分支。
public bool CheckIfChildItemExists(Item parentItem, long childItemId)
{
foreach (Item item in parentItem.Children)
{
if (item == context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault() || item.Children.Contains(context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault()))
{
return true;
}
else
{
var childItemExists = CheckIfChildItemExists(item, childItemId);
if(childItemExists) return true; // else continue search in other children
}
}
return isChildExisting;
}
我不确定您在 if
声明中的条件是否正确。如果您的所有项目都符合搜索条件,您可以尝试使用该代码。您应该 运行 对每个根项执行此过程,或者将人工根项传递给该函数。
public bool CheckIfChildItemExists(Item parentItem, long childItemId)
{
if(parentItem.ItemID == childItemId && parentItem.IsActive) return true;
foreach (Item item in parentItem.Children)
{
var childItemExists = CheckIfChildItemExists(item, childItemId);
if(childItemExists) return true; // else continue search in
}
return false;
}
在 item == context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault()
中,您实际上是在比较两个对象,为什么不直接检查 item
对象的条件呢?此外,您可以使用 context.Items.FirstOrDefault(x => x.ItemID == childItemId && x.IsActive)
代替。
我有以下格式的树结构数据:
H1
H1 - 1
H1 - 1 - 1
H1 - 1 - 1 - 1
H1 - 1 - 1 - 1 -1
H1 - 1 - 1 - 1 -1 - 1
H1 - 1 - 2
H1 - 1 - 3
H1 - 2
H1 - 2 - 1
H1 - 2 - 2
H2
H3
H3 - 1
H3 - 1 - 1
H3 - 1 - 1 - 1
H3 - 1 - 1 - 1 - 1
H3 - 1 - 1 - 2
H3 - 1 - 2
H3 - 1 - 3
H3 - 2
我必须通过传递子项目的 ID
来检查项目是否存在于上述树中的任何位置,因此我需要遍历上述树中的每个项目。到目前为止,我已经编写了以下递归方法:
public bool CheckIfChildItemExists(Item parentItem, long childItemId)
{
var isChildExisting = false;
foreach (Item item in parentItem.Children)
{
if (item == context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault() || item.Children.Contains(context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault()))
{
isChildExisting = true;
return isChildExisting;
}
else
{
return CheckIfChildItemExists(item, childItemId);
}
}
return isChildExisting;
}
使用上面的方法:
- 根项目
H1, H2, H3
可访问。 H1
的所有分支(H1 -1
、H1 - 1 - 1
、H1 - 1 - 1 -1
等)均可访问。H1 - 2 - 1
和H1 - 2 - 2
不可访问,它们没有被遍历。他们的父项H1 - 2
是可以访问的。H3
的所有子项都不可访问。
我的方法哪里做错了?
你提前回来了。你所做的实际上是从根到叶最多检查一个分支,但你永远不会检查其他分支。
public bool CheckIfChildItemExists(Item parentItem, long childItemId)
{
foreach (Item item in parentItem.Children)
{
if (item == context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault() || item.Children.Contains(context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault()))
{
return true;
}
else
{
var childItemExists = CheckIfChildItemExists(item, childItemId);
if(childItemExists) return true; // else continue search in other children
}
}
return isChildExisting;
}
我不确定您在 if
声明中的条件是否正确。如果您的所有项目都符合搜索条件,您可以尝试使用该代码。您应该 运行 对每个根项执行此过程,或者将人工根项传递给该函数。
public bool CheckIfChildItemExists(Item parentItem, long childItemId)
{
if(parentItem.ItemID == childItemId && parentItem.IsActive) return true;
foreach (Item item in parentItem.Children)
{
var childItemExists = CheckIfChildItemExists(item, childItemId);
if(childItemExists) return true; // else continue search in
}
return false;
}
在 item == context.Items.Where(x => x.ItemID == childItemId && x.IsActive).FirstOrDefault()
中,您实际上是在比较两个对象,为什么不直接检查 item
对象的条件呢?此外,您可以使用 context.Items.FirstOrDefault(x => x.ItemID == childItemId && x.IsActive)
代替。