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

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).

The height of a complete binary tree is O(logn), where n is the number of nodes. We prove this by observing that the number of nodes 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: Is the output of postorder_print simply preorder_print backwards? (I.e., the same items in the reverse order?) How would we define a procedure reverse_preorder that prints the reverse of a preorder traversal?


Last modified: Mon Aug 14 21:19:08 PDT 2000