page207.html

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

HTML
71
字号
<HTML><HEAD><TITLE>Example</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="tex2html3589" HREF="page208.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3587" HREF="page206.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3583" HREF="page206.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3591" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H4><A NAME="SECTION008100100000000000000">Example</A></H4><P>We wish to implement a searchable containerwhich will be used to contain character strings from the set of strings <I>K</I>,<P> <IMG WIDTH=500 HEIGHT=41 ALIGN=BOTTOM ALT="equation10544" SRC="img822.gif"  ><P><P>Suppose we define a function  <IMG WIDTH=71 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline61513" SRC="img823.gif"  > as givenby the following table:<DIV ALIGN=CENTER><P ALIGN=CENTER><TABLE COLS=2 BORDER FRAME=HSIDES RULES=GROUPS><COL ALIGN=LEFT><COL ALIGN=RIGHT><TBODY><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>	<I>x</I> </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> <I>h</I>(<I>x</I>) </TD></TR></TBODY><TBODY><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><tt>&quot;ett&quot;</tt>  </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 1 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;tv&#229;&quot;</tt>  </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 2 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;tre&quot;</tt>  </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 3 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;fyra&quot;</tt> </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 4 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;fem&quot;</tt>  </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 5 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;sex&quot;</tt>  </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 6 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;sju&quot;</tt>  </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 7 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;&#229;tta&quot;</tt> </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 8 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;nio&quot;</tt>  </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 9 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;tio&quot;</tt>  </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 10 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;elva&quot;</tt> </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 11 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> 	<tt>&quot;tolv&quot;</tt> </TD><TD VALIGN=BASELINE ALIGN=RIGHT NOWRAP> 12 </TD></TR></TBODY></TABLE></P></DIV>Then, we can implement a searchable containerusing an array of length <I>n</I>=12.To insert item <I>x</I>, we simply store it a position <I>h</I>(<I>x</I>)-1 of the array.Similarly, to locate item <I>x</I>,we simply check to see if it is found at position <I>h</I>(<I>x</I>)-1.If the function  <IMG WIDTH=24 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58645" SRC="img261.gif"  > can be evaluated in constant time,then the both the insert and the find operations are <I>O</I>(1).<P>We expect that any reasonable implementationof the function  <IMG WIDTH=24 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58645" SRC="img261.gif"  > will run in constant time,since the size of the set of strings, <I>K</I>, is a constant!This example illustrates how we can achieve <I>O</I>(1) performancein the worst case when we have complete, <em>a priori</em> knowledge.<P><HR><A NAME="tex2html3589" HREF="page208.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3587" HREF="page206.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3583" HREF="page206.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3591" 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 + -
显示快捷键?