page244.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 70 行
HTML
70 行
<HTML><HEAD><TITLE>Inserting Items</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="tex2html4014" HREF="page245.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4012" HREF="page242.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4006" HREF="page243.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4016" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION008642000000000000000">Inserting Items</A></H3><P>The method for inserting an item into a scatter tableusing open addressing is actually quite simple--find an unoccupied array location and then put the item in that location.To find an unoccupied array element,the array is probed according to a probing sequence.In this case, the probing sequence is linear probing.Program <A HREF="page244.html#progopenScatterTablec"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the methods neededto insert an item into the scatter table.<P><P><A NAME="13658"> </A><A NAME="progopenScatterTablec"> </A> <IMG WIDTH=575 HEIGHT=409 ALIGN=BOTTOM ALT="program13344" SRC="img1005.gif" ><BR><STRONG>Program:</STRONG> <tt>OpenScatterTable</tt> class <tt>c</tt>, <tt>findUnoccupied</tt> and <tt>insert</tt> methods.<BR><P><P>The method <tt>c</tt> defines the probing sequence.As it turns out,the implementation required for a linear probing sequence is trivial.The method <tt>c</tt> is the identity function.<P>The method <tt>findUnoccupied</tt>is to locate an unoccupied array position.The <tt>findUnoccupied</tt> method probes the arrayaccording the probing sequence determined by the <tt>c</tt> method.At most <I>n</I>+1 probes are made,where <IMG WIDTH=72 HEIGHT=9 ALIGN=BOTTOM ALT="tex2html_wrap_inline60691" SRC="img663.gif" > is the number of items in the scatter table.When using linear probing it is always possible to find an unoccupiedcell in this many probes as long as the table is not full.Notice also that we do not search for an <tt>EMPTY</tt> cell.Instead, the search terminates when a cell is found,the state of which is not <tt>OCCUPIED</tt>,that is, <tt>EMPTY</tt> or <tt>DELETED</tt>.The reason for this subtlety has to do withthe way items may be removed from the table.The <tt>findUnoccupied</tt> method returns a value between 0 and <I>M</I>-1,where <I>M</I> is the length of the scatter table,if an unoccupied location is found.Otherwise, it throws an exception that indicates that the table is full.<P>In addition to <tt>self</tt>,the <tt>insert</tt> method takes an objectand puts that object into the scatter table.It does so by calling <tt>findUnoccupied</tt> todetermine the location of an unoccupied entry in which to put the object.The state of the unoccupied entry is set to <tt>OCCUPIED</tt>and the object is saved in the entry.<P>The running time of the <tt>insert</tt> methodis determined by that of <tt>findUnoccupied</tt>.The worst case running time of <tt>findUnoccupied</tt> is <I>O</I>(<I>n</I>),where <I>n</I> is the number of items in the scatter table.Therefore,the running time of <tt>insert</tt> is <IMG WIDTH=101 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62215" SRC="img932.gif" >.<P><HR><A NAME="tex2html4014" HREF="page245.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4012" HREF="page242.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4006" HREF="page243.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4016" 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 + -
显示快捷键?