📄 page283.html
字号:
<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="tex2html4466" HREF="page284.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4464" HREF="page281.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4458" HREF="page282.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4468" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION009642000000000000000"><tt>__init__</tt> and <tt>purge</tt> Methods</A></H3><P>The <tt>__init__</tt> method for the <tt>NaryTree</tt> classis given in Program <A HREF="page281.html#prognaryTreea"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.In addition to <tt>self</tt>,the <tt>__init__</tt> method takes either one or two arguments.The first argument always an <tt>int</tt> that specifies the degree of the tree.If a second argument is not provided,the <tt>__init__</tt> method creates an empty tree.It does so by setting the <tt>_key</tt> instance attribute to <tt>None</tt>,and by setting the <tt>_subtree</tt> array to <tt>None</tt>.In this casethe running time of the <tt>__init__</tt> method is <I>O</I>(1).<P>When a second argument is providedit is taken to be an object to be stored in the tree.In this case,the <tt>__init__</tt> method creates a non-empty treein which the specified object occupies the root node.According to Definition <A HREF="page256.html#defnnarytree"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,every internal node in an <I>N</I>-ary tree must have exactly <I>N</I> subtrees.Therefore, the <tt>__init__</tt> method creates and attaches <I>N</I> empty subtreesto the root node.In this case,the running time of the <tt>__init__</tt> method is <I>O</I>(<I>N</I>),since <I>N</I> empty subtrees are created and constructedand constructing an empty <I>N</I>-ary tree takes <I>O</I>(1) time.<P><HR><A NAME="tex2html4466" HREF="page284.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4464" HREF="page281.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4458" HREF="page282.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4468" 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 + -