page286.html

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

HTML
56
字号
<HTML><HEAD><TITLE>getSubtree,    attachSubtree and detachSubtree Methods</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="tex2html4497" HREF="page287.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4495" HREF="page281.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4491" HREF="page285.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4499" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION009645000000000000000"><tt>getSubtree</tt>,    <tt>attachSubtree</tt> and <tt>detachSubtree</tt> Methods</A></H3><P>Program&nbsp;<A HREF="page286.html#prognaryTreec"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the three methodsfor manipulating the subtrees of an <I>N</I>-ary tree.The <tt>getSubtree</tt> method takes as its argumentan <tt>int</tt>, <tt>i</tt>, which must be between 0 and <I>N</I>-1.It returns the  <IMG WIDTH=19 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline63295" SRC="img1124.gif"  > subtree of the given tree.Note that this operation is only defined for a non-empty <I>N</I>-ary tree.Given that the tree is not empty,the running time is <I>O</I>(1).<P><P><A NAME="16340">&#160;</A><A NAME="prognaryTreec">&#160;</A> <IMG WIDTH=575 HEIGHT=390 ALIGN=BOTTOM ALT="program16283" SRC="img1130.gif"  ><BR><STRONG>Program:</STRONG> <tt>NaryTree</tt> class <tt>getSubtree</tt>, 	<tt>attachSubtree</tt> and <tt>detachSubtree</tt> methods.<BR><P><P>In addition to <tt>self</tt>,the <tt>attachSubtree</tt> method takes two arguments.The first is an integer <tt>i</tt> between 0 and <I>N</I>-1.The second is an <tt>NaryTree</tt> instance.The purpose of this method is to make the <I>N</I>-ary tree specifiedby the second argument become the  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif"  > subtree of the given tree.It is only possible to attach a subtree to a non-empty node andit is only possible to attach a subtreein a place occupied by an empty subtree.If none of the exceptions are thrown,the running time of this method is simply <I>O</I>(1).<P>in addition to <tt>self</tt>,the <tt>detachSubtree</tt> method takes a single argument <tt>i</tt>which is an integer between 0 and <I>N</I>-1.This method removes the  <IMG WIDTH=19 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline63295" SRC="img1124.gif"  > subtree from a given <I>N</I>-arytree and returns that subtree.Of course, it is only possible to remove a subtree from a non-empty tree.Since every non-empty node must have <I>N</I> subtrees,when a subtree is removed it is replaced by an empty tree.Clearly, the running time is <I>O</I>(1) if we assume that no exceptions are thrown.<P><HR><A NAME="tex2html4497" HREF="page287.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4495" HREF="page281.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4491" HREF="page285.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4499" 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 + -
显示快捷键?