0

好的,所以我试图在 Assembly 中递归地计算二叉树的高度,但我正在努力获得正确的数字。我的代码现在探索了这棵树,什么也不做,因为我从中得到的唯一一件事就是计算左右叶子的数量。有人可以帮我解决这个问题吗?我的代码完全错误还是我只需要添加一些行来计算高度?任何帮助表示赞赏。

ps:不要介意评论,他们是意大利语

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

.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
4

1 回答 1

0

首先,“递归”标签在哪里?看不到它。另外,为什么你只为右边的孩子而不是左边的孩子增加身高?

于 2013-06-02T16:01:47.907 回答