递归GetEnumerator() [英] recursive GetEnumerator()

查看:79
本文介绍了递归GetEnumerator()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



是否可以使用递归的GetEnumerator来遍历树状结构?


public IEnumerator< DTypeGetEnumerator()

{


返回GetEnumerator(root);


}


公共IEnumerator< DTypeGetEnumerator(节点p)

{


if(p.low!= null )


GetEnumerator(p.low);


收益率返回p.val;


if(p.high!= null)


GetEnumerator(p.high);


}


编译好,但是foreach只进行了1次迭代。


Colin = ^。^ =

解决方案
"科林" < co ********* @ ntworld.NOSPAM.com写在留言中

新闻:VB **************** @ newsfe6- win.ntli.net ...




是否可以使用递归的GetEnumerator来遍历树

结构?


公共IEnumerator< DTypeGetEnumerator()


{


返回GetEnumerator(root);


}


public IEnumerator< DTypeGetEnumerator(node p)


{


if(p.low!= null)


GetEnumerator(p.low);


收益率返回p.val;


if(p.high!= null)


GetEnumerator(p.high); < br $>

}


编译好,但是foreach只进行1次迭代。


Colin = ^。 ^ =



啊不用担心,我通过使用递归foreach代替

函数调用来进行递归。


Colin = ^。^ =


colin写道:




是否有可能有一个递归的GetEnumerator用于遍历树

结构?


public IEnumerator< DTypeGetEnumerator()

{


返回GetEnumerator(root);


}


public IEnumerator< DTypeGetEnumerator(node p)


{


if(p.low!= null)


GetEnumerator(p.low);



GetEnumerator方法返回一个枚举器。如果你只是扔掉

的返回值,几乎没有任何理由可以拨打电话。


yield return p。 VAL;



为什么返回后代码会继续?您是否忽略了编译器

关于无法访问代码的警告?


if(p.high!= null)

>
GetEnumerator(p.high);



再次注意返回值。


}


编译好,但foreach只进行1次迭代。



你的意思是它编译,但有警告...


-

G?ran Andersson

_____
http://www.guffa .com


" G?ran Andersson" < gu *** @ guffa.com写信息

新闻:呃************** @ TK2MSFTNGP05.phx.gbl ...


colin写道:


>
是否有可能有一个递归的GetEnumerator来遍历一个树
结构?

public IEnumerator< DTypeGetEnumerator()


返回GetEnumerator(root);

}

公共IEnumerator< DTypeGetEnumerator(节点p)

如果(p.low!= null)

GetEnumerator(p.low);



GetEnumerator方法返回一个枚举器。如果你只是丢掉

的返回值,几乎没有理由打电话,是吗?



我不认为在这种情况下它实际上会返回任何东西,至少不是那个

使用过的。

如果我尝试返回它抱怨的IEnumerator。


>


> yield return p.val;



返回后为什么代码会继续?您是否忽略了编译器

关于无法访问代码的警告?



这就是我相信收益的本质,但这是我第一次使用

吧,

所以我有点不确定,但它似乎只是产生控制,实际上并没有真正回报。

即当它来到accros一个''收益率表达''它executs

foreach循环中的代码,其值为expresion

当代码执行时,它继续''yield return expresion''

声明


>


> if(p.high!= null)

GetEnumerator(p.high);



再一次,请注意返回值。



它是递归的,因此该值不是从返回中使用的,而是来自

yield语句。


>编译好,但foreach只进行1次迭代。



你的意思是它编译,但有警告...



不要认为有任何警告总之,

但是自从使用foreach而不是调用另一个GetEnumerator

后它就可以了......


所以getenumerator对于所有的树节点,它都会调用getenumerator并且预先确认

.


Colin = ^。^ =


Hi,
is it possible to have a recursive GetEnumerator for traversing a tree
structure ?

public IEnumerator<DTypeGetEnumerator()

{

return GetEnumerator(root);

}

public IEnumerator<DTypeGetEnumerator(node p)

{

if(p.low!=null)

GetEnumerator(p.low);

yield return p.val;

if(p.high!=null)

GetEnumerator(p.high);

}

compiles ok, but foreach only does 1 iteration.

Colin =^.^=

解决方案

"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:VB****************@newsfe6-win.ntli.net...

Hi,
is it possible to have a recursive GetEnumerator for traversing a tree
structure ?

public IEnumerator<DTypeGetEnumerator()

{

return GetEnumerator(root);

}

public IEnumerator<DTypeGetEnumerator(node p)

{

if(p.low!=null)

GetEnumerator(p.low);

yield return p.val;

if(p.high!=null)

GetEnumerator(p.high);

}

compiles ok, but foreach only does 1 iteration.

Colin =^.^=

ah not to worry, i did the recursion by using recursive foreach instead of
the function call.

Colin =^.^=


colin wrote:

Hi,
is it possible to have a recursive GetEnumerator for traversing a tree
structure ?

public IEnumerator<DTypeGetEnumerator()

{

return GetEnumerator(root);

}

public IEnumerator<DTypeGetEnumerator(node p)

{

if(p.low!=null)

GetEnumerator(p.low);

The GetEnumerator method returns an enumerator. If you just throw away
the return value, there is hardly any reason to make the call, is there.

yield return p.val;

Why is the code continuing after return? Did you ignore the compiler
warning about unreachable code?

if(p.high!=null)

GetEnumerator(p.high);

Once again, mind the return value.

}

compiles ok, but foreach only does 1 iteration.

You mean that it compiles, but with warnings...

--
G?ran Andersson
_____
http://www.guffa.com


"G?ran Andersson" <gu***@guffa.comwrote in message
news:er**************@TK2MSFTNGP05.phx.gbl...

colin wrote:

>Hi,
is it possible to have a recursive GetEnumerator for traversing a tree
structure ?

public IEnumerator<DTypeGetEnumerator()

{

return GetEnumerator(root);

}

public IEnumerator<DTypeGetEnumerator(node p)

{

if(p.low!=null)

GetEnumerator(p.low);


The GetEnumerator method returns an enumerator. If you just throw away the
return value, there is hardly any reason to make the call, is there.

I dont think in this case it actually returns anything, at least not thats
used.
if I try and return an IEnumerator it complains.

>

>yield return p.val;


Why is the code continuing after return? Did you ignore the compiler
warning about unreachable code?

thats the nature of yield I beleive, however this is the first time ive used
it,
so im a bit unsure, but it seems it just yields control, doesnt actually
realy return.
ie when it comes accros a ''yield return expresion'' it executs
the code inside the foreach loop with the value in expresion
when that code has executed, it continues after the ''yield return expresion''
statement

>

>if(p.high!=null)

GetEnumerator(p.high);


Once again, mind the return value.

its recursive so the value isnt used from the return as such, but from the
yield statement.

>compiles ok, but foreach only does 1 iteration.


You mean that it compiles, but with warnings...

dont think there were any warnings at all,
however ive since used foreach instead of calling another GetEnumerator
and it works ok...

so getenumerator does foreach wich calls getenumerator and does foreach
recursivly for all the trees nodes.

Colin =^.^=


这篇关于递归GetEnumerator()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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