I was studying disjoint set data structure. I understand how rank helps us make shallow trees and path compression decrease tree height.
I can find the below code in most articles or study materials for path compression.
int find(subset[] subsets, int I) {
if (subsets[i].parent != I) {
subsets[i].parent= find(subsets, subsets[i].parent);
return subsets[i].parent;
}
}
I am wondering what about ranks. When we will do path compression the rank for the root also change but we did not update that. Could you please explain to me if I am missing something?
I have checked with the online tool so it looks like if we don't update the rank then it would not work as expected. I think it's more about the approximation of path compression. I am thinking in the worst-case scenario it can possible to create a dense tree.