1 Crore+ students have signed up on EduRev. Have you? 
What is the worst case time complexity for search, insert and delete operations in a general Binary Search Tree?
In skewed Binary Search Tree (BST), all three operations can take O(n). See the following example BST and operations.
Search 40.
Delete 40
Insert 50.
In delete operation of BST, we need inorder successor (or predecessor) of a node when the node to be deleted has both left and right child as nonempty. Which of the following is true about inorder successor needed in delete operation?
Let X be the node to be deleted in a tree with root as 'root'. There are three cases for deletion 1) X is a leaf node: We change left or right pointer of parent to NULL (depending upon whether X is left or right child of its parent) and we delete X 2) One child of X is empty: We copy values of nonempty child to X and delete the nonempty child 3) Both children of X are nonempty: In this case, we find inorder successor of X. Let the inorder successor be Y. We copy the contents of Y to X, and delete Y. Sp we need inorder successor only when both left and right child of X are not empty. In this case, the inorder successor Y can never be an ancestor of X. In this case, the inorder successor is the leftmost node in right subtree of X. Since it is leftmost node, the left child of Y must be empty.
We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree? (GATE CS 2011)
There is only one way. The minimum value has to go to the leftmost node and the maximum value to the rightmost node. Recursively, we can define for other nodes.
How many distinct binary search trees can be created out of 4 distinct keys?
Which of the following traversal outputs the data in sorted order in a BST?
Inorder traversal of a BST outputs data in sorted order. Read here for details.
Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the inorder traversal sequence of the resultant tree?
Inorder traversal of a BST gives elements in increasing order. So answer c is correct without any doubt.
The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the maximum distance of a leaf node from the root)? (GATE CS 2004)
Constructed binary search tree will be..
The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?
The following is the constructed tree
Consider the following Binary Search Tree
Q.
If we randomly search one of the keys present in above BST, what would be the expected number of comparisons?
Expected number of comparisons = (1*1 + 2*2 + 3*3 + 4*1)/7 = 18/7 = 2.57
Which of the following traversals is sufficient to construct BST from given traversals 1) Inorder 2) Preorder 3) Postorder
When we know either preorder or postorder traversal, we can construct the BST. Note that we can always sort the given traversal and get the inorder traversal. Inorder traversal of BST is always sorted.
Consider the following code snippet in C. The function print() receives root of a Binary Search Tree (BST) and a positive integer k as arguments.
// A BST node
struct node {
int data;
struct node *left, *right;
};
int count = 0;
void print(struct node *root, int k)
{
if (root != NULL && count <= k)
{
print(root>right, k);
count++;
if (count == k)
printf("%d ", root>data);
print(root>left, k);
}
}
Q. What is the output of print(root, 3) where root represent root of the following BST.
The code mainly finds out k'th largest element in BST, see K’th Largest Element in BSTfor details.
Consider the same code as given in above question. What does the function print() do in general? The function print() receives root of a Binary Search Tree (BST) and a positive integer k as arguments.
// A BST node
struct node {
int data;
struct node *left, *right;
};
int count = 0;
void print(struct node *root, int k)
{
if (root != NULL && count <= k)
{
print(root>right, k);
count++;
if (count == k)
printf("%d ", root>data);
print(root>left, k);
}
}
The function basically does reverse inorder traversal of the given Binary Search Tree. The reverse inorder traversal produces data in reverse sorted order. Whenever a nod is visited, count is incremented by 1 and data of a node is printed only when count becomes k.
You are given the postorder traversal, P, of a binary search tree on the n elements 1, 2, ..., n. You have to determine the unique binary search tree that has P as its postorder traversal. What is the time complexity of the most efficient algorithm for doing this?
One important thing to note is, it is Binary Search Tree, not Binary Tree. In BST, inorder traversal can always be obtained by sorting all keys.
Suppose we have a balanced binary search tree T holding n numbers. We are given two numbers L and H and wish to sum up all the numbers in T that lie between L and H. Suppose there are m such numbers in T. If the tightest upper bound on the time to compute the sum is O(n^{a}log^{b} n + m^{c}log^{d} n), the value of a + 10b + 100c + 1000d is ____.
int getSum(node *root, int L, int H)
{
// Base Case
if (root == NULL)
return 0;
if (root>key < L)
return getSum(root>right, L, H);
if (root>key > H)
return getSum(root>left, L, H)
if (root>key >= L && root>key <=H)
return getSum(root>left, L, H) + root>key + getSum(root>right, L, H);
}
The above always takes O(m + Logn) time. Note that the code first traverses across height to find the node which lies in range. Once such a node is found, it recurs for left and right children. Two recursive calls are made only if the node is in range. So for every node that is in range, we make at most one extra call (here extra call means calling for a node that is not in range).
Let T(n) be the number of different binary search trees on n distinct elements. Then
where x is
The idea is to make a key root, put (k1) keys in one subtree and remaining nk keys in other subtree. A Binary Search Tree (BST) is a tree in which all the nodes follow the belowmentioned properties −
Now construction binary search trees from n distinct number Lets for simplicity consider n distinct numbers as first n natural numbers (starting from 1) If n=1 We have only one possibility, therefore only 1 BST. If n=2 We have 2 possibilities , when smaller number is root and bigger number is the right child or second when the bigger number is root and smaller number as left child.
If n=3 We have 5 possibilities. Keeping each number first as root and then arranging the remaining 2 numbers as in case of n=2.
n=4 We have 14 possibilities. Taking each number as root and arranging smaal numbers as left subtree and larger numbers as right subtree.
Thus we can conclude that with n distinct numbers, if we take ‘k’ as root then all the numbers smaller than k will left subtree and numbers larger than k will be right subtree where the the right subtree and left subtree will again be constructed recursively like the root. Therefore,
What are the worstcase complexities of insertion and deletion of a key in a binary search tree?
The time taken by search, insert and delete on a BST is always proportional to height of BST. Height may become O(n) in worst case.
While inserting the elements 71, 65, 84, 69, 67, 83 in an empty binary search tree (BST) in the sequence shown, the element in the lowest level is
Here is The Insertion Algorithm For a Binary Search Tree :
Insert(Root,key)
{
if(Root is NULL)
Create a Node with value as key and return
Else if(Root.key <= key) Insert(Root.left,key)
Else
Insert(Root.right,key)
}
Creating the BST one by one using the above algorithm in the below given image :
The number of ways in which the numbers 1, 2, 3, 4, 5, 6, 7 can be inserted in an empty binary search tree, such that the resulting tree has height 6, is _____________ Note: The height of a tree with a single node is 0. [This question was originally a FillintheBlanks question]
To get height 6, we need to put either 1 or 7 at root. So count can be written as T(n) = 2*T(n1) with T(1) = 1
Therefore count is 2^{6} = 64 Another Explanation: Consider these cases, 1 2 3 4 5 6 7 1 2 3 4 5 7 6 1 7 6 5 4 3 2 1 7 6 5 4 2 3 7 6 5 4 3 2 1 7 6 5 4 3 1 2 7 1 2 3 4 5 6 7 1 2 3 4 6 5 For height 6, we have 2 choices. Either we select the root as 1 or 7. Suppose we select 7. Now, we have 6 nodes and remaining height = 5. So, now we have 2 ways to select root for this subtree also. Now, we keep on repeating the same procedure till remaining height = 1 For this last case also, we have 2 ways. Therefore, total number of ways = 2^{6}= 64
Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 55. Which of the following sequences CANNOT be the sequence of nodes examined?
In BST on right side of parent number should be greater than it, but in C after 47, 43 appears that is wrong.
When searching for the key value 60 in a binary search tree, nodes containing the key values 10, 20, 40, 50, 70 80, 90 are traversed, not necessarily in the order given. How many different orders are possible in which these key values can occur on the search path from the root to the node containing the value 60?
There are two set of values, smaller than 60 and greater than 60. Smaller values 10, 20, 40 and 50 are visited, means they are visited in order. Similarly, 90, 80 and 70 are visited in order.
= 7!/(4!3!)
= 35
3 videos7 docs100 tests

Use Code STAYHOME200 and get INR 200 additional OFF

Use Coupon Code 
3 videos7 docs100 tests








