⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page270.html

📁 wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq
💻 HTML
字号:
<HTML>
<HEAD>
<TITLE>Breadth-First Traversal</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
 <img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms 
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html5265" HREF="page271.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page271.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html5263" HREF="page267.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page267.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html5257" HREF="page269.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page269.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html5267" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html5268" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H3><A NAME="SECTION0010613000000000000000">Breadth-First Traversal</A></H3>
<P>
Program&nbsp;<A HREF="page270.html#progtree2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page270.html#progtree2c"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> defines the <tt>BreadthFirstTraversal</tt>
member function of the <tt>Tree</tt> class.
As defined in Section&nbsp;<A HREF="page257.html#sectreestraversals" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page257.html#sectreestraversals"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>,
a breadth-first traversal of a tree visits the nodes
in the order of their depth in the tree and
at each level the nodes are visited from left to right.
<P>
<P><A NAME="16493">&#160;</A><A NAME="progtree2c">&#160;</A> <IMG WIDTH=575 HEIGHT=429 ALIGN=BOTTOM ALT="program16281" SRC="img1163.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1163.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>Tree</tt> Class <tt>BreadthFirstTraversal</tt> 	Member Function Definition<BR>
<P>
<P>
We have already seen in Section&nbsp;<A HREF="page156.html#secqueuesapps" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html#secqueuesapps"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> a non-recursive
breadth-first traversal algorithm for <I>N</I>-ary trees.
This algorithm makes use of a queue as follows.
Initially, the root node of the given tree is enqueued,
provided it is not the empty tree.
Then, the following steps are repeated until the queue is empty:
<OL><LI> Remove the node at the head of the queue and call it <tt>head</tt>.<LI> Visit the object contained in <tt>head</tt>.<LI> Enqueue in order each non-empty subtree of <tt>head</tt>.
</OL>
Notice that empty trees are never put into the queue.
Furthermore, it should be obvious that each node of the tree
is enqueued exactly once.
Therefore, it is also dequeue exactly once.
Consequently, the running time for the breadth-first traversal
is  <IMG WIDTH=129 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline61328" SRC="img714.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img714.gif"  >.
<P>
There are several aspects of the way in which the queue is used here
that need to be explained.
First, the queue will be used to contain trees which are subtrees
of the given tree.
Since the subtrees of a tree are owned by the tree,
they cannot also be owned by the queue.
Therefore, on line&nbsp;4 of Program&nbsp;<A HREF="page270.html#progtree2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page270.html#progtree2c"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>,
the <tt>RescindOwnership</tt> function of the queue is called.
This is really conservative programming,
since the queue will normally be empty by the time its destructor is called
so it will should not have any contained objects to delete.
<P>
The <tt>BreadthFirstTraversal</tt> function is a <tt>const</tt> member function.
Therefore, it must not modify the given tree.
However, we need to push the tree onto the queue.
And since the <tt>Enqueue</tt> function
takes a non-<tt>const</tt> <tt>Object</tt> reference,
a <tt>constcast</tt> is required
on line&nbsp;7 of Program&nbsp;<A HREF="page270.html#progtree2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page270.html#progtree2c"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>
to cast away the <tt>const</tt>ness.
This is not an unsafe cast in this context,
because the queue is a local variable
of the <tt>BreadthFirstTraversal</tt> routine
and because the routine does not modify anything which it later dequeues
from the queue.
<P>
<HR><A NAME="tex2html5265" HREF="page271.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page271.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html5263" HREF="page267.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page267.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html5257" HREF="page269.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page269.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html5267" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html5268" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright &#169; 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.

</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

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