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.
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
andq
.Union: takes constant time, given roots.
Depth of any node
x
is at mostlog(N)
.
Last updated