page316.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 58 行
HTML
58 行
<HTML><HEAD><TITLE>insert and attachKey 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="tex2html4839" HREF="page317.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4837" HREF="page315.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4833" HREF="page315.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4841" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0010421000000000000000"><tt>insert</tt> and <tt>attachKey</tt> Methods</A></H3><P>The <tt>insert</tt> method of the <tt>BinarySearchTree</tt> classis defined in Program <A HREF="page316.html#progbinarySearchTreec"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.This method takes as its argument the objectwhich is to be inserted into the binary search tree.It is assumed in this implementation that duplicate keys are not permitted.That is, all of the keys contained in the tree are unique.<P><P><A NAME="18801"> </A><A NAME="progbinarySearchTreec"> </A> <IMG WIDTH=575 HEIGHT=504 ALIGN=BOTTOM ALT="program18713" SRC="img1242.gif" ><BR><STRONG>Program:</STRONG> <tt>BinarySearchTree</tt> class <tt>insert</tt>, <tt>attachKey</tt> and <tt>balance</tt> methods.<BR><P><P>The <tt>insert</tt> method behaves like the <tt>find</tt> methoduntil it arrives at an external, empty node.Once the empty node has been found,it is transformed into an internal nodeby calling the <tt>attachKey</tt> method.<tt>attachKey</tt> works as follows:The object being inserted is assigned to the <tt>_key</tt> instance attributeand two new empty binary trees are attached to the node.<P>Notice that after the insertion is done,the <tt>balance</tt> method is called.However, as shown in Program <A HREF="page316.html#progbinarySearchTreec"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,the <tt>BinarySearchTree.balance</tt> method does nothing.(Section <A HREF="page319.html#sectreesavl"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> describes the class <tt>AVLTree</tt>which is derived from the <tt>BinarySearchTree</tt> classand which inherits the <tt>insert</tt> method butoverrides the <tt>balance</tt> operation).<P>The asymptotic running time of the <tt>insert</tt> methodis the same as that of <tt>find</tt> for an unsuccessful search.That is, in the worst case the running time is <IMG WIDTH=106 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63513" SRC="img1153.gif" >and the average case running time is<P> <IMG WIDTH=303 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath64161" SRC="img1243.gif" ><P>where <IMG WIDTH=152 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline64165" SRC="img1244.gif" > is the average depth of an externalnode in a binary search tree with <I>n</I> internal nodes.When <IMG WIDTH=96 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline64143" SRC="img1239.gif" >, the worst case running time is <I>O</I>(<I>n</I>)and the average case is <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif" >.<P><HR><A NAME="tex2html4839" HREF="page317.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4837" HREF="page315.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4833" HREF="page315.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4841" 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 + =
减小字号Ctrl + -
显示快捷键?