private void divide(DBIDArrayIter it,double[] data,ArrayList<DBIDs> idx,int start,int end,int depth,Mean mean){
  final int count=end - start;
  if (depth == 0) {
    if (count > 0) {
      ModifiableDBIDs out=DBIDUtil.newHashSet(count);
      it.seek(start);
      for (int i=count; i > 0; i--, it.advance()) {
        out.add(it);
      }
      idx.add(out);
    }
 else {
      idx.add(DBIDUtil.EMPTYDBIDS);
    }
    return;
  }
 else {
    if (count > 0) {
      mean.reset();
      for (int i=start; i < end; i++) {
        mean.put(data[i]);
      }
      final double m=mean.getMean();
      int pos=Arrays.binarySearch(data,start,end,m);
      if (pos >= 0) {
        int opt=(start + end) >> 1;
        while (Double.compare(data[pos],m) == 0) {
          if (pos < opt) {
            pos++;
          }
 else           if (pos > opt) {
            pos--;
          }
 else {
            break;
          }
        }
      }
 else {
        pos=(-pos - 1);
      }
      divide(it,data,idx,start,pos,depth - 1,mean);
      divide(it,data,idx,pos,end,depth - 1,mean);
    }
 else {
      divide(it,data,idx,start,end,depth - 1,mean);
      divide(it,data,idx,start,end,depth - 1,mean);
    }
  }
}

private void divide ( DBIDArrayIter it , double [ ] data , ArrayList < DBIDs > idx , int start , int end , int depth , Mean mean ) { final int count = end - start ; if ( depth == 0 ) { if ( count > 0 ) { ModifiableDBIDs out = DBIDUtil . newHashSet ( count ) ; it . seek ( start ) ; for ( int i = count ; i > 0 ; i -- , it . advance ( ) ) { out . add ( it ) ; } idx . add ( out ) ; } else { idx . add ( DBIDUtil . EMPTYDBIDS ) ; } return ; } else { if ( count > 0 ) { mean . reset ( ) ; for ( int i = start ; i < end ; i ++ ) { mean . put ( data [ i ] ) ; } final double m = mean . getMean ( ) ; int pos = Arrays . binarySearch ( data , start , end , m ) ; if ( pos >= 0 ) { int opt = ( start + end ) > > 1 ; while ( Double . compare ( data [ pos ] , m ) == 0 ) { if ( pos < opt ) { pos ++ ; } else if ( pos > opt ) { pos -- ; } else { break ; } } } else { pos = ( - pos - 1 ) ; } divide ( it , data , idx , start , pos , depth - 1 , mean ) ; divide ( it , data , idx , pos , end , depth - 1 , mean ) ; } else { divide ( it , data , idx , start , end , depth - 1 , mean ) ; divide ( it , data , idx , start , end , depth - 1 , mean ) ; } } }
recursive call to further subdivide the array .
