Union by size keeps a size per root and always hangs the smaller tree under the larger one. A node’s depth then grows only when its tree at least doubles, so depth stays at most log2 n. Union by rank is the same idea using an estimate of height.