disjoint set data structure

后端 未结 1 1172
逝去的感伤
逝去的感伤 2021-01-25 04:45

this is the code for findind disjoint sets

    class disjoint_sets {
      struct disjoint_set {
      size_t parent;
      unsigned rank;
      disjoint_set(siz         


        
相关标签:
1条回答
  • 2021-01-25 05:20

    Because in this case - you add one tree is a "sub tree" of the other - which makes the original subtree increase its size.

    Have a look at the following example:

    1           3
    |           |
    2           4
    

    In the above, the "rank" of each tree is 2.
    Now, let's say 1 is going to be the new unified root, you will get the following tree:

        1
       / \
      /   \
     3     2
     |
     4
    

    after the join the rank of "1" is 3, rank_old(1) + 1 - as expected.

    0 讨论(0)
提交回复
热议问题