Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code to answer exercise 9.1 is below; however, it is incorrect. Please edit

ID: 3777379 • Letter: T

Question

The code to answer exercise 9.1 is below; however, it is incorrect. Please edit the code so that the value 38 is found in the variable "akey" at index 0.

Sorry incorrect, the value in akey 38 was NOT found in the array at index 0 Return to Exercise Close Window Your Code with color highlights added) index 0; while (index the array lengt if (akey thea inde array break else if key k the array[index] inde 2 index 1 else index 2 index 2; The Instructions: Exercise 9.1 Search ID Binary Tree Array The ID array thearray contains a binary tree with the root at index -0 Write a while loop to search for the value in the variable akey Write an ifelse to change the variable index to search the left or right child When the loop completes index must be the slot that equals variable akey Do not declare the variable index, but do initialize and change it during the search. Help Type this in first, then back up and fill it out: while f if else

Explanation / Answer

Please follow the code and comments for description :

CODE :

public class BSTClass { // class to run the code

    public static void main(String[] args) { // driver method
        int thearray[] = {1, 25, 38, 45, 51, 85, 97, 99}; // sample array data
        int index = 0, mid = 0, akey = 38; // initialise the variables
        int end = thearray.length - 1; // get the last index
        while (index <= end) { // check for the loop
            mid = (index + end) / 2; // get the mid value
            if (akey == thearray[mid]) { // check for the key value             
                break; // if found break the loop
            } else {
                if (akey < thearray[mid]) { // else check for the value at the index
                    end = mid - 1; // decrement the index
                } else {
                    index = mid + 1; // increment the index
                }
            }
        }
        System.out.println("The Desired Key is found at : " + mid); // print the data to console
    }
}


OUTPUT :

The Desired Key is found at : 2

Hope this is helpful.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote