page285.html

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

HTML
53
字号
<HTML><HEAD><TITLE>getKey, AttachKey and DetachKey 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="tex2html4488" HREF="page286.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4486" HREF="page281.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4480" HREF="page284.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4490" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION009644000000000000000"><tt>getKey</tt>, <tt>AttachKey</tt> and <tt>DetachKey</tt> Methods</A></H3><P>Program&nbsp;<A HREF="page284.html#prognaryTreeb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> also defines three operations for manipulatingthe root of an <I>N</I>-ary tree.The <tt>getKey</tt> method returns theobject contained in the root node of the tree.Clearly, this operation is not defined for the empty tree.If the tree is not empty, the running time of this method is <I>O</I>(1).<P>The purpose of <tt>attachKey</tt> is to insert the specified object into agiven <I>N</I>-ary tree at the root node.This operation is only defined for an empty tree.The <tt>attachKey</tt> method takes as its argument anobject to be inserted in the root nodeand assigns that object to the <tt>_key</tt> instance attribute.Since the node is no longer empty,it must have exactly <I>N</I> subtrees.Therefore, <I>N</I> new empty subtrees are created and attached to the node.The running time is <I>O</I>(<I>N</I>) since <I>N</I> subtrees are created,and the running time of the <tt>__init__</tt>method for an empty <I>N</I>-ary tree takes <I>O</I>(1).<P>Finally, <tt>detachKey</tt> is usedto remove the object from the root of a tree.In order that the tree which remains still conformsto Definition&nbsp;<A HREF="page256.html#defnnarytree"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,it is only permissible to remove the root from a leaf node.And upon removal, the leaf node becomes an empty tree.The implementation given in Program&nbsp;<A HREF="page284.html#prognaryTreeb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>throws an exception if an attempt is made to remove the rootfrom a non-leaf node.Otherwise, the node is a leaf which means that its <I>N</I> subtrees are all empty.When the root is detached,the array of subtrees is also discarded.The running time of this method is clearly <I>O</I>(1).<P><HR><A NAME="tex2html4488" HREF="page286.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4486" HREF="page281.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4480" HREF="page284.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4490" 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 + -
显示快捷键?