📄 page156.html
字号:
<HTML>
<HEAD>
<TITLE>Applications</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="tex2html3835" HREF="page157.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page157.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="tex2html3833" HREF="page147.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page147.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="tex2html3829" HREF="page155.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page155.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="tex2html3837" 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="tex2html3838" 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>
<H2><A NAME="SECTION007230000000000000000">Applications</A></H2>
<A NAME="secqueuesapps"> </A>
<P>
The FIFO nature of queues makes them useful in certain algorithms.
E.g., we will see in Chapter <A HREF="page523.html#chapgraphs" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page523.html#chapgraphs"><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> that a queue
is an essential data structure for many different graph algorithms.
In this section we illustrate the use of a queue
in the <em>breadth-first traversal</em><A NAME=7387> </A> of a tree.
<P>
Figure <A HREF="page156.html#figlot1" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html#figlot1"><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> shows an example of a tree.
A tree is comprised of <em>nodes</em><A NAME=7390> </A> (indicated by the circles)
and <em>edges</em><A NAME=7392> </A> (shown as arrows between nodes).
We say that the edges point from the <em>parent</em><A NAME=7394> </A> node
to a <em>child</em><A NAME=7396> </A> node.
The <em>degree</em><A NAME=7398> </A> of a node
is equal to the number of children of that node.
E.g., node A in Figure <A HREF="page156.html#figlot1" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html#figlot1"><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> has degree three
and its children are nodes B, C, and D.
A child and all of its descendents is called a <em>subtree</em><A NAME=7401> </A>.
<P>
<P><A NAME="7669"> </A><A NAME="figlot1"> </A> <IMG WIDTH=575 HEIGHT=195 ALIGN=BOTTOM ALT="figure7402" SRC="img748.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img748.gif" ><BR>
<STRONG>Figure:</STRONG> A Tree<BR>
<P>
<P>
One way to represent such a tree is to use a collection of linked structures.
Consider the following class definition which is an abridged version
of the <tt>NaryTree</tt> class described in Chapter <A HREF="page250.html#chaptrees" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page250.html#chaptrees"><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>.
<PRE>class NaryTree : public Object
{
Object* key;
unsigned int const degree;
Array<NaryTree*> subtree;
public:
Object& Key () const;
unsigned int Degree () const;
NaryTree& Subtree (unsigned int) const;
};</PRE>
<P>
Each <tt>NaryTree</tt> object represents one node in a tree.
The member variable <tt>degree</tt> keeps track of the degree of the node
and the variable <tt>subtree</tt> is an array of pointers to
the children of the node.
The <tt>key</tt> field points to an object which represents
the contents of the node.
E.g. in Figure <A HREF="page156.html#figlot1" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html#figlot1"><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>, each node carries a one-character label.
The <tt>key</tt> field is intended to be used to represent that label.
The member functions <tt>Key</tt>, <tt>Degree</tt>, and <tt>Subtree</tt>
provide read-only access to the corresponding private member variables.
<P>
One of the essential operations on a tree
is a <em>tree traversal</em><A NAME=7684> </A>.
A traversal <em>visits</em> one-by-one all the nodes in a given tree.
To <em>visit a node</em> means to perform some computation
using the information contained in that node--e.g., print the key.
The standard tree traversals are discussed in Chapter <A HREF="page250.html#chaptrees" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page250.html#chaptrees"><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>.
In this section we consider a traversal which is based on
the levels of the nodes in the tree.
<P>
Each node in a tree has an associated level
which arises from the position of that node in the tree.
E.g., node A in Figure <A HREF="page156.html#figlot1" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html#figlot1"><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> is at level 0,
nodes B, C, and D are at level 1, etc.
A <em>breadth-first traversal</em><A NAME=7690> </A>
visits the nodes of a tree in the order of their levels.
At each level, the nodes are visited from left to right.
For this reason, it is sometimes also called a
<em>level-order traversal</em><A NAME=7692> </A>.
The breadth-first traversal of the tree in Figure <A HREF="page156.html#figlot1" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html#figlot1"><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>
visits the nodes from A to L in alphabetical order.
<P>
One way to implement a breadth-first traversal of a tree
is to make use of a queue as follows:
To begin the traversal, the root node of the tree is enqueued.
Then, we repeat the following steps until the queue is empty:
<OL><LI> Dequeue and visit the first node in the queue.<LI> Enqueue its children in order from left to right.
</OL>
Figure <A HREF="page156.html#figlot2" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html#figlot2"><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> illustrates the breadth-first traversal algorithm
by showing the contents of the queue
immediately prior to each iteration.
<P>
<P><A NAME="8073"> </A><A NAME="figlot2"> </A> <IMG WIDTH=575 HEIGHT=541 ALIGN=BOTTOM ALT="figure7697" SRC="img749.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img749.gif" ><BR>
<STRONG>Figure:</STRONG> Queue Contents during the Breadth-First Traversal of the Tree in Figure <A HREF="page156.html#figlot1" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page156.html#figlot1"><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><BR>
<P><BR> <HR>
<UL>
<LI> <A NAME="tex2html3839" HREF="page157.html#SECTION007231000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page157.html#SECTION007231000000000000000">Implementation</A>
</UL>
<HR><A NAME="tex2html3835" HREF="page157.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page157.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="tex2html3833" HREF="page147.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page147.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="tex2html3829" HREF="page155.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page155.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="tex2html3837" 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="tex2html3838" 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 © 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 + -