10 REM------------------------------------------------------
20 REM
30 REM insert.bas
40 REM Add to the contents of a data file thru its index
50 REM
60 REM-------------------------------------------------------
70 CLS:KEY OFF
80 S0%=20:DIM STACK%(S0%):GOTO 2220
90 REM
100 REM Subroutines used:
110 REM 100,150:read, write a node of b-tree
120 REM 200,250:save, restore copy of b-tree node
130 REM 300,350,395:push, pop, init the stack
140 REM 400 :shift items in node for splitting node
150 REM 500: search down b-tree
160 REM 600: allocate more space for b-tree
170 REM 700: split b-tree node to root node
180 REM 800: overflow b-tree node to root node
190 REM 900: insert a new item into b-tree
200 REM 1000: close all files and finish up
210 REM
220 REM
230 REM
240 REM
250 REM-----------------------------------------------------------
260 REM
270 REM READ.BAS
280 REM INPUT A B-TREE NODE FROM DISK FILE #1
290 REM------------------------------------------------------------
300 GET 1, P%:LSET REC$=R$
310 FOR INDEX%=1 TO N%
320 CH%=SIZE%*(INDEX%-1)
330 FLAG$=MID$(REC$,CH%+1,1)
340 IF FLAG$="E" THEN FLAG%(INDEX%)=0
350 IF FLAG$="F" THEN FLAG%(INDEX%)=1
360 IF FLAG$="D" THEN FLAG%(INDEX%)=2
370 KEYS$(INDEX%)=MID$(REC$,CH%+2,SIZE%-3)
380 ARC%(INDEX%)=CVI(MID$(REC$,CH%+SIZE%-1,2))
390 NEXT INDEX%
400 ARC%(N%+1)=CVI(MID$(REC$,126,2))
410 RETURN
420 REM------------------------------------------------------------
430 REM WRITE.BAS
440 REM Output a b-tree node to file #1
450 REM-------------------------------------------------------------
460 REC$=STRING$(127, " " )
470 FOR INDEX%=1 TO N%
480 CH%=SIZE%*(INDEX%-1)
490 FLAG$=MID$(REC$,CH%+1,1)
500 FLAG$="E" :GOTO 530
510 FLAG$="F":GOTO 530
520 FLAG$="D"
530 MID$(REC$,CH%+1,1)=FLAG$
540 MID$(REC$,CH%+2,SIZE%-3)=KEYS$(INDEX%)
550 MID$(REC$,CH%+SIZE%-1,2)=MKI$(ARC%(INDEX%))
560 NEXT INDEX%
570 MID$(REC$,126,2)=MKI$(ARC%(N%+1))
580 LSET R$=REC$:PUT 1, P%
590 RETURN
600 REM-------------------------------------
610 REM SAVE A B-TREE NODE
620 REM-------------------------------------
630 FOR INDEX%=1 TO N%+1
640 SFLAG%(INDEX%)=FLAG%(INDEX%)
650 SKEYS$(INDEX%)=KEYS$(INDEX%)
660 SARC%(INDEX%)=ARC%(INDEX%)
670 NEXT INDEX%
680 RETURN
690 REM------------------------------------------
700 REM RESTORE A B-TREE NODE
710 REM------------------------------------------
720 FOR INDEX%=1 TO N%+1
730 FLAG%(INDEX%)=SFLAG%(INDEX%)
740 KEYS$(INDEX%)=SKEYS$(INDEX%)
750 ARC%(INDEX%)=SARC%(INDEX%)
760 NEXT INDEX%
770 RETURN
780 REM------------------------
790 REM PUSH
800 REM------------------------
810 IF TS%<=S0% THEN 840
820 D$="Stack overflow"
830 RETURN
840 STACK%(TS%)=A%:TS%=TS%+1
850 D$= "" :RETURN
860 REM---------------------
870 REM pop
880 REM----------------------
890 TS%=TS%-1
900 IF TS%>0 THEN 930
910 D$="Stack underflow"
920 RETURN
930 A%=STACK%(TS%)
940 D$= "" :RETURN
950 REM--------------------------
960 REM Initialize stack
970 REM--------------------------
980 TS%=1:RETURN
990 REM--------------------------------
1000 REM Shift b-tree node
1010 REM--------------------------------
1020 SPLIT%=INT((N%+1)/2)
1030 I%=1
1040 IF SPLIT%+I%<=N% THEN 1050 ELSE 1100
1050 ARC%(I%)=ARC%(SPLIT%+I%)
1060 KEYS$(I%)=KEYS$(SPLIT%+I%)
1070 FLAG%(I%)=FLAG%(SPLIT%+I%)
1080 I%=I%+1
1090 GOTO 1040
1100 ARC%(I%)=TEMP%
1110 KEYS$(I%)=ZERO$
1120 FLAG%(I%)=0
1130 REM---------------------------------------
1140 REM Zero out remaining items in node
1150 REM---------------------------------------
1160 FOR I%=I%+1 TO N%
1170 ARC%(I%)=0
1180 KEYS$(I%)=ZERO$
1190 FLAG%(I%)=0
1200 NEXT I%
1210 GOSUB 1420 'allocate disk space at p2%
1220 SWAP P%,P2%
1230 GOSUB 420 'write right son to disk
1240 SWAP P%,P2%
1250 RETURN
1260 REM------------------------------------
1270 REM search b-tree for k$
1280 REM-------------------------------------
1290 D$= "" 'message
1300 GOSUB 950 'initialize stack
1310 P%=ROOT%
1320 REM repeat until found or not-in-file
1330 I%=1
1340 GOSUB 250
1350 IF KEYS$(I%)=ZERO$ THEN 1380
1360 IF KEYS$(I%)"Done" THEN 1960 'ascend b-tree?
2100 RETURN
2110 REM----------------------------
2120 REM Finish up
2130 REM-----------------------------
2140 FOR I%=1 TO 24
2150 PRINT
2160 NEXT I% 'clear screen
2170 CLOSE 1,2
2180 OPEN "o",2,"HEADER.DAT"
2190 PRINT #2,FSCREEN$;",";ROOT%;LNG%;LNF%;AN%;LINS%;N%;SIZE%;INDEX$;",";MAST$
2200 CLOSE 2
2210 RETURN
2220 REM--------------------------------------------
2230 REM Capture data from screen form
2240 REM--------------------------------------------
2241 PRINT :PRINT:PRINT "As each line of your screen form appears, type in the requested"
2242 PRINT " information.":PRINT:PRINT
2250 FOR I%=1 TO 3:PRINT:NEXT I%
2260 OPEN "I",2,"HEADER.DAT"
2270 INPUT #2,FSCREEN$, ROOT%,LNG%,LNF%,AN%,LINS%,N%,SIZE%,INDEX$, MAST$
2280 CLOSE 2
2290 N0%=N%+1:DIM FLAG%(N0%),KEYS$(N0%),ARC%(N0%)
2300 DIM SFLAG%(N0%),SKEYS$ (N0%),SARC%(N0%)
2310 OPEN "I",2,FSCREEN$
2320 FOR L%=1 TO LINS%:INPUT #2,RW$(L%):NEXT L%
2330 CLOSE 2
2340 OPEN "R",1,INDEX$
2350 FIELD 1,127 AS R$
2360 REC$=SPACE$(128):ZERO$=SPACE$(SIZE%-3):LSET ZERO$="0"
2370 K$=SPACE$(SIZE%-3)
2380 OPEN "R",2,MAST$
2390 FIELD 2, 127 AS MR$
2400 REM --------------------FORMS INPUT----------------------
2410 DIM AN$(AN%) 'ANSWERS IN AN$
2420 K%=0
2430 FOR L%=1 TO LINS%
2440 SRW$=RW$(L%) 'SAVE FORM PROMPT
2450 PRINT USING "##";L%;:PRINT ".";
2460 IF INSTR(LEFT$(RW$(L%),1),"-")=1 THEN 2480
2470 IF INSTR(LEFT$(RW$(L%),1)," ")=0 THEN 2500
2480 RW$(L%)=RIGHT$(RW$(L%),LEN(RW$(L%))-1)
2490 GOTO 2460
2500 STAR%=INSTR(RW$(L%), "*")
2510 J%=INSTR(RW$(L%), ":")
2520 IF STAR%=0 THEN 2540
2530 IF STAR%