在汇编(MIPS)中查找二叉树高度



好的,所以我试图在汇编中递归地计算二叉树的高度,但我正在努力获得正确的数字。我的代码现在只探索树,什么也没做,因为我从中得到的唯一的东西就是计算左叶节点和右叶节点的数量。有人能帮我一下吗?我的代码是完全错误的还是我只需要添加一些行来计算高度?如有任何帮助,不胜感激。

注。:不要介意这些注释,它们是意大利语

编辑:带有英文注释的改进代码

.data
radice:.word 17, node1, node2
node1:.word 5, node3, 0
node2:.word 8, node4, node5
node3:.word 1, node6, node7
node4:.word 2, 0, 0
node5:.word 3, 0, node8
node6:.word 5, 0, 0
node7:.word 12, 0, node9
node8:.word 6, 0, 0
node9:.word 2, 0, 0
.text
main:
la $s0, radice #first node
jal recursive
recursive:
addi $sp, $sp, -8 #making space in the stack
sw $ra, 0($sp) #save ra
beqz $s0, null #Does this node exist?
sw $s0, 4($sp) #If it does, save it in the stack
lw $s1, 0($s0) ####Display wich node is currently being examined#####
li $t9, 0  #Control
li $a3, 0  #Left height
li $a2, 0  #Right height
j left  #Examine left child of this node

null:
beq $t9, 1, nullL  #If this control is set to 1, this was a left child
li $a2, 0  #Right height is 0
lw $ra, 0($sp)  #load ra from stack
addi $sp, $sp, 8  #clear stack
jr $ra #jump to ra
nullL:
li $a3, 0  #Left height is 0
la $s0, 4($sp) #load father address from stack
lw $s2, 0($s0) ####Display who is the father####
lw $ra, 0($sp) #load ra
addi $sp, $sp, 8 #clear stack
jr $ra #jump to ra

left:
li $t9, 1 #set control to 1
lw $s0, 4($s0) #load left child of this node
jal recursive #start recursion
right:
lw $s0, 8($s0) #load right child of this node
jal recursive # start recursion
j right #Don't know if this will be useful

首先,"递归"标签在哪里?看不见。另外,为什么只增加右边子结点的高度而不增加左边子结点的高度呢?

最新更新