[   ^ to index...   |   <-- previous   |   next -->   ]

Performance of binary tree operations

To analyze the performance of BinaryTree operations, we must define a few terms:

height
The height of a tree is the greatest path distance from the root to any leaf.
complete tree
A binary tree is complete if, for every node, both of its subtrees are exactly the same height.
balanced tree
A binary tree is balanced if, for every node, both of its subtrees are "almost" the same height (i.e., within some constant factor).

Observing that the number of nodes in a complete binary tree must double as height increases by one. Therefore, the relationship between height h and node count n is given by the equation

n = 2h

Solving for h, we obtain h = log2n

Using a similar argument, we can demonstrate that the height of a balanced binary tree is proportional to the logarithm of the number of nodes. Now, assuming we insert data in random order, we shall expect the tree to be roughly balanced.

Given that that the height of a balanced tree is O(logn), what is the expected time cost of the following operations?

A tree in which all left children are null, or all right children are null, is "degenerate". How does performance change when we have a degenerate tree? (Hint: what is the height?)

Thought questions

Q: Clearly, inserting N items into a binary search tree must be equivalent to sorting the items. What are the average and worst-case running times of this sorting algorithm?

Q: What is the probability that a tree with randomly inserted items will be degenerate? Does the derivation look familiar? If not, consider what insertion order results in a degenerate tree.

Q: You might observe that a tree is really a kind of container, like a linked list. Can you think of two approaches to writing an in-order iterator for a binary tree? To refresh your memory, here's how you'd use an iterator:

BinaryTree tree; // ... some insertions, then: Iterator *i = tree.getIterator(); while (i->hasNext()) { int current_key = i->next(); // do something with current_key }

Last modified: Tue Aug 15 10:15:41 PDT 2000