public void insert ( INDArray point ) { if ( ! point . isVector ( ) || point . length ( ) != dims ) throw new IllegalArgumentException ( "Point must be a vector of length " + dims ) ; if ( root == null ) { root = new KDNode ( point ) ; rect = new HyperRect ( HyperRect . point ( point ) ) ; } else { int disc = 0 ; KDNode node = root ; KDNode insert = new KDNode ( point ) ; int successor ; while ( true ) { //exactly equal INDArray pt = node . getPoint ( ) ; INDArray countEq = Nd4j . getExecutioner ( ) . execAndReturn ( new Any ( pt . neq ( point ) ) ) . z ( ) ; if ( countEq . getInt ( 0 ) == 0 ) { return ; } else { successor = successor ( node , point , disc ) ; KDNode child ; if ( successor < 1 ) child = node . getLeft ( ) ; else child = node . getRight ( ) ; if ( child == null ) break ; disc = ( disc + 1 ) % dims ; node = child ; } } if ( successor < 1 ) node . setLeft ( insert ) ; else node . setRight ( insert ) ; rect . enlargeTo ( point ) ; insert . setParent ( node ) ; } size ++ ; }
Insert a point in to the tree