📄 page553.html
字号:
<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="tex2html7520" HREF="page554.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7518" HREF="page552.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7512" HREF="page552.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7522" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0016321000000000000000">Implementation</A></H3><P>Program <A HREF="page550.html#proggraphd"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the code for the <tt>breadthFirstTraversal</tt>method of the abstract <tt>Graph</tt> class.In addition to <tt>self</tt>,this method takes any <tt>Visitor</tt> and an integer.The <tt>visit</tt> method of the visitoris called once for each vertex in the graphand the vertices are visited in breadth-first traversal orderstarting from the vertex specified by the integer.<P><P><A NAME="50479"> </A><A NAME="proggraphe"> </A> <IMG WIDTH=575 HEIGHT=371 ALIGN=BOTTOM ALT="program50426" SRC="img2295.gif" ><BR><STRONG>Program:</STRONG> Abstract <tt>Graph</tt> class <tt>breadthFirstTraversal</tt> method.<BR><P><P>A <tt>bool</tt>-valued array, <tt>enqueued</tt>,is used to keep track of the vertices that have been put into the queue.The elements of the array are all initialized to <tt>False</tt> (lines 5-7).Next, a new queue is createdand the starting vertex is enqueued (lines 8-9).<P>The main loop of the <tt>breadthFirstTraversal</tt>method comprises lines 11-17.This loop continues as long as there is a vertex in the queueand the visitor is willing to do more work (line 11).In each iteration exactly one vertex is dequeued and visited (lines 12-13).After a vertex is visited,all the successors of that node are examined (lines 14-17).Every successor of the node that has not yet been enqueuedis put into the queueand the fact that it has been enqueued is recored in the array<tt>enqueued</tt> (lines 15-17).<P><HR><A NAME="tex2html7520" HREF="page554.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html7518" HREF="page552.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html7512" HREF="page552.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html7522" 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 © 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -