【问题标题】:Binary Search Tree MIPS confusion二叉搜索树 MIPS 混淆
【发布时间】:2019-11-26 21:30:11
【问题描述】:

我有一个项目需要在 MIPS 中创建一个二叉搜索树。我在 C 中很好理解,但 MIPS 是我迷路的地方。我的教授已包含此代码以使用插入方法。我对 addNodeToTree 和 addNode 之间的区别感到困惑:

    # add a node to tree
    # 
    # input: $a0 the address of the tree
    #    $a1 the value you want to add to the tree

addNodeToTree :
    subu $sp, $sp, 4    # adjust the stack pointer 
    sw $ra, 0($sp)      # save the return address on stack 
    subu $sp, $sp, 4    # adjust the stack pointer 
    sw $a0, 0($sp)      # save the parameter 

    lw $a0, 0($a0)      # addNode(bTree->root);
    jal addNode

    lw $a0, 0($sp)      # get the parameter 
    addu $sp, $sp, 4        # adjust the stack pointer 
    lw $ra, 0($sp)      # get the return address
    addu $sp, $sp, 4    # adjust the stack pointer 

    jr $ra


# please implement this method
#
# add a node to tree
# 
# input: $a0 the address of the root node
#    $a1 the value you want to add to the tree
#
# hint: you need to write a recursive call here

addNode :

    jr $ra                  # to return

【问题讨论】:

标签: binary binary-search-tree mips


【解决方案1】:

在给定的代码中,addNodeToTree 是您在实际将节点插入树之前进行准备的地方。就 c 而言,这可以表示如下:

void addNodeToTree(struct BinaryTree btree, int newValue){
     struct Node root = btree->root;
     addNode(root, newValue);
}

void addNode(struct Node root, int newValue){
     //please implement this method
}

struct BinaryTree 的根地址、当前深度等在哪里可以保存在struct Node中;可以保持左孩子、右孩子和父节点等。

这里给出的真正任务是实现addNode,你的根地址在$a0 上,newValue 本身在$a1 上。因此,当 main 或任何其他函数中的某处使用二叉树的地址(不是根地址)和值调用 addNodeToTree 时,addNodeToTree 可以准备参数并调用 addNode 函数来完成实际工作。

【讨论】:

    猜你喜欢
    • 2017-05-08
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2023-03-08
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多