📄 lxshu.asm
字号:
dseg segment
mess db ' tree is empty!',0Dh,0Ah,"$"
ecs db 64 DUP(?)
jsq dw 1 DUP(0)
zh dw 1 DUP(0)
dseg ends
cseg segment
assume cs:cseg,ds:dseg
start: mov ax,dseg
mov ds,ax
mov di,offset ecs
jsl: mov ah,01h ;建树
int 21h
cmp al,3dh
je bls
mov zh,di
mov byte ptr [di], al
inc zh
mov di,zh
jmp jsl
bls: mov dl,0dh;开始遍历
mov ah,2
int 21h
mov dl,0ah
mov ah,2
int 21h
mov di,offset ecs;得到树的根结点
mov si,di
dec si
cmp byte ptr [di],00h;树为空
je ks
cmp byte ptr [di],20h;结点为空格
je ks
pre: cmp byte ptr [di],00h;前序
je pre2
cmp byte ptr [di],20h
je pre2
jne pre1
ks: mov dx,offset mess;空树提示信息
mov ah,9
int 21h
jmp over
pre1: mov zh, di
mov dl,byte ptr [di]
mov ah,02h
int 21h
mov cl,byte ptr [zh]
push cx
inc jsq
sub zh,si
shl zh,1
add zh,si
mov di,zh
jmp pre
pre2: cmp jsq,0
je preover;前序遍历完,开始中序遍历
pop di
dec jsq
sub di,si
shl di,1
add di,si
inc di
jmp pre
preover: mov dl,0dh;换行
mov ah,2
int 21h
mov dl,0ah
mov ah,2
int 21h
mov di,offset ecs
mov si,di
dec si
mov zh,di
mid : cmp byte ptr [di],00h;中序
je mid2
cmp byte ptr [di],20h
je mid2
mid1 : mov cx,di
push cx
inc jsq
sub di,si
shl di,1
add di,si
jmp mid
ks1: jmp ks
mid2: cmp jsq,0
je midover
pop di
dec jsq
mov dl,byte ptr [di]
mov ah,02h
int 21h
sub di,si
shl di,1
add di,si
inc di
jmp mid
midover: mov dl,0dh;换行
mov ah,2
int 21h
mov dl,0ah
mov ah,2
int 21h
mov di,offset ecs
mov si,di
dec si
mov zh,di
aff : cmp byte ptr [di],00h;后序
je aff2
cmp byte ptr [di],20h
je aff2
aff1: mov cx,di
push cx
inc jsq
sub di,si
shl di,1
add di,si
jmp aff
aff2 : cmp jsq,0
je over
pop di
push di
sub di,si
shl di,1
add di,si
inc di
cmp byte ptr [di],20h
je ok
cmp byte ptr [di],00h
jne aff
ok: pop di
dec jsq
mov dl,byte ptr [di]
mov ah,2
int 21h
mov byte ptr [di],00h
cmp jsq,0
je over
pop di
dec jsq
jmp aff
over : mov ah,4ch
int 21h
cseg ends
end start
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -