page278.html

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

HTML
38
字号
<HTML><HEAD><TITLE>__init__ and Purge 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="tex2html4408" HREF="page279.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4406" HREF="page276.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4400" HREF="page277.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4410" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION009632000000000000000"><tt>__init__</tt> and <tt>Purge</tt> Methods</A></H3><P>Program&nbsp;<A HREF="page276.html#proggeneralTreea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>GeneralTree</tt><tt>__init__</tt> and <tt>Purge</tt> methods.According to Definition&nbsp;<A HREF="page252.html#defntree"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,a general tree must contain at least one node--an empty tree is not allowed.Therefore,in addition to <tt>self</tt>,the <tt>__init__</tt> method takes one argument.The <tt>__init__</tt> method initializes the instance attributes as follows:The <tt>_key</tt> instance attribute is assigned the argument;the <tt>_degree</tt> instance attribute is set to zero; and,the <tt>_list</tt> instance attribute is assigned an empty linked list.The running time of the <tt>__init__</tt> method is clearly <I>O</I>(1).<P>The <tt>purge</tt> method of a container normally empties the container.In this case, the container is a general tree which is not allowed to be empty.Thus, the <tt>purge</tt> method shown in Program&nbsp;<A HREF="page276.html#proggeneralTreea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>discards the subtrees of the tree, but it does not discard the root.The running time of the <tt>purge</tt> method is clearly <I>O</I>(1).<P><HR><A NAME="tex2html4408" HREF="page279.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4406" HREF="page276.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4400" HREF="page277.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4410" 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 + -
显示快捷键?