Question

In: Computer Science

Use recursion function to derive computation time for Binary Search by drawing a recursion tree diagram...

Use recursion function to derive computation time for Binary Search by drawing a recursion tree diagram and using algebra calculation.

Solutions

Expert Solution

Here is the solution for your problem -

Binary search is a divide and conquer algorithm. So binary search reduces the search space to half in every call.

Given A is an array of n elements where elements are in sorted order.
Let the key element to be searched be x.
If x is present in the array we will return its position else return -1.

Let us start with low=0 and high = n-1. In each step we will find mid value in the array and compare it with key element.

there are 3 cases possible -

1) If key=A[mid], we will return mid

2) If key<A[mid] , we will discard all elemnts in right half including the midlle element , and make our new high as mid-1.

3) If key>A[mid], we will discard all the elements present in left half including the middle element, and make our new low as mid+1.

We will repeat the above three steps until we found the key element of our low becomes greater than high .
Algorithm-
Algorithm binary_search(int A[], int low, int high, int x)
{

if(low>high) { return -1; }
mid=(low+high)/2;
if(x==A[mid])
return mid;
else if(x < A[mid])
binary_search(A, low, mid-1, x);
else binary_search(A, mid+1, high, x);
}

Let n be the number of elements in array.
Let T(n) be the time taken to search a value in list of n elements.
BEST CASE TIME COMPLEXITY-
T(n)=1 for n=1
for best case time complexity will be O(1).

AVERAGE OR WORST CASE-
Every time the recursive function is called with half the number of elements than the previous call.
Each time we will make one comparision and divide the list to half i.e. n elements to n/2 elements.
T(n)= 1 + T(n/2) for n>1
Here T(n/2) is the time taken to search in an array of n/2 elements.

T(n) = 1 + T(n/2)
= 1 + [1 + T(n/4) ]
= 2 + T(n/4)
= 2 + [1 + T(n/8)]
= 3 + T(n/8)
= 3 + T(n/ 2^3) and so on

T(n) = k + T(n/2^k)

So computation time for binary serach will be O(log​​​​2​​​​​​ n)



Related Solutions

Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these structure emplate <typename T> class Tree {    struct TreeNode    {        T mData;        TreeNode* mLeft = nullptr;        TreeNode* mRight = nullptr;        TreeNode* mParent = nullptr;        bool mIsDead = false;        TreeNode()        {        }        TreeNode(T tData) : TreeNode()        {            mData = tData;...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order traversal, preorder traversal, and find. Please separate the code in the four parts and explain in detail what is happening. Also, if you can please basic C language. If not, then I understand. Thank you for your time. The test cases are 'm', 'd', 'g', 'r', 'p', 'b', and 'x'. Output: Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT