View the step-by-step solution to:

Question

can anyone help me with this MIPS code, it is for implementing binary tree in MIPS assembly language. The skeleton

of the code is here bellow and I need to fill with the functions like the addNode method , the delete method,  the contain method, and the free method in the blank space.....

.data

msg1:      .asciiz "This tree contains: "

msg2:      .asciiz "This tree doesn't contains: "

msg3:      .asciiz "After delete: "

newline:   .asciiz "n"

space:       .asciiz " "

.text

.globl main

main:

    li $a0, 72      # struct bTree* btree = newTree(72);

    jal newTree     #

    move $t0, $v0        # copy the address of bTree to t0

    

    move $a0, $t0        # add(root, 12);

    li $a1, 12      #

    subu $sp, $sp, 4 # adjust the stack pointer

    sw $t0, 0($sp)      # save the parameter

    jal addNodeToTree #

    lw $t0, 0($sp)      # get the return address

    addu $sp, $sp, 4 # adjust the stack pointer

    

    move $a0, $t0        # add(root, 52);

    li $a1, 52      #

    subu $sp, $sp, 4 # adjust the stack pointer

    sw $t0, 0($sp)      # save the parameter

    jal addNodeToTree #

    lw $t0, 0($sp)      # get the return address

    addu $sp, $sp, 4 # adjust the stack pointer

    

    move $a0, $t0        # add(root, 87);

    li $a1, 87      #

    subu $sp, $sp, 4 # adjust the stack pointer

    sw $t0, 0($sp)      # save the parameter

    jal addNodeToTree #

    lw $t0, 0($sp)      # get the return address

    addu $sp, $sp, 4 # adjust the stack pointer

    

    move $a0, $t0        # add(root, 18);

    li $a1, 18      #

    subu $sp, $sp, 4 # adjust the stack pointer

    sw $t0, 0($sp)      # save the parameter

    jal addNodeToTree #

    lw $t0, 0($sp)      # get the return address

    addu $sp, $sp, 4 # adjust the stack pointer

    

    move $a0, $t0        # add(root, 49);

    li $a1, 49      #

    subu $sp, $sp, 4 # adjust the stack pointer

    sw $t0, 0($sp)      # save the parameter

    jal addNodeToTree #

    lw $t0, 0($sp)      # get the return address

    addu $sp, $sp, 4 # adjust the stack pointer

               

    move $a0, $t0        # printTree(bTree);

    subu $sp, $sp, 4 # adjust the stack pointer

    sw $t0, 0($sp)      # save the parameter

    jal printTree        #

    lw $t0, 0($sp)      # get the return address

    addu $sp, $sp, 4 # adjust the stack pointer

# you can add more test cases here

...........................................

    li $v0, 10      # exit the program

    syscall

# construct a tree

# input: $a0 the value of the root node

# output: $v0 the address of the tree

newTree :

    move $t0, $a0        # copy the value of the root node to t0  

   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  

    li $a0, 3       # (struct Node*)malloc(sizeof(struct Node));

    jal malloc      # length space for root node

    move $t1, $v0        # copy the address of root node to t1

    sw $t0, 0($t1)       #root->value = value;

    li $t2, 0

    sw $t2, 8($t1)       #root->rightChild = NULL;

    sw $t2, 4($t1)       #root->leftChild = NULL;

    li $a0, 1       # (struct bTree*)malloc(sizeof(struct bTree));

    jal malloc      # length space for bTree

    move $t2, $v0        # copy the address of root node to t2  

    sw $t1, 0($t2)       # bTree->root = root;

    move $v0, $t2        # set the return value to v0

    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

    # 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 recursive call here

addNode :

....................................................................

...............................................................

    jr $ra               # to return

# please implement this method

#

# delete a node from tree, you still need to use free() to

# release the memory when a node is removed

# input: $a0 the address of the tree

#   $a1 the value you want to delete from the tree

# hint: like addNodeToTree or printTree method, you may need

#   to create other recursive method to implement delete

delete:

....................................................

...................................................

    jr $ra

# please implement this method

# check whether the tree contains the input value

# input: $a0 the address of the tree

#   $a1 the value you want to searce from the tree

# output: $v0 | 1:if the tree contains this value;

#        | 0: if the tree doesn't contain this value

# hint: like addNodeToTree or printTree method, you may need

#   to create other recursive method to implement contain

contain :

..............................................................

...............................................................

    jr $ra

# print the tree

# input: $a0 the address of the tree

printTree:

    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)       # printInorder(bTree->root);

    jal printInorder

    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  

    la $a0, newline      # printf("n");

    li $v0, 4

    syscall  

    jr $ra

# print the tree in order

# input: $a0 the address of the root node

printInorder:

    move $t0, $a0

    bne $t0, 0, else     # if $node == null return

    jr $ra               # to return

else:

    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 $t0, 0($sp)       # save the parameter on stack    

    lw $a0, 4($t0)      # printInorder(node->leftChild);

    jal printInorder  

    lw $t0, 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

    lw $a0, 0($t0)      # print node->value

    li $v0, 1

    syscall  

    la $a0, space

    li $v0, 4

    syscall    

    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 $t0, 0($sp)       # save the parameter on stack  

    lw $a0, 8($t0)       # printInorder(node->rightChild);

    jal printInorder  

    lw $t0, 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

# dynamically allocated memory block

# input: $a0 the size of memory block in words

# output: $v0 the address to the allocated memory

malloc:          

    mul $a0, $a0, 4 # the number of bytes you need.

    li $v0,9     # allocate memory

    syscall           

    jr $ra

# please implement this method

# free the memory block, here we can simply set

# all bits of the memory blocks to be zero

# input: $a0 the address of memory block

#   $a1 the size of memory block in words

free:

.............................................

.......................................

    jr $ra

Top Answer

I had a similar answer. you can use my code as reference purpose. .text .macro reset_reg li $t0, 0 li $t1, 0 li $t2, 0 li... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question