0

Computer Algorithm

Description: Binary Trees Data Structure
Number of Questions: 15
Created by:
Tags: Binary Trees Data Structure
Attempted 0/15 Correct 0 Score 0

Which of the following statements is wrong about trees?

  1. A binary tree is the one in which the internal nodes have atmost two children.

  2. File system in unix OS make use of trees.

  3. In a complete binary tree, the number of internal nodes is 1 less than the number of leaves.

  4. In a binary tree, the number of internal nodes is greater than the number of leaves.

  5. A tree with n nodes can have a maximum height n -1.


Correct Option: D
Explanation:

This statement is incorrect.

Which of the following is correct?

A. Running time cost O(n) B. Running time cost O(1) C. Running time cost O(logn)

  1. A search operation in an ordered list will take C.

  2. An insert operation in an ordered list will take B.

  3. A search operation in an un ordered list will take B.

  4. An insert operation in an un ordered list will take A.

  5. 1 and 2 are correct.


Correct Option: A
Explanation:

This is correct. In an ordered list searching will take up O(log n) time using binary search

Given a traversal result abcdfge and following statements -

  1. Above traversal is PreOrder Traversal
  2. Above traversal is PostOrder Traversal
  3. Above traversal is InOrder Traversal
  4. For the given tree, a is the root of the tree
  5. For the given tree, e is the root of the tree Which of the above statements is/are correct?

    Given an algorithm to traverse a tree Traverse(Node v){ visit(v); for each child w of v Traverse(w); }

  1. 1 and 5

  2. 2 and 5

  3. 3 and 4

  4. 2 and 4

  5. 1 and 4


Correct Option: E
Explanation:

This is correct, as in the given algorithm , first v node is visited and then child nodes, it is preorder traversal. So in the beginning of traversal root will be traversed

What would come in place of y and z respectively in the above algorithm?

An iterative algorithm to search for key k in a tree T is given below-
 search(T,k)
{
x = root(T);
while(x!=null or x!= k)
{
if(x < k) x= right[x];
else y
}
return z; 
}

  1. x = left[x] and x respectively

  2. x = k and x respectively

  3. x = right[x] and x respectively

  4. x = left[right[x]] and k respectively

  5. x != right[x] & x respectively


Correct Option: A
Explanation:

This is correct, as in the else part the condition which holds is if(x > k) then the next x should be left[x]. Again,  since at the end x only contains the node where the key is present, so x should be returned

The above algorithm ____________.

Following is an algorithm to return a key in a BST(Binary Search Tree).
Returnkey(Tree T)
{
x= root[T];
while(right[x] != null)
{
x = right[x];
}
return x;
}

  1. finds the maximum key in the tree

  2. finds the minimum key in the tree

  3. finds the root key in the tree

  4. finds the rightmost leaf-key in the tree

  5. finds the maximum internal node in the tree


Correct Option: A
Explanation:

This is correct, as  the algorithm updates the x value to right[x], it is looking for the maximum key.

After insertion of n keys, worst case running time of the algorithm would be _______________

An iterative algorithm to search for key k in a tree T is given below-
Assume that the tree has n nodes and height h.
search(T,k)
{
x = root(t);
while(x!=null or x!= k)
{
if(x > k) x= left[x];
else x = right[x];
}
return x;
}

  1. O(h)

  2. O(n)

  3. O(nh)

  4. O(n2)

  5. O(h2)


Correct Option: B
Explanation:

 This is correct, as the worst case will comprise of height n-1. So O(n) will be the running and is the specific answer.

This algorithm _______.

Given below is an algorithm:
if(right[x] != null)
{
x = right[x];
Treemin(x);
}
else
{
y = parent[x];
while(y !=null && x = right[y])
{
 x=y; y=parent[y];
}
return y;
}

  1. finds the successor of key x in the tree

  2. finds the predecessor of key x in the tree

  3. finds the maximum key in the tree

  4. finds the minimum key in the tree

  5. none of the above


Correct Option: A
Explanation:

This is correct. If right[x] != null, then the minimum key in the right sub-tree will be the successor otherwise it will trace its way to the parent node which is greater than key value.

Given numbers 1, 2, 3, 4, 5, 6…. N. Inorder to insert a random permutation of the numbers in binary search tree, what is the average running time?

  1. O(logn)

  2. O(nlogn)

  3. O(n2)

  4. O(n)

  5. O(1)


Correct Option: B
Explanation:

 This is correct,  since there are n! permutation. Taking the average of the entire time taken to insert any of the permutations would lead to a complexity of O(nlogn).

What technique is used to get a good running time for an algorithm such as quick sort, which has bad worst case running time but good average case?

  1. Randomization

  2. Polymorphism

  3. Synchronization

  4. Inheritance

  5. Encapsulation


Correct Option: A
Explanation:

This is correct. The process is called randomization,  which improves the average running time of cases where worst case running time is bad.

Which of the following is not correct about quicksort algorithm?

  1. The partition method returns the position of pivot element as it should be in the final sorted array.

  2. The average running time of quicksort is O(nlogn).

  3. Quicksort is faster than insertion sort and bubble sort.

  4. Best case running time of quicksort is O(nlogn).

  5. Worst case running time of quicksort is O(nlogn).


Correct Option: E
Explanation:

This is incorrect, the worst case running time of quicksort is quadratic.

Which of the following statements is incorrect about a complete binary tree with n nodes and height h?

  1. Level i has 2i nodes.

  2. Total number of internal nodes = Number of leaves - 1.

  3. Total number of leaves is 2h+1 - 1.

  4. The relation between the height and number of nodes is h = log2(n + 1/2).

  5. Number of internal nodes is 2h - 1.


Correct Option: C
Explanation:

 This is incorrect. Total number of nodes is given by this expression, and total number of leaves is 2h.

What is the running time of the algorithm?

An iterative algorithm to search for key k in a tree T is given below. Assume that the tree has n nodes and height h.
search(T,k)
{
x = root(t);
while(x!=null or x!= k)
{
if(x < k) x= right[x];
else x = left[x];
}
return x;
}

  1. O(n)

  2. O(n2)

  3. O(nh)

  4. O(h)

  5. O(h2)


Correct Option: D
Explanation:

 This is correct. Since the height of the tree is h, the search process will go upto height h only.

What would come in place of two in the above algorithm?

Here is an algorithm to insert a key k in a binary search tree T.
insert(T,k)
{
int flag = 0;
node x = root[T]; i
if(x == null)
{
root = new node(k);
}
while(x != null)
{
if(x > k)
{
y = x;
x = left[x];
flag = 1;
}
else
{
y = x;
x = right[x];
flag = 2;
}
}
if(flag ==1)
{
k = ?;
}
else
{
k = ?;
}
}

  1. right[x] and left[x] respectively

  2. right[y] and left[y] respectively

  3. left[x] and right[x] respectively

  4. left[y] and right[y] respectively

  5. left[x] and left[y] respectively


Correct Option: D
Explanation:

This is correct,  since y is the parent node to null value, this will designate the new key to correct place.

Given a sequence of numbers 8, 4, 9, 3, 2, 0, 11, 10 to be inserted in the tree. Which key will be visited last in the inorder traversal of the inserted keys?

Here is an algorithm to insert a key k in a binary search tree T.
insert(T,k)
{
int flag = 0;
node x = root[T];
if(x == null)
{
root = new node(k);
}
while(x != null)
{
if(x > k)
{
Y = x;
X = left[x];
flag = 1;
}
else
{
Y = x;
X = right[x];
flag = 2;
}
}
if(flag ==1)
{
K = left[y];
}
else
{
K = right[y];
}
}

  1. 10

  2. 9

  3. 11

  4. 8

  5. 4


Correct Option: C
Explanation:

This is correct. since with insertion and inorder traversal the rightmost key will be referred last. During insertion since 11 comes second last and only 10 comes after that which will be the left key of 11 so inorder traversal will visit 11 key only at the end.

What should come in place of ? in the above algorithm?

An algorithm to delete the node/key k from a tree T is given below:
delete(T, k)
{
if(left[k] == null && right[k] == null)
{
K = null;
}
else if(left[k] != null && right[k] == null)
{
if(k == left[parent[k]]) left[parent[k]] == left[k];
else right[parent[k]] = left[k];
 }
else if(left[k] == null && right[k] != null)
{
if(k == left[parent[k]]) left[parent[k]] == right[k];
else right[parent[k]] = right[k];
 }
else Y = ?;
X = y;
Right[x] = left[y];
 }
}

  1. Treemax(T)

  2. Successor(T, k)

  3. Predecessor(T, k)

  4. Treemin(T)

  5. Root[T]


Correct Option: C
Explanation:

This is the correct answer since when both right and left child node of the node to be deleted is not null, then the predecessor of the current node should be connected to the parent node

- Hide questions