Weighted Quick Union

Weighted Quick Union

  • Keep track of size of each tree (number of objects).

  • Balance by linking root of smaller tree to root of larger tree.

Comparison between Quick Union and Weighted Quick Union

Data structure Same as quick-union, but maintain extra array sz[i] to count number of objects in the tree rooted at i.

Java Implementation

Analysis

  • Find: takes time proportional to depth of p and q.

  • Union: takes constant time, given roots.

  • Depth of any node x is at most log(N).

Last updated

Was this helpful?