<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="http://gentoo-zh.org/extern.php?action=feed&amp;tid=612&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[Gentoo中文社区 / 史上最简明易懂非递归遍历二叉树算法]]></title>
		<link>http://www.gentoo-zh.org/viewtopic.php?id=612</link>
		<description><![CDATA[史上最简明易懂非递归遍历二叉树算法 最近发表的帖子。]]></description>
		<lastBuildDate>Sun, 05 Feb 2023 02:25:57 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[Re: 史上最简明易懂非递归遍历二叉树算法]]></title>
			<link>http://www.gentoo-zh.org/viewtopic.php?pid=751#p751</link>
			<description><![CDATA[<p>看明白了，对逻辑有帮助，希望继续多写些帖子继续加油</p>]]></description>
			<author><![CDATA[dummy@example.com (Urit)]]></author>
			<pubDate>Sun, 05 Feb 2023 02:25:57 +0000</pubDate>
			<guid>http://www.gentoo-zh.org/viewtopic.php?pid=751#p751</guid>
		</item>
		<item>
			<title><![CDATA[史上最简明易懂非递归遍历二叉树算法]]></title>
			<link>http://www.gentoo-zh.org/viewtopic.php?pid=655#p655</link>
			<description><![CDATA[<p>巧若拙（欢迎转载，但请注明出处：http://blog.csdn.net/qiaoruozhuo）</p><p>遍历二叉树的递归函数是体现了算法之美的高妙算法，思路清晰，代码简洁，读之赏心悦目。代码如下：<br />程序代码：</p><p>void PreOrderTraverse_R(BiTree BT)//采用递归方式先序遍历二叉树BT<br />{<br />&#160; &#160; if(BT != NULL)<br />&#160; &#160; {<br />&#160; &#160; &#160; &#160; printf(&quot;%c&quot;, BT-&gt;data);//输出该结点（根结点） <br />&#160; &#160; &#160; &#160; PreOrderTraverse_R(BT-&gt;lchild);&#160; //遍历左子树 <br />&#160; &#160; &#160; &#160; PreOrderTraverse_R(BT-&gt;rchild);//遍历右子树<br />&#160; &#160; }<br />}</p><p>void InOrderTraverse_R(BiTree BT)//采用递归方式中序遍历二叉树BT<br />{<br />&#160; &#160; if(BT != NULL)<br />&#160; &#160; {<br />&#160; &#160; &#160; &#160; InOrderTraverse_R(BT-&gt;lchild);&#160; //遍历左子树 <br />&#160; &#160; &#160; &#160; printf(&quot;%c&quot;, BT-&gt;data);//输出该结点（根结点） <br />&#160; &#160; &#160; &#160; InOrderTraverse_R(BT-&gt;rchild);//遍历右子树<br />&#160; &#160; }<br />}</p><p>void PostOrderTraverse_R(BiTree BT)//采用递归方式后序遍历二叉树BT<br />{<br />&#160; &#160; if(BT != NULL)<br />&#160; &#160; {<br />&#160; &#160; &#160; &#160; PostOrderTraverse_R(BT-&gt;lchild);&#160; //遍历左子树 <br />&#160; &#160; &#160; &#160; PostOrderTraverse_R(BT-&gt;rchild);//遍历右子树<br />&#160; &#160; &#160; &#160; printf(&quot;%c&quot;, BT-&gt;data);//输出该结点（根结点） <br />&#160; &#160; }<br />}</p><br /><p>相较之下，大部分流传的非递归遍历二叉树算法语言晦涩，面目可憎，虽然利用了栈数据结构来模拟递归遍历过程，但思路和表达形式上未能与递归算法对应起来，造成初学者理解上的困惑。如果能够将非递归算法和递归算法相互对应，则可大大方便初学者理解二者间的等效性。<br />我们知道，编译器在调用函数时会分配一个栈空间，以存储必要信息，如果该函数调用了别的函数，其栈空间会一直保留，直到该函数最后一条语句执行完毕，才会释放其栈空间。在模拟非递归算法的时候，我们需要手工分配和释放栈空间。<br />三种不同的遍历方式区别在于栈空间的释放时机和输出结点信息时机的不同：先序和中序遍历是在访问栈顶元素的右孩子（右子树）之前退栈，而后序遍历在访问右子树之后退栈；先序遍历是在某结点入栈时输出其信息，而中序和后序遍历是在该结点退栈时输出其信息。<br />&#160; &#160; 无论是递归算法还是非递归算法，都遵循上述规则，二者可以一一对应。</p>]]></description>
			<author><![CDATA[dummy@example.com (batsom)]]></author>
			<pubDate>Fri, 09 Dec 2022 09:02:22 +0000</pubDate>
			<guid>http://www.gentoo-zh.org/viewtopic.php?pid=655#p655</guid>
		</item>
	</channel>
</rss>
