汇编实现简单二叉树

来源:百度文库 编辑:神马文学网 时间:2024/10/01 10:48:19
TITLE Binary
INCLUDE Irvine32.inc
.data
array BYTE 50 dup('0')
strAskRoot BYTE 0dh,0ah,"Root:" ,0
strAskLeft BYTE 0dh,0ah,"left:" ,0
strAskRight BYTE 0dh,0ah,"Right:" ,0
strOrder BYTE 0dh,0ah,"Order:" ,0
.code
main proc
;mov ax,@data
;mov ds,ax
mov  edx,offset strAskRoot
call WriteString
mov  ebx,1
call CreateBT ;递归建树
mov  edx,offset strOrder
call WriteString
mov  ebx,1
call Order ;前序遍历树
exit
main endp
CreateBT proc uses eax ebx
mov eax,0
call ReadChar
cmp al,0dh
je Exit
mov array[ebx],al
call WriteChar
call LeftChild
call RightChild
Exit:
ret
CreateBT endp
LeftChild proc uses eax edx ebx
mov edx,offset strAskLeft
call WriteString
push ebx
shl ebx,1
call CreateBT
pop ebx
ret
LeftChild endp
RightChild proc uses eax edx ebx
mov edx,offset strAskRight
call WriteString
push ebx
shl ebx,1
inc ebx
call CreateBT
pop ebx
ret
RightChild endp
Order proc uses eax ebx edx ;该过程为前序遍历,中序遍历、后序遍历与前序遍历大致相同
cmp array[ebx],'0'
je return1
mov al,array[ebx]
call WriteChar
call PrintLeft
call PrintRight
return1:
ret
Order endp
PrintLeft proc uses ebx
push ebx
shl ebx,1
call Order
pop ebx
ret
PrintLeft endp
PrintRight proc uses ebx
push ebx
shl ebx,1
inc ebx
call Order
pop ebx
ret
PrintRight endp
end main