Improvement 1: Weighting
- Trace the Quick Union implementation with a union-by-size heuristic.
- Explain the runtime improvements gained by using the heuristic for union-find operations.
Modify quick-union to avoid tall trees:
- Keep track of the size of each component.
- Balance by linking small tree below large one.

Proposition: In this scheme, the depth of any node is at most .
Exercise To justify the proposition, complete the following statements by filling in the blanks.
- The depth of increases at most by
______
when tree containing merges into another tree . - Since the larger tree (among and ) is at least
___________
the smaller tree, the resultant tree (after union) must have at least_________
the number of elements in the smaller one. - The size of tree containing can
_______
at most________
times because if you start with one node and_______________
you will get , and there is only nodes in the tree.
Solution
- The depth of increases at most by one when tree containing merges into another tree .
- (Assume the worst case where each tree has elements and a height of . Then the resulting tree after union will have elements and heigh of .)
- Since the larger tree (among and ) is at least as large as the smaller tree, the resultant tree (after union) must have at least double the number of elements in the smaller one.
- The size of tree containing can double at most times because if you start with one node and double it times you will get , and there is only nodes in the tree.
From the proposition, it follows the height of the tree is in .
The heuristic described above is known as union by size.
Aside: An alternative strategy is union by rank, which always attaches the tree with a smaller "rank" to the root of the tree having a higher rank. The rank of a node is the height of its subtree; the rank of a node can only change if it is a root.