question: NetNode agg3way ( NetNode x , NetNode y , NetNode z , Stack < NetNode > amalgs , double [ ] [ ] D , NetNode netNodes , int num_nodes ) { NetNode u = new NetNode ( ) ; u . id = num_nodes + 1 ; u . ch1 = x ; u . ch2 = y ; NetNode v = new NetNode ( ) ; v . id = num_nodes + 2 ; v . ch1 = y ; v . ch2 = z ; u . next = x . next ; u . prev = x . prev ; if ( u . next != null ) u . next . prev = u ; if ( u . prev != null ) u . prev . next = u ; v . next = z . next ; v . prev = z . prev ; if ( v . next != null ) v . next . prev = v ; if ( v . prev != null ) v . prev . next = v ; if ( y . next != null ) y . next . prev = y . prev ; if ( y . prev != null ) y . prev . next = y . next ; u . nbr = v ; v . nbr = u ; for ( NetNode p = netNodes . next ; p != null ; p = p . next ) { D [ u . id ] [ p . id ] = D [ p . id ] [ u . id ] = ( 2.0 / 3.0 ) * D [ x . id ] [ p . id ] + D [ y . id ] [ p . id ] / 3.0 ; D [ v . id ] [ p . id ] = D [ p . id ] [ v . id ] = ( 2.0 / 3.0 ) * D [ z . id ] [ p . id ] + D [ y . id ] [ p . id ] / 3.0 ; } D [ u . id ] [ u . id ] = D [ v . id ] [ v . id ] = 0.0 ; amalgs . push ( u ) ; return u ; }
target:  agglomerate 3 nodes. Note that this version doesn't rescan num_nodes, you need to num_nodes+=2 after calling this!