The instructions to answer my question are below in bulletpo
The instructions to answer my question are below in bulletpoints, and write the code in Java, thank you.
Search ID Binary Tree Array The 1D array my array contains a binary tree with the root at index = 0; Write a while loop to search for the value in the variable key Write an if-else to change the variable index to search the left or right child When the loop completes index must be the slot that equals variable key Do not declare the variable index, but do initialize and change it during the search. Implement the instructions above by placing your Code here:Solution
Here is the code for you:
index = 0; //Initializes index to root node.
while(index < myarray.length()) //Till there are elements in the array.
{
if(key == myarray[index]) //If the key equals current node.
break; //Stop looping.
else If not.
{
if(key < myarray[index]) //If the element to be searched is less than current node.
index = 2 * index + 1; //Move to the left subtree.
else //Else.
index = 2 * index + 2; //Move to the right subtree.
}
}
