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

Complete Trees

 

The preceding chapter introduces the idea of a perfect tree (see Definition gif). Complete trees and perfect trees are closely related, yet quite distinct. As pointed out in the preceding chapter, a perfect binary tree of height h has exactly tex2html_wrap_inline66162 internal nodes. Since, the only permissible values of n are

displaymath66154

there is no perfect binary tree which contains, say 2, 4, 5, or 6 nodes.

However, we want a data structure that can hold an arbitrary number of objects so we cannot use a perfect binary tree. Instead, we use a complete binary tree, which is defined as follows:

Definition (Complete Binary Tree)  A complete binary tree   of height tex2html_wrap_inline63700, is a binary tree tex2html_wrap_inline66168 with the following properties.
  1. If h=0, tex2html_wrap_inline64524 and tex2html_wrap_inline64526.
  2. For h>0 there are two possibilities:
    1. tex2html_wrap_inline63784 is a perfect binary tree of height h-1 and tex2html_wrap_inline63786 is a complete binary tree of height h-1; or
    2. tex2html_wrap_inline63784 is a complete binary tree of height h-1 and tex2html_wrap_inline63786 is a perfect binary tree of height h-2.

Figure gif shows an example of a complete binary tree of height four. Notice that the left subtree of node 1 is a complete binary tree of height three; and the right subtree is a perfect binary tree of height two. This corresponds to case 2 (b) of Definition gif. Similarly, the left subtree of node 2 is a perfect binary tree of height two; and the right subtree is a complete binary tree of height two. This corresponds to case 2 (a) of Definition gif.

   figure24050
Figure: A Complete Binary Tree

Does there exist an complete binary with exactly n nodes for every integer n>0? The following theorem addresses this question indirectly by defining the relationship between the height of a complete tree and the number of nodes it contains.

Theorem  A complete binary tree of height tex2html_wrap_inline63700 contains at least tex2html_wrap_inline63824 and at most tex2html_wrap_inline63812 nodes.

extbfProof First, we prove the lower bound by induction. Let tex2html_wrap_inline66204 be the minimum number of nodes in a complete binary tree of height h. To prove the lower bound we must show that tex2html_wrap_inline66208.

Base Case There is exactly one node in a tree of height zero. Therefore, tex2html_wrap_inline66210.

Inductive Hypothesis Assume that tex2html_wrap_inline66208 for tex2html_wrap_inline63710, for some tex2html_wrap_inline61524. Consider the complete binary tree of height k+1 which has the smallest number of nodes. Its left subtree is a complete tree of height k having the smallest number of nodes and its right subtree is a perfect tree of height k-1.

From the inductive hypothesis, there are tex2html_wrap_inline58821 nodes in the left subtree and there are exactly tex2html_wrap_inline66226 nodes in the perfect right subtree. Thus,

eqnarray24231

Therefore, by induction tex2html_wrap_inline66208 for all tex2html_wrap_inline63700, which proves the lower bound.

Next, we prove the upper bound by induction. Let tex2html_wrap_inline66232 be the maximum number of nodes in a complete binary tree of height h. To prove the upper bound we must show that tex2html_wrap_inline66236.

Base Case There is exactly one node in a tree of height zero. Therefore, tex2html_wrap_inline66238.

Inductive Hypothesis Assume that tex2html_wrap_inline66236 for tex2html_wrap_inline63710, for some tex2html_wrap_inline61524. Consider the complete binary tree of height k+1 which has the largest number of nodes. Its left subtree is a perfect tree of height k and its right subtree is a complete tree of height k having the largest number of nodes.

There are exactly tex2html_wrap_inline66252 nodes in the perfect left subtree. From the inductive hypothesis, there are tex2html_wrap_inline66252 nodes in the right subtree. Thus,

eqnarray24243

Therefore, by induction tex2html_wrap_inline66236 for all tex2html_wrap_inline63700, which proves the upper bound.

It follows from Theorem gif that there exists exactly one complete binary tree that contains exactly n internal nodes for every integer tex2html_wrap_inline59063. It also follows from Theorem gif that the height of a complete binary tree containing n internal nodes is tex2html_wrap_inline66266.

Why are we interested in complete trees? As it turns out, complete trees have some useful characteristics. For example, in the preceding chapter we saw that the internal path length of a tree, i.e., the sum of the depths of all the internal nodes, determines the average time for various operations. A complete binary tree has the nice property that it has the smallest possible internal path length:

Theorem  The internal path length of a binary tree with n nodes is at least as big as the internal path length of a complete binary tree with n nodes.

extbfProof Consider a binary tree with n nodes that has the smallest possible internal path length. Clearly, there can only be one node at depth zero--the root. Similarly, at most two nodes can be at depth one; at most four nodes can be at depth two; and so on. Therefore, the internal path length of a tree with n nodes is always at least as large as the sum of the first n terms in the series

displaymath66155

But this summation is precisely the internal path length of a complete binary tree!

Since the depth of the average node in a tree is obtained by dividing the internal path length of the tree by n, Theorem gif tells us that complete trees are the best possible in the sense that the average depth of a node in a complete tree is the smallest possible. But how small is small? I.e., is does the average depth grow logarithmically with n. The following theorem addresses this question:

Theorem  The internal path length   of a complete binary tree with n nodes is

displaymath66156

extbfProof The proof of Theorem gif is left as an exercise for the reader (Exercise gif).

From Theorem gif we may conclude that the internal path length of a complete tree is tex2html_wrap_inline59897. Consequently, the depth of the average node in a complete tree is tex2html_wrap_inline59891.




next up previous contents index

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