C#重构为递归方法



我正在尝试采用这种基本的迭代方法并使其递归。运气不太好。到目前为止,我已经尝试过这样的东西。

我的目标是最外层的循环;标题";无论在孩子的哪个位置标识Id。

尝试:

private string Fn( List<WorkflowHierarchy.Result> results, string id, string title )
{
foreach (var result in results)
{
if (string.IsNullOrEmpty(title))
{
title = result.title;
}

if (result.id == id)
{
break;
}
else
{
if (result.children != null && result.children.page.results.Any())
{
Fn(result.children.page.results, id, title);
}
else
{
title = null;
}
}
}
return title;
}

工作非递归方法:

private string GetParent(WorkflowHierarchy.Rootobject workFlowHierarchy, string id)
{
var parent = string.Empty;
foreach (var item in workFlowHierarchy.results)
{
if (item.id == id)
{
parent = item.title;
break;
}
else
{
if (item.children != null)
{
foreach (var item2 in item.children.page.results)
{
if (item2.id == id)
{
parent = item.title;
break;
}
else
{
if (item2.children != null)
{
foreach (var item3 in item2.children.page.results)
{
if (item3.id == id)
{
parent = item.title;
break;
}
else
{
if (item3.children != null)
{
foreach (var item4 in item3.children.page.results)
{
if (item4.id == id)
{
parent = item.title;
break;
}
else
{
if (item4.children != null)
{
foreach (var item5 in item4.children.page.results)
{
if (item5.id == id)
{
parent = item.title;
break;
}
else
{
if (item5.children != null)
{
foreach (var item6 in item5.children.page.results)
{
if (item6.id == id)
{
parent = item.title;
break;
}
else
{
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

return parent;
}

JSON结构示例:

给定ID:239108139返回父标题(我是根(

[
{
"id": "237109438",
"title": "I'm Root",
"children": {
"page": {
"results": [
{
"id": "236978371",
"title": "abc",
"children": {
"page": {
"results": [
{
"id": "236945706",
"title": "1234",
"children": {
"page": {
"results": [
]
}
}
},
{
"id": "239108139",
"title": "456",
"children": {
"page": {
"results": [
]
}
}
}
]
}
}
}
]
}
}
}
]

这样的事情不会成功吗?

private string GetParent(WorkflowHierarchy.Rootobject root, string id)
{
foreach (var item in root.results)
{
if (ContainsId(item, id)) return item.title;
}
return null;
}
private bool ContainsId(WorkflowHierarchy.Result item, string id)
{
if (item.id == id) return true;
if (item.children == null) return false;
foreach (var child in item.children.page.results)
{
if (ContainsId(child, id)) return true;
}
return false;
}

然后你可以打电话给:

var title = GetParent(workFlowHierarchy, "239108139");

您的代码看起来像BreadthF第一个S搜索(BFS

private string Fn(WorkflowHierarchy.Rootobject workFlowHierarchy, string id) {
//TODO: Put the right type (type of workFlowHierarchy.results item) 
Queue<MyItemType> agenda = new Queue<MyItemType>();
//TODO: Put the right type (type of workFlowHierarchy.results item) 
HashSet<MyItemType> completed = new HashSet<MyItemType>(); 
foreach (var item in workFlowHierarchy.results)
agenda.Enqueue(item); 
while (agenda.Count > 0) {
var item = agenda.Dequeue();
if (!completed.Add(item))
continue;
if (item.id == id)
return item.Parent.title;
foreach (var child in item.children.page.results)
if (!completed.Contains(child))
agenda.Enqueue(child);
};
return string.Empty;   
}

最新更新