Question

In: Computer Science

Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p...

  • Given the pseudocode for Binary Search Algorithm as below:

BinarySearch(A, p, r, V)

   if p < r

q = (p + r)/2

if V = A[q]

return q

else if V > A[q]

return BinarySearch(A, q+1, r, V)

   else return BinarySearch(A, p, q-1)

else if p = r,

   if V = A[p]

   return p

else

return -1

   return -1

end function

Using this pseudocode, write a function for BinarySearch and also complete the program, by writing a main, which will supply the array A (you may like to use the same main as the other programming exercise you have done so far), and also get an user input for V (which should have same data type as the array itself).

Call the function BinarySearch by sending the array, and 1 for p, N for r and V, store the value in a variable x, which stores the position number of the searched key V.

Then check if x is -1, then display data is not found, otherwise display the value of x, by using a suitable title, saying Data V is found in location x (value of x should be displayed).

Must compile, run and copy and paste the program underneath this word document.

2. a) Construct a Binary Search Tree using the following data:

54 37 17 28 44 71 64 60

b) Illustrate how will you search for 30 from the above tree and how many searches will be needed.

c) Illustrate how you will delete the root from the above tree, redraw the tree after deletion.

d) Add a new data after conducting operation c, say the new value = 50

(Do not start from scratch, show this operation by adding onto existing Tree).

e) Write down the pre-order, post-order and in-order traversal.

Solutions

Expert Solution

// Hope this helps you
//If u face any doubt feel free to ask in the comments 
// I will be very happy to help you

#include <bits/stdc++.h> 
using namespace std; 
// Recursive function of binary search
int binarySearch(int arr[], int l, int r, int x) //Parameter array,start index,end index,and the value to be searched
{ 
        if (r >= l) 
        { 
                int mid = l + (r - l) / 2; 
                // If the element is present at the middle itself
                if (arr[mid] == x) 
                        return mid; 

                // If element is smaller than mid, then 
                // it can only be present in left subarray 
                if (arr[mid] > x) 
                        return binarySearch(arr, l, mid - 1, x); // calling for the left subarray

                // Else the element can only be present 
                // in right subarray 
                return binarySearch(arr, mid + 1, r, x); // calling function for right subarray
        } 

        // We reach here when element is not 
        // present in array 
        return -1; // No found in the array
} 

int main() 
{ 
        int arr[] = {2,5,8,10,15,25,32,45}; 
        int V;
        cout<<"Enter the item to be searched: ";
        cin>>V;
        int n = sizeof(arr) / sizeof(arr[0]); 
        int x = binarySearch(arr, 0, n - 1, V); // return -1 if not found else return position of data
        (x == -1) ? cout << "Data is not found"
                                : cout << "Data "<<V<<" is present at index " << x; 
        return 0; 
} 

Code screenshots:

OUTPUT SCREENSHOTS:

2)

part (a) to (d) are in image

(e) Preorder traversal of the given tree in a part is:

54,37,17,28,44,71,64,60

Inorder Traversal is:

17,28,37,44,54,60,64,71

Postorder traversal is:

28,17,44,37,60,64,71,54


Related Solutions

20.14 Program BinarySearch Objectives Examine a binary search algorithm Debug existing code Instructions For this lab...
20.14 Program BinarySearch Objectives Examine a binary search algorithm Debug existing code Instructions For this lab you will code directly in ZyBooks. That means no uploading a file. If you wish, you can copy the template code into your IDE, work out a solution, and paste that into the code window. The problem The code does not work on certain data sets. Fix the sets but do not alter the binary search algorithm. The obvious Yes, this is a problem...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm...
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 0 1 2 3 4 5 6 7 8 9 33 32 27 26 24 22 21 8 7 3 IN C++ PLEASE
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm...
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 0 1 2 3 4 5 6 7 8 9 33 32 27 26 24 22 21 8 7 3
Show that if P=NP then there is a polynomial-time algorithm for the following search problem: given...
Show that if P=NP then there is a polynomial-time algorithm for the following search problem: given a graph, find its largest clique.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT