Creating the BTPINS and BTPKEY programs
So far, you have created a fairly typical NAMES file containing names and addresses, and a simple GET.NAMES update program for maintaining that file. Together, they form a rudimentary mailing list application that might be found on any Pick system. The next step is to start using B-TREE-P subroutines to create B-trees, so that the mailing list can be sorted and searched in a convenient manner.
One of the most important B-TREE-P programs is a subroutine named BTPINS, short for B-TREE-P Insert. Give the command EDIT BP BTPINS in order to enter the Editor and type in the following source code for BTPINS:
BTPINS001 SUBROUTINE BTPINS(ROOT,SIZE,BFILE,DFILE,ID,ITEM)002 UID = ID003 CALL BTPKEY(ROOT, UID, ITEM, KEY)004 FK = KEY005 READ NID FROM BFILE, ROOT ELSE006 READ NID FROM BFILE, "NEXT.ID" ELSE NID = 0007 WRITE (NID+1) ON BFILE, "NEXT.ID"008 WRITE NID ON BFILE, ROOT009 WRITE "" ON BFILE, NID010 END011 BU = 0012 NP = ""013 100 READ N FROM BFILE, NID ELSE STOP "BTP1"014 L = 0 ; R = N<1>+1015 IF R = 1 THEN P = 1 ELSE016 LOOP017 P = INT((L+R)/2)018 IN = N<2, P>019 IF IN = UID THEN020 CRT "Already inserted!"021 IF BU THEN STOP "BTP2" ELSE RETURN022 END023 READ IT FROM DFILE, IN ELSE STOP "BTP3"024 CALL BTPKEY(ROOT, IN, IT, KEY)025 IG = (FK > KEY)026 IF IG THEN L = P ELSE R = P027 UNTIL (R-L) <= 1 DO REPEAT028 IF IG THEN P = R029 END030 NNID = N<3, P>031 IF (NNID # "") AND NOT(BU) THEN032 NID = NNID033 GO TO 100034 END035 N = INSERT(N, 2, P, 0, UID)036 IF BU THEN N = INSERT(N, 3, P+1, 0, NP)037 N<1> = N<1>+1038 IF N<1> <= (2*SIZE) THEN WRITE N ON BFILE, NID ELSE039 LOOP040 READ NP FROM BFILE, "NEXT.ID" ELSE NP = 0041 WRITE (NP+1) ON BFILE, "NEXT.ID"042 READ NN FROM BFILE, NP ELSE NN = ""043 UNTIL NN = "" DO REPEAT044 J = 1045 FOR I = (SIZE+2) TO ((2*SIZE)+2)046 IF N<2, I> # "" THEN NN<2, J> = N<2, I>047 CID = N<3, I>048 IF CID # "" THEN049 NN<3, J> = CID050 READ C FROM BFILE, CID ELSE STOP "BTP4"051 C<4> = NP052 WRITE C ON BFILE, CID053 END054 J = J+1055 NEXT I056 NN<1> = SIZE057 NN<4> = N<4>058 WRITE NN ON BFILE, NP059 UID = N<2, SIZE+1>060 NN = SIZE061 FOR I = 1 TO (SIZE+1)062 IF I <= SIZE THEN NN<2, I> = N<2, I>063 IF N<3, I> # "" THEN NN<3, I> = N<3, I>064 NEXT I065 NN<4> = N<4>066 WRITE NN ON BFILE, NID067 IF NN<4> # "" THEN068 BU = 1069 NID = NN<4>070 GO TO 100071 END072 LOOP073 READ RID FROM BFILE, "NEXT.ID" ELSE RID = 0074 WRITE (RID+1) ON BFILE, "NEXT.ID"075 READ NN FROM BFILE, RID ELSE NN = ""076 UNTIL NN = "" DO REPEAT077 NN<1> = 1078 NN<2> = UID079 NN<3, 1> = NID080 NN<3, 2> = NP081 WRITE NN ON BFILE, RID082 WRITE RID ON BFILE, ROOT083 READ NN FROM BFILE, NID ELSE STOP "BTP5"084 NN<4> = RID085 WRITE NN ON BFILE, NID086 READ NN FROM BFILE, NP ELSE STOP "BTP6"087 NN<4> = RID088 WRITE NN ON BFILE, NP089 END090 RETURN091 ENDAt this point, don't worry about trying to understand how BTPINS works. It has been coded in such a way that it will never have to be modified by you, regardless of the application that will be using your B-trees or the way in which you will be doing searching and sorting of your files.
Compile BTPINS. Once BTPINS has been successfully compiled, catalog the program with the command CATALOG BP BTPINS, since most Pick systems require that subroutines be cataloged. Similarly, edit, compile, and catalog the following BTPKEY subroutine:
BTPKEY001 SUBROUTINE BTPKEY(ROOT, ID, ITEM, KEY)002 EQU nul TO CHAR(0)003 BEGIN CASE004 CASE ROOT = "ZIP" ;* By zip/adr/comp/lname/fname/id005 KEY = ITEM<6>:nul:ITEM<4>:nul:ITEM<3>:nul:ITEM<2>:nul:ITEM<1>:nul:(ID"R#10")006 CASE ROOT = "COMP" ;* By company/lname/fname/id007 KEY = ITEM<3>:nul:ITEM<2>:nul:ITEM<1>:nul:(ID "R#10")008 CASE ROOT = "LNAME" ;* By lname/fname/id009 KEY = ITEM<2>:nul:ITEM<1>:nul:(ID "R#10")010 CASE 1 ; STOP "BTP7"011 END CASE012 RETURN013 ENDBTPKEY is called by BTPINS, and controls the way in which your files will sort. By changing the statements inside BTPKEY, you can design B-trees that allow you to sort and search your files in any order you want. For now, just leave BTPKEY exactly as listed above.