code: private void bfs(int[] membership, int cluster, int id) {
          int n = merge.length + 1;
          Queue<Integer> queue = new LinkedList<>();
          queue.offer(cluster);
  
          for (Integer i = queue.poll(); i != null; i = queue.poll()) {
              if (i < n) {
                  membership[i] = id;
                  continue;
              }
  
              i -= n;
  
              int m1 = merge[i][0];
  
              if (m1 >= n) {
                  queue.offer(m1);
              } else {
                  membership[m1] = id;
              }
  
              int m2 = merge[i][1];
  
              if (m2 >= n) {
                  queue.offer(m2);
              } else {
                  membership[m2] = id;
              }
          }
      }
docs: BFS the merge tree and identify cluster membership.
  @param membership the cluster membership array.
  @param cluster the last merge point of cluster.
  @param id the cluster ID.