;
; Programm testet die Funktion des "Binary Tree" Verfahrens
;
; Es werden bis zu 10 beliebige Zeilen eingegeben.
; Falls eine Zeile schon im Tree, so wird das angezeigt.
; Beendigung durch Eingabe einer leeren Zeile
;
; Am Schluss Ausgabe des Tree Inhaltes
;
; Es werden die Module LOOKUP, PUTREE und SEARCH benoetigt.
;
; Linken mit z.B. LINK TEST=LOOKUP,PUTREE,SEARCH,BINTREE
;
; Werner Cirsovius
;

	entry	$memry,putnam,first

	extrn	putree,lookup

OS	equ	0000h
BDOS	equ	0005h

conout	equ	 2
string	equ	 9
kblin	equ	10

linlen	equ	80

cr	equ	0dh
lf	equ	0ah
eot	equ	'$'

BINTREE:
	ld	sp,LocStk	; Lokalen Stack
	ld	hl,($memry)	; Erste freie Adresse
	ld	(first),hl	; .. fuer Zeiger FIRST
	xor	a
	ld	(hl),a		; Zeiger erden
	inc	hl
	ld	(hl),a
	ld	de,messg
	call	prstr		; Funktionsmeldung ausgeben
	ld	b,10		; Anzahl laden
loop:
	push	bc
	call	fortree		; Zeile einlesen
	jr	nc,break	; .. Abbruch
	call	totree		; Zeile einfuegen
	pop	bc
	djnz	loop
display:
	ld	hl,(root)	; ROOT laden
	ld	a,h
	ld	h,l
	ld	l,a
	or	h
	push	af		; Test ob Daten im Tree
	call	nz,telltree	; .. ausgeben
	pop	af
	call	z,emptree	; Leeren Tree melden
	jp	OS
break:
	pop	bc
	jr	display

; **************************************
;
; Nicht leeren Tree ausgeben
;
telltree:
	ld	de,content
	call	prstr		; Kurze Anzeige
	call	putree		; .. dann Tree
	ret
;
; Leeren Tree melden
;
emptree:
	ld	de,empty
	call	prstr		; .. simple Meldung
	ret
;
; Zeile fuer Tree einlesen
;
fortree:
	ld	a,':'
	call	cout		; Anzeige der Eingabe
	ld	de,linbuf
	ld	c,kblin
	call	BDOS		; Zeile lesen
	ld	de,linbuf+2
	ld	a,(linbuf+1)
	or	a		; Test ob Daten in Zeile
	ret	z
	ld	l,a
	ld	h,0
	add	hl,de		; Zeiger an das Ende
	ld	(hl),0		; .. Zeile anschliessen
	ld	de,crlf
	call	prstr		; .. Konsole schliessen
	scf
	ret
;
; Zeile in den Tree einfuegen
;
totree:
	ld	bc,linbuf+2	; Werte laden
	ld	hl,root
	ld	d,(hl)
	inc	hl
	ld	e,(hl)
	dec	hl
	ex	de,hl
	call	lookup		; Zeile einfuegen
	ret	c		; .. ok
	ld	de,symkno
	call	prstr		; Meldung dass schon im Tree
	ret
;
; Tree Element ^HL auf Konsole ausgeben - Register HL retten
;
putnam:
	push	hl
	push	de
	call	put0		; .. Ausgabe
	ld	de,crlf
	call	prstr		; .. Zeile schliessen
	pop	de
	pop	hl
	ret
;
; Tree Element ^HL auf Konsole ausgeben
;
put0:
	ld	a,(hl)		; Zeichen holen
	or	a
	ret	z		; Ende bei Null
	call	cout
	inc	hl
	jr	put0
;
; Zeichen im Akku auf Bildschirm ausgeben
;
cout:
	push	bc
	push	de
	push	hl
	ld	e,a		; Zeichen umpacken
	ld	c,conout
	call	BDOS		; .. ausgeben
	pop	hl
	pop	de
	pop	bc
	ret
;
; Zeichenkette in ^DE auf Bildschirm ausgeben
;
prstr:
	push	bc
	push	de
	push	hl
	ld	c,string
	call	BDOS		; .. ausgeben
	pop	hl
	pop	de
	pop	bc
	ret
;
symkno:
	db	'Eintrag schon im Binary Tree enthalten'
crlf:
	db	cr,lf,eot
messg:
	db	'Binary Tree Test',cr,lf
	db	'10 Eingaben, danach sortierte Ausgabe'
	db	cr,lf,lf,eot
empty:
	db	'Binary Tree ist leer'
	db	cr,lf,eot
content:
	db	cr,lf,'Inhalt des Binray Tree'
	db	cr,lf,lf,eot
;
	ds	2*32
LocStk	equ	$
;
first:
	ds	2
root:
	dw	0
$memry:
	ds	2
linbuf:
	db	linlen,0
	ds	linlen+3

	end	BINTREE

