page156.html

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

HTML
101
字号
<HTML><HEAD><TITLE>Applications</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="tex2html3001" HREF="page157.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2999" HREF="page147.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2995" HREF="page155.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3003" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION006230000000000000000">Applications</A></H2><A NAME="secqueuesapps">&#160;</A><P>The FIFO nature of queues makes them useful in certain algorithms.For example, we will see in Chapter&nbsp;<A HREF="page519.html#chapgraphs"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> that a queueis an essential data structure for many different graph algorithms.In this section we illustrate the use of a queuein the <em>breadth-first traversal</em><A NAME=6740>&#160;</A> of a tree.<P>Figure&nbsp;<A HREF="page156.html#figlot1"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows an example of a tree.A tree is comprised of <em>nodes</em><A NAME=6743>&#160;</A> (indicated by the circles)and <em>edges</em><A NAME=6745>&#160;</A> (shown as arrows between nodes).We say that the edges point from the <em>parent</em><A NAME=6747>&#160;</A> nodeto a <em>child</em><A NAME=6749>&#160;</A> node.The <em>degree</em><A NAME=6751>&#160;</A> of a nodeis equal to the number of children of that node.For example, node&nbsp;A in Figure&nbsp;<A HREF="page156.html#figlot1"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> has degree threeand its children are nodes&nbsp;B, C, and&nbsp;D.A child and all of its descendents is called a <em>subtree</em><A NAME=6754>&#160;</A>.<P><P><A NAME="7016">&#160;</A><A NAME="figlot1">&#160;</A> <IMG WIDTH=575 HEIGHT=195 ALIGN=BOTTOM ALT="figure6755" SRC="img698.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 versionof the abstract <tt>Tree</tt> class described in Chapter&nbsp;<A HREF="page251.html#chaptrees"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<PRE>class Tree:    key = property(fget = ...)    degree = property(fget = ...)    def getSubtree(self, i): pass    getSubtree = abstractmethod(getSubtree)    # ...</PRE><P>Each node in a tree is representedby an instance of a class derived from the abstract <tt>Tree</tt> class.The <tt>key</tt> property provides a get accessorthat returns an object which representsthe contents of the node.E.g. in Figure&nbsp;<A HREF="page156.html#figlot1"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>, each node carries a labelso the <tt>key</tt> property would return a <tt>str</tt> valuethat represents that label.The <tt>degree</tt> property provides a get accessorthat returns the degree of the nodeand the <tt>getSubtree</tt> method takes an <tt>int</tt> argument <tt>i</tt>and returns the corresponding child of that node.<P>One of the essential operations on a treeis a <em>tree traversal</em><A NAME=7031>&#160;</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 computationusing the information contained in that node--e.g., print the key.The standard tree traversals are discussed in Chapter&nbsp;<A HREF="page251.html#chaptrees"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.In this section we consider a traversal which is based onthe levels of the nodes in the tree.<P>Each node in a tree has an associated levelwhich arises from the position of that node in the tree.For example, node&nbsp;A in Figure&nbsp;<A HREF="page156.html#figlot1"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is at level&nbsp;0,nodes&nbsp;B, C, and&nbsp;D are at level&nbsp;1, etc.A <em>breadth-first traversal</em><A NAME=7037>&#160;</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=7039>&#160;</A>.The breadth-first traversal of the tree in Figure&nbsp;<A HREF="page156.html#figlot1"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../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 treeis 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&nbsp;<A HREF="page156.html#figlot2"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates the breadth-first traversal algorithmby showing the contents of the queueimmediately prior to each iteration.<P><P><A NAME="7423">&#160;</A><A NAME="figlot2">&#160;</A> <IMG WIDTH=575 HEIGHT=542 ALIGN=BOTTOM ALT="figure7044" SRC="img699.gif"  ><BR><STRONG>Figure:</STRONG> Queue contents during the breadth-first traversal 	of the tree in Figure&nbsp;<A HREF="page156.html#figlot1"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<BR><P><BR> <HR><UL> <LI> <A NAME="tex2html3004" HREF="page157.html#SECTION006231000000000000000">Implementation</A></UL><HR><A NAME="tex2html3001" HREF="page157.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2999" HREF="page147.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2995" HREF="page155.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3003" 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 + -
显示快捷键?