eulertour-eulertour.html

来自「经典的数据结构源代码(java 实现)」· HTML 代码 · 共 46 行

HTML
46
字号
<html><head><title>Code Fragment</title></head><body text=#000000><center></center><br><br><dl><dd><pre><font color=#ff0080>/**   * Template for algorithms traversing a binary tree using an Euler  * tour. The subclasses of this class will redefine some of the  * methods of this class to create a specific traversal.  */</font><font color=#8000a0>public</font> <font color=#8000a0><font color=#ff8000>abstract</font> </font><font color=#ff8000>class</font> EulerTour&lt;E, R&gt; {  <font color=#8000a0><font color=#8000a0>protected</font> </font>BinaryTree&lt;E&gt; tree;  <font color=#ff0080>/** Execution of the traversal. This abstract method must be   * specified in a concrete subclass. */</font>  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#ff8000>abstract</font> <font color=#8000a0>R </font><font color=#0000ff>execute</font>(BinaryTree&lt;E&gt; T);  <font color = #ff0080>/** Initialization of the traversal */</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font><font color=#8000a0>void</font> <font color=#0000ff>init</font>(BinaryTree&lt;E&gt; T) { tree = T; }  <font color = #ff0080>/** Template method */</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font>R <font color=#0000ff>eulerTour</font>(Position&lt;E&gt; v) {    TourResult&lt;R&gt; r = <font color=#8000a0><font color=#ff8000>new</font> </font>TourResult&lt;R&gt;<font color=#0000ff></font>();    <font color=#0000ff>visitLeft</font>(v, r);    <font color=#ff8000>if</font><font color=#0000ff> </font>(tree.<font color=#0000ff>hasLeft</font>(v))      r.left = <font color=#0000ff>eulerTour</font>(tree.<font color=#0000ff>left</font>(v));		<font color=#ff0080>// recursive traversal</font>    <font color=#0000ff>visitBelow</font>(v, r);    <font color=#ff8000>if</font><font color=#0000ff> </font>(tree.<font color=#0000ff>hasRight</font>(v))      r.right = <font color=#0000ff>eulerTour</font>(tree.<font color=#0000ff>right</font>(v));	<font color=#ff0080>// recursive traversal</font>    <font color=#0000ff>visitRight</font>(v, r);    <font color=#8000a0><font color=#ff8000>return</font> </font>r.out;  }  <font color=#ff0080>// Auxiliary methods that can be redefined by subclasses:</font>  <font color = #ff0080>/** Method called for the visit on the left */</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font><font color=#8000a0>void</font> <font color=#0000ff>visitLeft</font>(Position&lt;E&gt; v, TourResult&lt;R&gt; r) {}  <font color = #ff0080>/** Method called for the visit on from below */</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font><font color=#8000a0>void</font> <font color=#0000ff>visitBelow</font>(Position&lt;E&gt; v, TourResult&lt;R&gt; r) {}  <font color = #ff0080>/** Method called for the visit on the right */</font>  <font color=#8000a0><font color=#8000a0>protected</font> </font><font color=#8000a0>void</font> <font color=#0000ff>visitRight</font>(Position&lt;E&gt; v, TourResult&lt;R&gt; r) {}}</dl></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?