📄 page317.html
字号:
<HTML>
<HEAD>
<TITLE>Insert and AttachKey Member Functions</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html5846" HREF="page318.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page318.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html5844" HREF="page316.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page316.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html5840" HREF="page316.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page316.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html5848" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html5849" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H3><A NAME="SECTION0011421000000000000000"><tt>Insert</tt> and <tt>AttachKey</tt> Member Functions</A></H3>
<P>
The <tt>Insert</tt> member function of the <tt>BST</tt> class
is defined in Program <A HREF="page319.html#progbst4c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page319.html#progbst4c"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
This function takes as its lone argument a reference to
the object instance which is to be inserted into the binary search tree.
It is assumed in this implementation that duplicate keys are not permitted.
I.e., all of the keys contained in the tree are unique.
<P>
<P><A NAME="19521"> </A><A NAME="progbst3c"> </A> <IMG WIDTH=575 HEIGHT=544 ALIGN=BOTTOM ALT="program19430" SRC="img1299.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1299.gif" ><BR>
<STRONG>Program:</STRONG> <tt>BST</tt> Class <tt>Insert</tt>, <tt>AttachKey</tt> and <tt>Balance</tt> Member Function Definitions<BR>
<P>
<P>
The <tt>Insert</tt> routine behaves like the <tt>Find</tt> routine
until it arrives at an external, empty node.
Once the empty node has been found,
it is transformed into an internal node
by calling the <tt>AttachKey</tt> function.
<tt>AttachKey</tt> works as follows:
A pointer to the object being inserted
is saved in the <tt>key</tt> member variable
and two new empty binary trees are attached to the node.
<P>
Notice that after the insertion is done,
the function <tt>Balance</tt> is called.
However, as shown in Program <A HREF="page317.html#progbst3c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page317.html#progbst3c"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>,
the <tt>BST::Balance</tt> function does nothing.
Section <A HREF="page320.html#sectreesavl" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page320.html#sectreesavl"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> describes the class <tt>AVLTree</tt>
which is derived from the <tt>BST</tt> class
and which inherits the <tt>Insert</tt> function but
overrides the <tt>Balance</tt> operation.
<P>
The asymptotic running time of the <tt>Insert</tt> member function
is the same as that of <tt>Find</tt> for an unsuccessful search.
I.e., in the worst case the running time is <IMG WIDTH=188 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline64844" SRC="img1300.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1300.gif" >
and the average case running time is
<P> <IMG WIDTH=344 HEIGHT=17 ALIGN=BOTTOM ALT="displaymath64842" SRC="img1301.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1301.gif" ><P>
where <IMG WIDTH=151 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline64846" SRC="img1302.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1302.gif" > is the average depth of an external
node in a binary search tree with <I>n</I> internal nodes.
When <IMG WIDTH=179 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline64824" SRC="img1296.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1296.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_inline59891" SRC="img403.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img403.gif" >.
<P>
<HR><A NAME="tex2html5846" HREF="page318.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page318.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html5844" HREF="page316.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page316.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html5840" HREF="page316.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page316.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html5848" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html5849" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright © 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -