page306.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 109 行
HTML
109 行
<HTML><HEAD><TITLE>Successful 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="tex2html4728" HREF="page307.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4726" HREF="page305.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4720" HREF="page305.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4730" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0010310000000000000000">Successful Search</A></H2><P>When a search is successful,exactly <I>d</I>+1 internal nodes are visited,where <I>d</I> is the depth in the tree of object of the search.For example, if the object of the search is at the root which has depth zero,the search visits just one node--the root itself.Similarly, if the object of the search is at depth one,two nodes are visited, and so on.We shall assume that it is equally likely for the object of the searchto appear in any node of the search tree.In that case, the <em>average</em> number of nodes visited duringa successful search is <IMG WIDTH=35 HEIGHT=30 ALIGN=MIDDLE ALT="tex2html_wrap_inline63877" SRC="img1188.gif" >,where <IMG WIDTH=10 HEIGHT=15 ALIGN=BOTTOM ALT="tex2html_wrap_inline63879" SRC="img1189.gif" > is the average of the depths of the nodes in a given tree.That is, given a binary search tree with <I>n</I><I>></I>0 nodes,<P> <IMG WIDTH=294 HEIGHT=44 ALIGN=BOTTOM ALT="displaymath63869" SRC="img1190.gif" ><P>where <IMG WIDTH=13 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63883" SRC="img1191.gif" > is the depth of the <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif" > node of the tree.<P>The quantity <IMG WIDTH=52 HEIGHT=25 ALIGN=MIDDLE ALT="tex2html_wrap_inline63887" SRC="img1192.gif" > is calledthe <em>internal path length</em><A NAME=18400> </A><A NAME=18401> </A>.The internal path length of a tree is simply the sum of the depths (levels)of all the internal nodes in the tree.Clearly, the average depth of an internal node is equalto the internal path length divided by <I>n</I>,the number of nodes in the tree.<P>Unfortunately, for any given number of nodes <I>n</I>,there are many different possible search trees.Furthermore,the internal path lengths of the various possibilities are not equal.Therefore, to compute the average depth of a node in a tree with <I>n</I> nodes,we must consider all possible trees with <I>n</I> nodes.In the absence of any contrary information,we shall assume that all trees having <I>n</I> nodes are equiprobableand then compute the average depth of a node in the average treecontaining <I>n</I> nodes.<P>Let <I>I</I>(<I>n</I>) be the average internal path length of a tree containing <I>n</I> nodes.Consider first the case of <I>n</I>=1.Clearly, there is only one binary tree that contains one node--the tree of height zero.Therefore, <I>I</I>(1)=0.<P>Now consider an arbitrary tree, <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63909" SRC="img1193.gif" >,having <IMG WIDTH=38 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58983" SRC="img341.gif" > internal nodes altogether,<I>l</I> of which are found in its left subtree, where <IMG WIDTH=64 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63915" SRC="img1194.gif" >.Such a tree consists of a root,the left subtree with <I>l</I> internal nodes andand a right subtree with <I>n</I>-<I>l</I>-1 internal nodes.The average internal path length for such a tree is the sumof the average internal path length of the left subtree, <I>I</I>(<I>l</I>),plus that of the right subtree, <I>I</I>(<I>n</I>-<I>l</I>-1),plus <I>n</I>-1 because the nodes in the two subtreesare one level lower in <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63909" SRC="img1193.gif" >.<P>In order to determine the average internal path length fora tree with <I>n</I> nodes,we must compute the average of the internal path lengths of the trees <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63909" SRC="img1193.gif" > averaged over all possible sizes, <I>l</I>,of the (left) subtree, <IMG WIDTH=64 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63915" SRC="img1194.gif" >.<P>To do this we consider an ordered set of <I>n</I> distinct keys, <IMG WIDTH=144 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline63939" SRC="img1195.gif" >.If we select the <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline63941" SRC="img1196.gif" > key, <IMG WIDTH=11 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63943" SRC="img1197.gif" >, to be the root of a binary search tree,then there are <I>l</I> keys, <IMG WIDTH=13 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63947" SRC="img1198.gif" >, <IMG WIDTH=13 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63723" SRC="img1169.gif" >, ..., <IMG WIDTH=27 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline63951" SRC="img1199.gif" >,in its left subtree and<I>n</I>-<I>l</I>-1 keys, <IMG WIDTH=26 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline63955" SRC="img1200.gif" >, <IMG WIDTH=28 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline63957" SRC="img1201.gif" >, ..., <IMG WIDTH=31 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline63727" SRC="img1171.gif" > in its right subtree.<P>If we assume that it is equally likelyfor any of the <I>n</I> keys to be selected as the root,then all the subtree sizes in the range <IMG WIDTH=64 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63915" SRC="img1194.gif" > are equally likely.Therefore, the average internal path length for a tree with <IMG WIDTH=38 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58983" SRC="img341.gif" > nodes is<P> <IMG WIDTH=500 HEIGHT=102 ALIGN=BOTTOM ALT="eqnarray18408" SRC="img1202.gif" ><P><P>Thus, in order to determine <I>I</I>(<I>n</I>)we need to solve the recurrence<P><A NAME="eqnsrchtreerecurrence"> </A> <IMG WIDTH=500 HEIGHT=48 ALIGN=BOTTOM ALT="equation18418" SRC="img1203.gif" ><P><P>To solve this recurrence we consider the case <I>n</I><I>></I>1and then multiply Equation <A HREF="page306.html#eqnsrchtreerecurrence"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> by <I>n</I> to get<P><A NAME="eqnsrchtreea"> </A> <IMG WIDTH=500 HEIGHT=46 ALIGN=BOTTOM ALT="equation18428" SRC="img1204.gif" ><P>Since this equation is valid for any <I>n</I><I>></I>1,by substituting <I>n</I>-1 for <I>n</I> we can also write<P><A NAME="eqnsrchtreeb"> </A> <IMG WIDTH=500 HEIGHT=46 ALIGN=BOTTOM ALT="equation18433" SRC="img1205.gif" ><P>which is valid for <I>n</I><I>></I>2.Subtracting Equation <A HREF="page306.html#eqnsrchtreeb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> from Equation <A HREF="page306.html#eqnsrchtreea"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives<P> <IMG WIDTH=410 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath63870" SRC="img1206.gif" ><P>which can be rewritten as<P><A NAME="eqnsrchtreec"> </A> <IMG WIDTH=500 HEIGHT=34 ALIGN=BOTTOM ALT="equation18440" SRC="img1207.gif" ><P><P>Thus, we have shown the solution to the recurrencein Equation <A HREF="page306.html#eqnsrchtreerecurrence"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>is the same as the solution of the recurrence<P><A NAME="eqnsrchtreed"> </A> <IMG WIDTH=500 HEIGHT=67 ALIGN=BOTTOM ALT="equation18445" SRC="img1208.gif" ><P><HR><A NAME="tex2html4728" HREF="page307.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4726" HREF="page305.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4720" HREF="page305.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4730" 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 + -
显示快捷键?