<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Recursion, Part Six: Making CPS Work</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/08/15/recursion-part-six-making-cps-work.aspx</link><description>JScript doesn't support CPS natively but we can write another dispatch engine that makes it work. There's only ever one active continuation, so lets have a new rule: JScript CPS functions are allowed to return, but the last thing that they do must be</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Recursion, Part Six: Making CPS Work</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/08/15/recursion-part-six-making-cps-work.aspx#6063366</link><pubDate>Sat, 10 Nov 2007 20:21:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6063366</guid><dc:creator>PiersH</dc:creator><description>&lt;p&gt;great series, eric. here's a stack-based solution that doesn't use parent/sibling pointers or pointer rotations: &lt;/p&gt;
&lt;p&gt;		int treeDepth (Node node)&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			int depth = 0;&lt;/p&gt;
&lt;p&gt;			Stack&amp;lt;Node&amp;gt; rest = new Stack&amp;lt;Node&amp;gt; ();&lt;/p&gt;
&lt;p&gt;			while (node != null)&lt;/p&gt;
&lt;p&gt;			{&lt;/p&gt;
&lt;p&gt;				while (node != null)&lt;/p&gt;
&lt;p&gt;				{&lt;/p&gt;
&lt;p&gt;					rest.Push (node.right);&lt;/p&gt;
&lt;p&gt;					node = node.left;&lt;/p&gt;
&lt;p&gt;				}&lt;/p&gt;
&lt;p&gt;				depth = Math.Max (depth, rest.Count);&lt;/p&gt;
&lt;p&gt;				while (rest.Count &amp;gt; 0 &amp;amp;&amp;amp; (node = rest.Pop ()) == null)&lt;/p&gt;
&lt;p&gt;					;&lt;/p&gt;
&lt;p&gt;				rest.Push (null);&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;			return depth;&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6063366" width="1" height="1"&gt;</description></item><item><title>re: Recursion, Part Six: Making CPS Work</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/08/15/recursion-part-six-making-cps-work.aspx#452821</link><pubDate>Thu, 18 Aug 2005 00:32:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:452821</guid><dc:creator>Patrick Geiller</dc:creator><description>I'll push the recursion stack ! If your tree nodes are well connected (eg pointers to sibling, parent) you can walk them in a single loop, without recursion. &lt;br&gt;&lt;br&gt;In javascript :&lt;br&gt;&lt;br&gt;function	nodeMaxDepth(startNode)&lt;br&gt;{&lt;br&gt;	var	depth	= 0, maxDepth	= 0&lt;br&gt;	var	node	= startNode&lt;br&gt;	while (node)&lt;br&gt;	{&lt;br&gt;		if (node.firstChild)&lt;br&gt;		{&lt;br&gt;			node = node.firstChild&lt;br&gt;			depth++&lt;br&gt;			maxDepth = Math.max(depth, maxDepth)&lt;br&gt;		}&lt;br&gt;		while (node)&lt;br&gt;		{&lt;br&gt;			if (node == startNode)	return maxDepth&lt;br&gt;			if (node.nextSibling)&lt;br&gt;			{&lt;br&gt;				node = node.nextSibling&lt;br&gt;				break&lt;br&gt;			}&lt;br&gt;			node = node.parentNode&lt;br&gt;			depth--&lt;br&gt;		}	&lt;br&gt;	}&lt;br&gt;}	&lt;br&gt;alert(nodeMaxDepth(document.body))&lt;br&gt;&lt;br&gt;Much easier ;)&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=452821" width="1" height="1"&gt;</description></item></channel></rss>