public void splitNode(BallNode node, int numNodesCreated) throws Exception {
    correctlyInitialized();
    
    double maxDist = Double.NEGATIVE_INFINITY, dist = 0.0;
    Instance furthest1=null, furthest2=null, pivot=node.getPivot(), temp;
    double distList[] = new double[node.m_NumInstances];
    for(int i=node.m_Start; i<=node.m_End; i++) {
      temp = m_Instances.instance(m_Instlist[i]);
      dist = m_DistanceFunction.distance(pivot, temp, Double.POSITIVE_INFINITY);
      if(dist > maxDist) {
        maxDist = dist; furthest1 = temp;
      }
    }
    maxDist = Double.NEGATIVE_INFINITY;
    furthest1 = (Instance)furthest1.copy();
    for(int i=0; i < node.m_NumInstances; i++) {
      temp = m_Instances.instance(m_Instlist[i+node.m_Start]);
      distList[i] = m_DistanceFunction.distance(furthest1, temp, 
                                                Double.POSITIVE_INFINITY);
      if(distList[i] > maxDist) {
        maxDist = distList[i]; furthest2 = temp; //tempidx = i+node.m_Start;
      }
    }
    furthest2 = (Instance) furthest2.copy();
    dist = 0.0; int numRight=0;
    //moving indices in the right branch to the right end of the array
    for(int i=0, j=0; i < node.m_NumInstances-numRight; i++, j++) {
      temp = m_Instances.instance(m_Instlist[i+node.m_Start]);
      dist = m_DistanceFunction.distance(furthest2, temp, Double.POSITIVE_INFINITY);
      if(dist < distList[i]) {
        int t = m_Instlist[node.m_End-numRight];
        m_Instlist[node.m_End-numRight] = m_Instlist[i+node.m_Start];
        m_Instlist[i+node.m_Start] = t;
        double d = distList[distList.length-1-numRight];
        distList[distList.length-1-numRight] = distList[i];
        distList[i] = d;
        numRight++;
        i--;
      }
    }
    
    if(!(numRight > 0 && numRight < node.m_NumInstances)) 
      throw new Exception("Illegal value for numRight: "+numRight);
    
    node.m_Left = new BallNode(node.m_Start, node.m_End-numRight, numNodesCreated+1,
                              (pivot=BallNode.calcCentroidPivot(node.m_Start,
                                                node.m_End-numRight, m_Instlist, 
                                                m_Instances)), 
                              BallNode.calcRadius(node.m_Start, 
                                                node.m_End-numRight, m_Instlist, 
                                                m_Instances, pivot, 
                                                m_DistanceFunction)
                              );
    
    node.m_Right = new BallNode(node.m_End-numRight+1, node.m_End, numNodesCreated+2,
                       (pivot=BallNode.calcCentroidPivot(node.m_End-numRight+1,
                                                         node.m_End, m_Instlist, 
                                                         m_Instances)), 
                          BallNode.calcRadius(node.m_End-numRight+1, node.m_End, 
                                              m_Instlist, m_Instances, pivot, 
                                              m_DistanceFunction)
                              );
  }