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 VISUAL
; Input:
; R Value to search
; R Pointer to the tree root x
; Output:
; R Address of the value if found, if not found
start:
; Load the value to search
LD R R # ; R now holds the value to search
; Check if the tree root is null empty tree
CMP R #
BEQ notfound ; If root is null, go to notfound
searchloop:
; Load the current node's value
LD R R # ; R holds the value of the current node
; Compare the current node's value with the target value
CMP R R
BEQ found ; If equal, value found
; If target value is less than current node's value, move to the left child
BLT moveleft
; If target value is greater than current node's value, move to the right child
BGT moveright
notfound:
; If value not found, return in R
MOV R #
HALT ; End program
found:
; Value found, return the address R points to the node
HALT ; End program
moveleft:
; Move to left child left pointer
LD R R # ; R now points to the left child
B searchloop ; Repeat the search loop
moveright:
; Move to right child right pointer; Binary Tree Search Program for VISUAL
; Input:
; R Value to search
; R Pointer to the tree root x
; Output:
; R Address of the value if found, if not found
start:
; Load the value to search
LD R R # ; R now holds the value to search
; Check if the tree root is null empty tree
CMP R #
BEQ notfound ; If root is null, go to notfound
searchloop:
; Load the current node's value
LD R R # ; R holds the value of the current node
; Compare the current node's value with the target value
CMP R R
BEQ found ; If equal, value found
; If target value is less than current node's value, move to the left child
BLT moveleft
; If target value is greater than current node's value, move to the right child
BGT moveright
notfound:
; If value not found, return in R
MOV R #
HALT ; End program
found:
; Value found, return the address R points to the node
HALT ; End program
moveleft:
; Move to left child left pointer
LD R R # ; R now points to the left child
B searchloop ; Repeat the search loop
moveright:
; Move to right child right pointer
LD R R # ; R now points to the right child
B searchloop ; Repeat the search loop
LD R R # ; R now points to the right child
B searchloop ; Repeat the search loop
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
