Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Successful Search

When a search is successful, exactly d+1 internal nodes are visited, where d 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 search to appear in any node of the search tree. In that case, the average number of nodes visited during a successful search is tex2html_wrap_inline63663, where tex2html_wrap_inline63665 is the average of the depths of the nodes in a given tree. That is, given a binary search tree with n>0 nodes,

displaymath63655

where tex2html_wrap_inline63669 is the depth of the tex2html_wrap_inline57621 node of the tree.

The quantity tex2html_wrap_inline63673 is called the internal path length  . 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 equal to the internal path length divided by n, the number of nodes in the tree.

Unfortunately, for any given number of nodes n, 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 n nodes, we must consider all possible trees with n nodes. In the absence of any contrary information, we shall assume that all trees having n nodes are equiprobable and then compute the average depth of a node in the average tree containing n nodes.

Let I(n) be the average internal path length of a tree containing n nodes. Consider first the case of n=1. Clearly, there is only one binary tree that contains one node--the tree of height zero. Therefore, I(1)=0.

Now consider an arbitrary tree, tex2html_wrap_inline63695, having tex2html_wrap_inline58757 internal nodes altogether, l of which are found in its left subtree, where tex2html_wrap_inline63701. Such a tree consists of a root, the left subtree with l internal nodes and and a right subtree with n-l-1 internal nodes. The average internal path length for such a tree is the sum of the average internal path length of the left subtree, I(l), plus that of the right subtree, I(n-l-1), plus n-1 because the nodes in the two subtrees are one level lower in tex2html_wrap_inline63695.

In order to determine the average internal path length for a tree with n nodes, we must compute the average of the internal path lengths of the trees tex2html_wrap_inline63695 averaged over all possible sizes, l, of the (left) subtree, tex2html_wrap_inline63701.

To do this we consider an ordered set of n distinct keys, tex2html_wrap_inline63725. If we select the tex2html_wrap_inline63727 key, tex2html_wrap_inline63729, to be the root of a binary search tree, then there are l keys, tex2html_wrap_inline63733, tex2html_wrap_inline63509, ..., tex2html_wrap_inline63737, in its left subtree and n-l-1 keys, tex2html_wrap_inline63741, tex2html_wrap_inline63743, ..., tex2html_wrap_inline63513 in its right subtree.

If we assume that it is equally likely for any of the n keys to be selected as the root, then all the subtree sizes in the range tex2html_wrap_inline63701 are equally likely. Therefore, the average internal path length for a tree with tex2html_wrap_inline58757 nodes is

eqnarray18367

Thus, in order to determine I(n) we need to solve the recurrence

  equation18377

To solve this recurrence we consider the case n>1 and then multiply Equation gif by n to get

  equation18387

Since this equation is valid for any n>1, by substituting n-1 for n we can also write

  equation18392

which is valid for n>2. Subtracting Equation gif from Equation gif gives

displaymath63656

which can be rewritten as

  equation18399

Thus, we have shown the solution to the recurrence in Equation gif is the same as the solution of the recurrence

  equation18404


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.