      SUBROUTINE HPDEL(NMAX,NWDS,DATA,N,T,HPFUN,K)
C
C   PURPOSE
C          DELETE K-TH ELEMENT OF HEAP.  RESULTING TREE IS REHEAPED.
C   INPUT
C         NMAX = MAXIMUN NUMBER OF NODES ALLOWED BY USER.
C         NWDS = NUMBER OF WORDS PER NODE.
C         DATA = WORK AREA IN WHICH THE NODES ARE STORED.
C        N = CURRENT NUMBER OF NODES.
C        T = INTEGER ARRAY OF POINTERS TO NODES.
C         HPFUN = NAME OF USER WRITTEN FUNCTION TO DETERMINE TOP NODE.
C         K = INDEX OF NODE TO BE DELETED
C   OUTPUT
C         N = UPDATED NUMBER OF NODES.
C         T = UPDATED INTEGER POINTER ARRAY TO NODES.
C
      EXTERNAL HPFUN
      LOGICAL HPFUN
      DOUBLE PRECISION DATA(1)
      INTEGER T(1)
      IF(N .EQ. 0) RETURN
      IF(K.EQ.N) THEN
        N=N-1
        RETURN
      END IF
      KDEL=K
      JUNK=T(KDEL)
      T(KDEL)=T(N)
      T(N)=JUNK
      N=N-1
  10  IF(KDEL.EQ.1) THEN
        CALL HPGRO(NMAX,NWDS,DATA,N,T,HPFUN,KDEL)
        RETURN
      ELSE
        KHALVE=KDEL/2
        IL=T(KHALVE)
        IR=T(KDEL)
        IF(HPFUN(DATA(IL),DATA(IR),NWDS)) THEN
          CALL HPGRO(NMAX,NWDS,DATA,N,T,HPFUN,KDEL)
          RETURN
        ELSE
          T(KHALVE)=IR
          T(KDEL)=IL
          KDEL=KHALVE
        END IF
      END IF
      GO TO 10
      END
