这个递归函数怎么写,在线等,急

hawk234 2009-11-01 01:03:07
这个递归函数怎么写,在线等,急

Results为自定义的类
class Results
{
string Title="";
ArrayList ResultList=new ArrayList();
}

class Form1
{
//根据text查找对应的Results
public Results FindResults(string text, ArraylList resultList)
{
for (int i = 0; i < resultList.Count; i++)
{
Results result = (Results)resultList[i];
if (result.Title == text) return result;
if (result.ResultList.Count > 0)
return FindResults(text, result.ResultList);
}
ruturn null;
}
//如果不要最后那个(ruturn null;),那么并非所有代码返回值
//但如果要ruturn null;当第一遍循环后最后一个页节点就直接返回null了,后面的循环无法进行下去。
应该怎么写了,高手帮忙看一下
}
...全文
556 54 打赏 收藏 转发到动态 举报
写回复
用AI写文章
54 条回复
切换为时间正序
请发表友善的回复…
发表回复
wartim 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 52 楼 hawk234 的回复:]
回答那么多,和我的代码都没本质区别.
这个问题关键是提前就返回了,还没取到值就返回了
[/Quote]

lz看都不看上面所有人的代码就妄下定论
比如我的代码里已经提到了
if (result.ResultList.Count > 0)
{
Results R=FindResults(text, result.ResultList);
if (R != null) // 很简单么,就是判断一下null就是了
return R;
}
love_cloud 2009-11-04
  • 打赏
  • 举报
回复
占位 学习
rightyeah 2009-11-04
  • 打赏
  • 举报
回复
up
ssmartboy 2009-11-04
  • 打赏
  • 举报
回复
天知道楼主动什么脑筋!这个应该跟踪发现才是捷径,讨论个鬼!
hawk234 2009-11-04
  • 打赏
  • 举报
回复
回答那么多,和我的代码都没本质区别.
这个问题关键是提前就返回了,还没取到值就返回了
TOTTI-10 2009-11-03
  • 打赏
  • 举报
回复
看了大家的讨论,受益匪浅啊。。。赞!
wangxianshou 2009-11-03
  • 打赏
  • 举报
回复
占位学习
绿色夹克衫 2009-11-03
  • 打赏
  • 举报
回复
加个Dictionary,判断是否已经被搜索过就可以了,或者修改一下results类,加入一个bool变量,
记录是否被搜索过就可以解决循环引用的问题。

LZ的递归写的有点乱,建议修改为

public Results FindResults(string text, Results result)
{
if(result.SearchFlag)
return null;
else if(result.Title == text)
return result;

result.SearchFlag = true;

foreach (Results item in result.ResultList)
if(FindResults(text,item) != null)
return item;

return null;
}
bbbbbb888888 2009-11-03
  • 打赏
  • 举报
回复
少if 块吧.
ssmartboy 2009-11-03
  • 打赏
  • 举报
回复

yzxdc有道理,什么都想到了,赞成!!!
cecilia2006 2009-11-02
  • 打赏
  • 举报
回复
用ref 找到了就赋值
navy113 2009-11-02
  • 打赏
  • 举报
回复
后面那个if少了else,这方法(说树)只能找到叶子节点。
happy664618843 2009-11-02
  • 打赏
  • 举报
回复
路过 飘 友情帮顶
yzxdc 2009-11-02
  • 打赏
  • 举报
回复
发现我这个方法也无法避免循环引用导致的死循环。
楼主啊,循环引用很难检查的,比如a->b->c->a最好能够从逻辑上避免,如果实在无法避免的话干脆加一个递归层数限制


class Results
{
string Title="";
ArrayList ResultList=new ArrayList();

private const int MAX = 100;
//检查本对象以及所有子孙是否有符合条件的,深度优先遍历,初次调用层数设1
public Results FindResults(string text,int count)
{
if(this.Title == text) return this;
foreach(Results child in ResultList)
{
if(count >= MAX) return null;
Results r = child.FindResults(text,count++);
if(r != null) return r;
}
return null;
}
}


yymycabbage 2009-11-02
  • 打赏
  • 举报
回复
lz,你这种非泛型的集合类写法已经out了,另外你的意思是不是想返回所有的匹配的results啊,
而不是找到一个就返回,而你的result类本身含有集合,会使得问题比较难懂啊,看看我写的代码是不是
你想要的东西。


class Results
{
public string Title = "";
public List<Results> ResultList = new List<Results>();
}

class Form1
{
//根据text查找对应的Results
public void FindResults(string text, List<Results> resultList, ref Results allFindResults)
{
for (int i = 0; i < resultList.Count; i++)
{
Results temp = resultList[i];
if (temp.Title == text) allFindResults.ResultList.Add(temp);
if (temp.ResultList.Count > 0)
FindResults(text, temp.ResultList, ref allFindResults);
}
}
}
yzxdc 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 ssmartboy 的回复:]
对不起应是R==Null
C# codeclass Form1
{//根据text查找对应的Resultspublic Results FindResults(string text, ArraylList resultList)
{
Results R;
Results result;
R=null;for (int i=0; i< resultList.Count|| R==Null; i++)
{
Results result= (Results)resultList[i];if (result.Title== text) R=resultelseif (result.ResultList.Count>0)
R=FindResults(text, result.ResultList);
}
ruturn R;
}

}
[/Quote]

你这个程序之所以会有问题时因为你遍历了所有结点,而悲剧的是楼主的数据结构不是树,有循环引用,因此就造成了死循环
yzxdc 2009-11-02
  • 打赏
  • 举报
回复
另外我不喜欢楼主的代码,不够模块化,查找方法因该封装在result类的内部,像这样

class Results
{
string Title="";
ArrayList ResultList=new ArrayList();

//检查本对象以及所有子孙是否有符合条件的,深度优先遍历
public Results FindResults(string text)
{
if(this.Title == text) return this;
foreach(Results child in ResultList)
{
Results r = child.FindResults(text);
if(r != null) return r;
}
return null;
}
}

yzxdc 2009-11-02
  • 打赏
  • 举报
回复
这么简单一个问题,怎么搞得这么复杂,应该这么写:


public Results FindResults(string text, ArraylList resultList)
{
for (int i = 0; i < resultList.Count; i++)
{
Results result = (Results)resultList[i];
if (result.Title == text) return result;
//if (result.ResultList.Count > 0) 这个判断没必要

Results childresult = FindResults(text, result.ResultList);
if(childresult != null) return childresult; //找到第一个非空的就返回,否则继续找下去

}
ruturn null;
}

wartim 2009-11-02
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsApplication125
{
public partial class Form1 : Form
{
class Results
{
public string Title = "";
public ArrayList ResultList = new ArrayList();
}

Results FindResults(string text, ArrayList resultList)
{
for (int i = 0; i < resultList.Count; i++)
{
Results result = (Results)resultList[i];
if (result.Title == text) return result;
if (result.ResultList.Count > 0)
{
Results R=FindResults(text, result.ResultList);
if (R != null)
return R;
}
}
return null;
}

public Form1()
{
InitializeComponent();

ArrayList Tree = new ArrayList();

Results R1 = new Results();
R1.Title = "1";
Tree.Add(R1);

Results R1_1 = new Results();
R1_1.Title = "1_1";
R1.ResultList.Add(R1_1);

Results R1_2 = new Results();
R1_2.Title = "1_2";
R1.ResultList.Add(R1_2);

Results R1_3 = new Results();
R1_3.Title = "1_3";
R1.ResultList.Add(R1_3);

Results R2 = new Results();
R2.Title = "2";
Tree.Add(R2);

Results R2_1 = new Results();
R2_1.Title = "2_1";
R2.ResultList.Add(R2_1);

Results R2_2 = new Results();
R2_2.Title = "2_2";
R2.ResultList.Add(R2_2);

Results R2_3 = new Results();
R2_3.Title = "2_3";
R2.ResultList.Add(R2_3);

Results R = FindResults("2", Tree);

if (R != null)
Show(R.ResultList); // 会显示2下的3个节点title
}

void Show(ArrayList R)
{
for(int i=0;i<R.Count ;i++)
{
MessageBox.Show(((Results)R[i]).Title);
Show(((Results)R[i]).ResultList);
}
}
}
}
yangqizhong 2009-11-02
  • 打赏
  • 举报
回复
用 yield return , 循环返回
加载更多回复(33)

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧