📄 c#
字号:
lang=EN-US>6</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">种:<BR><IMG
alt="" src="C#与数据结构--二叉树的遍历 - abatei - 博客园.files/6-12.jpg" border=0><BR>
</P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">
先左后右</SPAN><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">
</SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">先右后左</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">先序</SPAN><SPAN
lang=EN-US><SPAN style="mso-spacerun: yes">
</SPAN>DLR<SPAN style="mso-spacerun: yes">
</SPAN>DRL</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中序</SPAN><SPAN
lang=EN-US><SPAN style="mso-spacerun: yes">
</SPAN>LDR<SPAN style="mso-spacerun: yes">
</SPAN>RDL</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">后序</SPAN><SPAN
lang=EN-US><SPAN style="mso-spacerun: yes">
</SPAN>LRD<SPAN style="mso-spacerun: yes">
</SPAN>RLD</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这里只讨论先左后右的三种遍历算法。<BR>
</P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如图</SPAN><SPAN
lang=EN-US>6.13</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所示,在沿着箭头方向所指的路径对二叉树进行遍历时,每个节点会在这条搜索路径上会出现三次,而访问操作只能进行一次,这时就需要决定在搜索路径上第几次出现的结点进行访问操作,由此就引出了三种不同的遍历算法。</SPAN></P><BR><IMG
alt="" src="C#与数据结构--二叉树的遍历 - abatei - 博客园.files/6-13.jpg" border=0><BR>
<P class=MsoNormal
style="MARGIN-LEFT: 42pt; TEXT-INDENT: -21pt; mso-list: l1 level1 lfo2; tab-stops: list 42.0pt"><STRONG
style="mso-bidi-font-weight: normal"><SPAN lang=EN-US
style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 华文楷体; mso-bidi-font-family: 楷体_GB2312"><SPAN
style="mso-list: Ignore">1.<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN></STRONG><STRONG style="mso-bidi-font-weight: normal"><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 华文楷体">先序遍历<SPAN
lang=EN-US><O:P></O:P></SPAN></SPAN></STRONG></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">若二叉树为非空,则过程为:</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 54pt; TEXT-INDENT: -27pt; mso-list: l0 level2 lfo1; tab-stops: list 54.0pt"><SPAN
lang=EN-US style="mso-bidi-font-family: 宋体"><SPAN
style="mso-list: Ignore">(1)<SPAN style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">访问根节点。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 54pt; TEXT-INDENT: -27pt; mso-list: l0 level2 lfo1; tab-stops: list 54.0pt"><SPAN
lang=EN-US style="mso-bidi-font-family: 宋体"><SPAN
style="mso-list: Ignore">(2)<SPAN style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">先序遍历左子树。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 54pt; TEXT-INDENT: -27pt; mso-list: l0 level2 lfo1; tab-stops: list 54.0pt"><SPAN
lang=EN-US style="mso-bidi-font-family: 宋体"><SPAN
style="mso-list: Ignore">(3)<SPAN style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">先序遍历右子树。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>6.13</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中,先序遍历就是把标号为</SPAN><SPAN
lang=EN-US>(1)</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的结点按搜索路径访问的先后次序连接起来,得出结果为:</SPAN><SPAN
lang=EN-US>ABDECF</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42pt; TEXT-INDENT: -21pt; mso-list: l1 level1 lfo2; tab-stops: list 42.0pt"><STRONG
style="mso-bidi-font-weight: normal"><SPAN lang=EN-US
style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 华文楷体; mso-bidi-font-family: 楷体_GB2312"><SPAN
style="mso-list: Ignore">2.<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN></STRONG><STRONG style="mso-bidi-font-weight: normal"><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 华文楷体">中序遍历<SPAN
lang=EN-US><O:P></O:P></SPAN></SPAN></STRONG></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">若二叉树为非空,则过程为:</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 54pt; TEXT-INDENT: -27pt; mso-list: l1 level2 lfo2; tab-stops: list 54.0pt"><SPAN
lang=EN-US style="mso-bidi-font-family: 宋体"><SPAN
style="mso-list: Ignore">(1)<SPAN style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">按中序遍历左子树。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 54pt; TEXT-INDENT: -27pt; mso-list: l1 level2 lfo2; tab-stops: list 54.0pt"><SPAN
lang=EN-US style="mso-bidi-font-family: 宋体"><SPAN
style="mso-list: Ignore">(2)<SPAN style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">访问根结点。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 54pt; TEXT-INDENT: -27pt; mso-list: l1 level2 lfo2; tab-stops: list 54.0pt"><SPAN
lang=EN-US style="mso-bidi-font-family: 宋体"><SPAN
style="mso-list: Ignore">(3)<SPAN style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">按中序遍历右子树。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>6.13</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中,先序遍历就是把标号为</SPAN><SPAN
lang=EN-US>(2)</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的结点按搜索路径访问的先后次序连接起来,得出结果为:</SPAN><SPAN
lang=EN-US>DBEACF</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 42pt; TEXT-INDENT: -21pt; mso-list: l1 level1 lfo2; tab-stops: list 42.0pt"><STRONG
style="mso-bidi-font-weight: normal"><SPAN lang=EN-US
style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 华文楷体; mso-bidi-font-family: 楷体_GB2312"><SPAN
style="mso-list: Ignore">3.<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN></STRONG><STRONG style="mso-bidi-font-weight: normal"><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-hansi-font-family: 华文楷体">后序遍历<SPAN
lang=EN-US><O:P></O:P></SPAN></SPAN></STRONG></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">若二叉树为非空,则过程为:</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 54pt; TEXT-INDENT: -27pt; mso-list: l1 level2 lfo2; tab-stops: list 54.0pt"><SPAN
lang=EN-US style="mso-bidi-font-family: 宋体"><SPAN
style="mso-list: Ignore">(1)<SPAN style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">按后序遍历左子树。</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 54pt; TEXT-INDENT: -27pt; mso-list: l1 level2 lfo2; tab-stops: list 54.0pt"><SPAN
lang=EN-US style="mso-bidi-font-family: 宋体"><SPAN
style="mso-list: Ignore">(2)<SPAN style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">按后序遍历右子树</SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: 54pt; TEXT-INDENT: -27pt; mso-list: l1 level2 lfo2; tab-stops: list 54.0pt"><SPAN
lang=EN-US style="mso-bidi-font-family: 宋体"><SPAN
style="mso-list: Ignore">(3)<SPAN style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">访问根结点。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>6.13</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中,先序遍历就是把标号为</SPAN><SPAN
lang=EN-US>(3)</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的结点按搜索路径访问的先后次序连接起来,得出结果为:</SPAN><SPAN
lang=EN-US>DEBFCA</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">【例</SPAN><SPAN
lang=EN-US>6-1<SPAN style="mso-spacerun: yes">
</SPAN>BinaryTreeNode.cs</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">】二叉树结点类</SPAN></P><BR>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN
style="COLOR: #008080"> 1</SPAN> <SPAN
style="COLOR: #0000ff">using</SPAN><SPAN
style="COLOR: #000000"> System;<BR></SPAN><SPAN
style="COLOR: #008080"> 2</SPAN> <SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">class</SPAN><SPAN
style="COLOR: #000000"> Node<BR></SPAN><SPAN
style="COLOR: #008080"> 3</SPAN> <SPAN
style="COLOR: #000000">{<BR></SPAN><SPAN
style="COLOR: #008080"> 4</SPAN> <SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">成员变量</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #008080"> 5</SPAN> <SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">private</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">object</SPAN><SPAN
style="COLOR: #000000"> _data; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">数据</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #008080"> 6</SPAN> <SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">private</SPAN><SPAN
style="COLOR: #000000"> Node _left; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">左孩子</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #008080"> 7</SPAN> <SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">private</SPAN><SPAN
style="COLOR: #000000"> Node _right; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">右孩子</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #008080"> 8</SPAN> <SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">object</SPAN><SPAN
style="COLOR: #000000"> Data<BR></SPAN><SPAN
style="COLOR: #008080"> 9</SPAN> <SPAN
style="COLOR: #000000"> {<BR></SPAN><SPAN
style="COLOR: #008080">10</SPAN> <SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">get</SPAN><SPAN
style="COLOR: #000000"> { </SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -