📄 page269.html
字号:
<HTML><HEAD><TITLE>Depth-First Traversal</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="tex2html4307" HREF="page270.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4305" HREF="page268.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4299" HREF="page268.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4309" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION009611000000000000000">Depth-First Traversal</A></H3><P>Program <A HREF="page269.html#progtreeb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>depthFirstTraversal</tt> methodof the abstract <tt>Tree</tt> class.In addition to <tt>self</tt>,the traversal method takes one argument--any object that is an instance of a subsclass ofthe <tt>PrePostVisitor</tt> class defined in Program <A HREF="page269.html#progprePostVisitora"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="15833"> </A><A NAME="progtreeb"> </A> <IMG WIDTH=575 HEIGHT=218 ALIGN=BOTTOM ALT="program15572" SRC="img1109.gif" ><BR><STRONG>Program:</STRONG> Abstract <tt>Tree</tt> class <tt>depthFirstTraversal</tt> method.<BR><P><P>A <tt>PrePostVisitor</tt> is a visitor with three methods,<tt>preVisit</tt>, <tt>inVisit</tt>, <tt>postVisit</tt>,and the propderty <tt>isDone</tt>.During a depth-first traversal,the <tt>preVisit</tt> and <tt>postVisit</tt>methods are each called once for every node in the tree.(The <tt>inVisit</tt> method is provided for binary treesand is discussed in Section <A HREF="page292.html#sectreesbintraversal"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).<P><P><A NAME="15835"> </A><A NAME="progprePostVisitora"> </A> <IMG WIDTH=575 HEIGHT=294 ALIGN=BOTTOM ALT="program15590" SRC="img1110.gif" ><BR><STRONG>Program:</STRONG> <tt>PrePostVisitor</tt> class.<BR><P><P>The depth-first traversal method first callsthe <tt>preVisit</tt> method with the object in the root node.Then, it calls recursively the <tt>depthFirstTraversal</tt> methodfor each subtree of the given node.After all the subtrees have been visited,the <tt>postVisit</tt> method is called.Assuming that the <tt>isEmpty</tt>, <tt>key</tt>, and <tt>getSubtree</tt>operations all run in constant time,the total running time of the <tt>depthFirstTraversal</tt> method is<P> <IMG WIDTH=375 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath63265" SRC="img1111.gif" ><P>where <I>n</I> is the number of nodes in the tree, <IMG WIDTH=71 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63269" SRC="img1112.gif" > is the running time of <tt>preVisit</tt>,and <IMG WIDTH=78 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63271" SRC="img1113.gif" > is the running time of <tt>postVisit</tt>.<P><HR><A NAME="tex2html4307" HREF="page270.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4305" HREF="page268.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4299" HREF="page268.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4309" 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 + -