INTEGER FUNCTION ITREE(X,DX, INX,NX, VECT,NDIM,NVEC, INDLR) * binary search tree * * A search is made for key vectors, which satisfy the conditions. * The arguments INX,NX, VECT,NDIM,NVEC, INDLR are as before in DFTREE. * The user has to specify the limits in the arrays X(NDIM),DX(NDIM). * * The function returns the index for each found key vector. * The value zero is returned, if no further key vector is found. * IMPLICIT NONE INTEGER NX,NDIM,NVEC,NSTACK,I,IST,ISTMAX,K,L REAL X(*),DX(*),VECT(NDIM,NVEC) INTEGER INX(NX),INDLR(2,NVEC) ! index vector, tree pointer PARAMETER (NSTACK=100) INTEGER KST(NSTACK),LST(NSTACK) ! stack LOGICAL TXL,TXR DATA I/0/ SAVE * ... IF(I.NE.0) GOTO 20 IST=1 KST(IST)=1 ! push LST(IST)=1 ! 1. component ISTMAX=0 10 I=KST(IST) ! pop element L=LST(IST) ! index IST=IST-1 IF(I.EQ.0) STOP 'I is zero ' TXL=VECT(INX(L),I).GE.X(L)-DX(L) TXR=VECT(INX(L),I).LE.X(L)+DX(L) IF(TXL.AND.TXR) THEN DO K=1,NX ! check rectangle IF(ABS(VECT(INX(K),I)-X(K)).GT.DX(K)) GOTO 20 END DO GOTO 30 END IF 20 IF(INDLR(2,I).NE.0.AND.TXR) THEN IST=IST+1 IF(IST.GT.NSTACK) STOP 'ITREE stack overflow' KST(IST)=INDLR(2,I) ! push r LST(IST)=MOD(L,NX)+1 ! next component END IF IF(INDLR(1,I).NE.0.AND.TXL) THEN IST=IST+1 IF(IST.GT.NSTACK) STOP 'ITREE stack overflow' KST(IST)=INDLR(1,I) ! push l LST(IST)=MOD(L,NX)+1 ! next component END IF IF(IST.NE.0) GOTO 10 I=0 30 ITREE=I ! no further elements END