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

时间:2013-06-01 15:36:20

标签: assembly recursion height binary-tree mips

好的,所以我试图在Assembly中递归计算二叉树的高度,但我很难得到正确的数字。我的代码现在正在探索树而不再做任何事情,因为我唯一得到的就是计算左右叶的数量。有人可以帮我吗?我的代码完全错了还是我只需要添加一些线来计算高度?任何帮助表示赞赏。

p.s。:不介意评论,他们是意大利语

编辑:一种带有英文评论的改进代码

.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

1 个答案:

答案 0 :(得分:0)

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