Question: ; Binary Tree Search Program for VISUAL 2 ; Input: ; R 0 - Value to search ; R 1 - Pointer to the tree

; Binary Tree Search Program for VISUAL2
; Input:
; R0- Value to search
; R1- Pointer to the tree root (0x200)
; Output:
; R1- Address of the value if found, 0 if not found
start:
; Load the value to search
LD R2, R0, #0 ; R2 now holds the value to search
; Check if the tree root is null (empty tree)
CMP R1, #0
BEQ not_found ; If root is null, go to not_found
search_loop:
; Load the current node's value
LD R3, R1, #8 ; R3 holds the value of the current node
; Compare the current node's value with the target value
CMP R2, R3
BEQ found ; If equal, value found
; If target value is less than current node's value, move to the left child
BLT move_left
; If target value is greater than current node's value, move to the right child
BGT move_right
not_found:
; If value not found, return 0 in R1
MOV R1, #0
HALT ; End program
found:
; Value found, return the address (R1 points to the node)
HALT ; End program
move_left:
; Move to left child (left pointer)
LD R1, R1, #0 ; R1 now points to the left child
B search_loop ; Repeat the search loop
move_right:
; Move to right child (right pointer); Binary Tree Search Program for VISUAL2
; Input:
; R0- Value to search
; R1- Pointer to the tree root (0x200)
; Output:
; R1- Address of the value if found, 0 if not found
start:
; Load the value to search
LD R2, R0, #0 ; R2 now holds the value to search
; Check if the tree root is null (empty tree)
CMP R1, #0
BEQ not_found ; If root is null, go to not_found
search_loop:
; Load the current node's value
LD R3, R1, #8 ; R3 holds the value of the current node
; Compare the current node's value with the target value
CMP R2, R3
BEQ found ; If equal, value found
; If target value is less than current node's value, move to the left child
BLT move_left
; If target value is greater than current node's value, move to the right child
BGT move_right
not_found:
; If value not found, return 0 in R1
MOV R1, #0
HALT ; End program
found:
; Value found, return the address (R1 points to the node)
HALT ; End program
move_left:
; Move to left child (left pointer)
LD R1, R1, #0 ; R1 now points to the left child
B search_loop ; Repeat the search loop
move_right:
; Move to right child (right pointer)
LD R1, R1, #4 ; R1 now points to the right child
B search_loop ; Repeat the search loop
LD R1, R1, #4 ; R1 now points to the right child
B search_loop ; Repeat the search loop

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!