Weighted Quick Union
Last updated
Last updated
Keep track of size of each tree (number of objects).
Balance by linking root of smaller tree to root of larger tree.
Data structure Same as quick-union, but maintain extra array sz[i]
to count number of objects in the tree rooted at i
.
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)
.