page550.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 50 行

HTML
50
字号
<HTML><HEAD><TITLE>Implementation</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html7487" HREF="page551.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7485" HREF="page549.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7479" HREF="page549.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7489" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0016311000000000000000">Implementation</A></H3><P>Program&nbsp;<A HREF="page550.html#proggraphd"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the code for the <tt>depthFirstTraversal</tt>method of the abstract <tt>Graph</tt> class.In addition to <tt>self</tt>,the <tt>DepthFirstTraversal</tt> methodtakes any <tt>PrePostVisitor</tt> and an integer.The idea is that the <tt>visit</tt> method of the visitoris called once for each vertex in the graphand the vertices are visited in depth-first traversal orderstarting from the vertex specified by the integer.<P><P><A NAME="50261">&#160;</A><A NAME="proggraphd">&#160;</A> <IMG WIDTH=575 HEIGHT=390 ALIGN=BOTTOM ALT="program50206" SRC="img2287.gif"  ><BR><STRONG>Program:</STRONG> Abstract <tt>Graph</tt> class <tt>depthFirstTraversal</tt> method.<BR><P><P>In order to ensure that each vertex is visited at most once,an array of length  <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline70975" SRC="img2249.gif"  > of <tt>bool</tt> valuescalled <tt>visited</tt> is used (line&nbsp;10).That is,  <IMG WIDTH=127 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline71179" SRC="img2288.gif"  > only if vertex <I>i</I> has been visited.All the array elements are initially <tt>False</tt> (lines&nbsp;5-7).After initializing the array,the <tt>depthFirstTraversal</tt> methodcalls the recursive <tt>_depthFirstTraversal</tt> method.passing it the array as the third argument.<P>The recursive <tt>_depthFirstTraversal</tt> emthodreturns immediately if the visitor is done.Otherwise, it visits the specified node,and then it follows all the edges emanating from that nodeand recursively visits the adjacent vertices<em>if those vertices have not already been visited</em>.<P><HR><A NAME="tex2html7487" HREF="page551.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7485" HREF="page549.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7479" HREF="page549.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html7489" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

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