page308.html

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

HTML
105
字号
<HTML><HEAD><TITLE>Unsuccessful Search</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="tex2html4750" HREF="page309.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4748" HREF="page305.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4742" HREF="page307.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4752" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0010330000000000000000">Unsuccessful Search</A></H2><P>All successful searches terminate when the object of the search is found.Therefore, all successful searches terminate at an internal node.In contrast, all unsuccessful searches terminate at an external node.In terms of the binary tree shown in Figure&nbsp;<A HREF="page301.html#figtree12"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,a successful search terminates in one of the nodeswhich are drawn as a circlesand an unsuccessful search terminates in one of the boxes.<P>The preceding analysis shows that the average number of nodes visitedduring a successful search depends on the<em>internal path length</em><A NAME=18550>&#160;</A><A NAME=18551>&#160;</A>,which is simply the sum of the depths of all the internal nodes.Similarly, the average number of nodes visited during an unsuccessful searchdepends on the <em>external path length</em><A NAME=18553>&#160;</A><A NAME=18554>&#160;</A>,which is the sum of the depths of all the external nodes.Fortunately, there is a simple relationship between the internalpath length and the external path length of a binary tree.<P><BLOCKQUOTE> <b>Theorem</b><A NAME="theoremsrchtreei">&#160;</A>Consider a binary tree <I>T</I> with <I>n</I> internal nodesand an internal path length of <I>I</I>.The external path length of <I>T</I> is given by<P> <IMG WIDTH=291 HEIGHT=12 ALIGN=BOTTOM ALT="displaymath64007" SRC="img1217.gif"  ><P></BLOCKQUOTE><P>In other words, Theorem&nbsp;<A HREF="page308.html#theoremsrchtreei"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> says that the<em>difference</em> between the internal path lengthand the external path length of a binary tree with <I>n</I> internal nodesis <I>E</I>-<I>I</I>=2<I>n</I>.<P>	extbfProof (By induction).<P><b>Base Case</b>Consider a binary tree with one internal node and internal path length of zero.Such a tree has exactly two empty subtrees immediately below the root andits external path length is two.Therefore, the theorem holds for <I>n</I>=1.<P><b>Inductive Hypothesis</b>Assume that the theorem holds for  <IMG WIDTH=112 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline64023" SRC="img1218.gif"  > for some  <IMG WIDTH=37 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59027" SRC="img353.gif"  >.Consider an arbitrary tree,  <IMG WIDTH=16 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline64027" SRC="img1219.gif"  >, that has <I>k</I> internal nodes.According to Theorem&nbsp;<A HREF="page256.html#theoremtreesi"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,  <IMG WIDTH=16 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline64027" SRC="img1219.gif"  > has <I>k</I>+1 external nodes.Let  <IMG WIDTH=13 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline64035" SRC="img1220.gif"  > and  <IMG WIDTH=18 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline64037" SRC="img1221.gif"  > be the internal and external path length of  <IMG WIDTH=16 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline64027" SRC="img1219.gif"  >,respectively,According to the inductive hypothesis,  <IMG WIDTH=91 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline64041" SRC="img1222.gif"  >.<P>Consider what happens when we create a new tree  <IMG WIDTH=32 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline64043" SRC="img1223.gif"  >by removing an external node from  <IMG WIDTH=16 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline64027" SRC="img1219.gif"  >and replacing it with an internal node that has two empty subtrees.Clearly, the resulting tree has <I>k</I>+1 internal nodes.Furthermore, suppose the external node we remove is at depth <I>d</I>.Then the internal path length of  <IMG WIDTH=32 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline64043" SRC="img1223.gif"  > is  <IMG WIDTH=95 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline64053" SRC="img1224.gif"  >and the external path length of  <IMG WIDTH=32 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline64043" SRC="img1223.gif"  > is <IMG WIDTH=275 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline64057" SRC="img1225.gif"  >.<P>The difference between the internal path length and the externalpath length of  <IMG WIDTH=32 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline64043" SRC="img1223.gif"  > is<P> <IMG WIDTH=500 HEIGHT=64 ALIGN=BOTTOM ALT="eqnarray18571" SRC="img1226.gif"  ><P>Therefore, by induction on <I>k</I>,the difference between the internal path lengthand the external path length of a binary tree with <I>n</I> internal nodes is 2<I>n</I>for all  <IMG WIDTH=38 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58983" SRC="img341.gif"  >.<P>Since the difference between the internal and external path lengthsof any tree with <I>n</I> internal nodes is 2<I>n</I>,then we can say the same thing about the <em>average</em> internaland external path lengths averaged over all search trees.Therefore, <I>E</I>(<I>n</I>), the average external path length of a binary search treeis given by<P> <IMG WIDTH=500 HEIGHT=65 ALIGN=BOTTOM ALT="eqnarray18576" SRC="img1227.gif"  ><P><P>A binary search tree with internal <I>n</I> nodes has <I>n</I>+1 external nodes.Thus, the average depth of an external node of a binary search treewith <I>n</I> internal nodes,  <IMG WIDTH=8 HEIGHT=10 ALIGN=BOTTOM ALT="tex2html_wrap_inline64081" SRC="img1228.gif"  >, is given by<P> <IMG WIDTH=500 HEIGHT=88 ALIGN=BOTTOM ALT="eqnarray18579" SRC="img1229.gif"  ><P><P>These very nice results are the <em>raison d'&#234;tre</em> for binary search trees.What they say is that the average number of nodes visitedduring either a successful or an unsuccessful searchin the average binary search tree having<I>n</I> nodes is  <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif"  >.We must remember, however,that these results are premised on the assumption that allpossible search trees of <I>n</I> nodes are equiprobable.It is important to be aware that in practicethis may not always be the case.<P><HR><A NAME="tex2html4750" HREF="page309.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4748" HREF="page305.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4742" HREF="page307.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html4752" 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 + -
显示快捷键?