All Exams  >   Software Development  >   DSA in C++  >   All Questions

All questions of Trees for Software Development Exam

What is the time complexity of searching for an element in a binary search tree?
  • a)
    O(1)
  • b)
    O(n)
  • c)
    O(log n)
  • d)
    O(n log n)
Correct answer is option 'C'. Can you explain this answer?

Codebreakers answered
The time complexity of searching for an element in a binary search tree is O(log n) in the average case, where n is the number of nodes in the tree.
1 Crore+ students have signed up on EduRev. Have you? Download the App

Which of the following operations is not typically performed on a binary search tree?
  • a)
    Insertion.
  • b)
    Deletion.
  • c)
    Search.
  • d)
    Sorting.
Correct answer is option 'B'. Can you explain this answer?

Juhi Datta answered
Explanation:

A binary search tree (BST) is a binary tree data structure that satisfies the binary search property. The binary search property states that for any node in the tree, the value of the node's left child is less than the value of the node, and the value of the node's right child is greater than the value of the node.

Insertion:
Insertion is a common operation performed on a binary search tree. When inserting a new value into a BST, the tree is traversed starting from the root node, comparing the new value with the values of each node. Based on the comparison, the new value is placed as the left child or the right child of a node until a suitable position is found. Insertion ensures that the binary search property is maintained.

Deletion:
Deletion is also a common operation performed on a binary search tree. When deleting a node from a BST, there are three cases to consider:
1. The node has no children: In this case, the node is simply removed from the tree.
2. The node has one child: In this case, the child node replaces the deleted node in the tree.
3. The node has two children: In this case, the node is replaced by its in-order successor or in-order predecessor in the tree, and the successor/predecessor is deleted.

Search:
Searching for a value in a binary search tree is another common operation. The tree is traversed starting from the root node, comparing the search value with the values of each node. If the search value is found, the search is successful. Otherwise, the search continues by moving to the left or right child of the current node based on the comparison. This process continues until the value is found or until a leaf node is reached, indicating that the value is not present in the tree.

Sorting:
Sorting is not typically performed directly on a binary search tree. While a binary search tree inherently stores its elements in a sorted order, the main purpose of a binary search tree is to efficiently search for and retrieve values. If the goal is to obtain a sorted list of elements, it is more common to perform an in-order traversal of the binary search tree and collect the elements in sorted order.

Therefore, the operation that is not typically performed on a binary search tree is Sorting (option 'D').

In a binary search tree, which property holds true for any node?
  • a)
    The value of the left child is greater than the value of the node.
  • b)
    The value of the right child is greater than the value of the node.
  • c)
    The value of the left child is less than the value of the node.
  • d)
    The value of the right child is less than the value of the node.
Correct answer is option 'B'. Can you explain this answer?

Jyoti Datta answered
Binary Search Tree (BST)
A binary search tree is a data structure in which each node has at most two children, referred to as the left child and the right child. The binary search tree follows a specific property that helps in efficient searching and sorting of data.

Property of Binary Search Tree
The property that holds true for any node in a binary search tree is that the value of the right child is greater than the value of the node. This property is known as the Binary Search Tree Property or the Binary Search Property.

Explanation
To understand why the value of the right child is greater than the value of the node, let's consider the following example:

Suppose we have the following binary search tree:

```
5
/ \
3 7
/ \ / \
2 4 6 8
```

In this binary search tree, every node follows the binary search tree property. For example, for the node with value 5, the value of its left child (3) is less than 5, and the value of its right child (7) is greater than 5.

If we consider any other node in the binary search tree, we can observe that the value of its left child is always less than the value of the node, and the value of its right child is always greater than the value of the node. This property holds true for all nodes in the binary search tree.

Importance of the Binary Search Tree Property
The binary search tree property is essential for efficient searching and sorting of data. It allows us to perform binary search operations, such as finding a specific value or determining the order of elements, in an optimized manner.

By following the binary search tree property, we can eliminate half of the search space at each step of the search process, leading to a logarithmic time complexity for operations like search, insert, and delete.

Therefore, option 'B' is the correct answer as it accurately represents the property of a binary search tree where the value of the right child is greater than the value of the node.

Which of the following traversal techniques visits the left subtree, then the root, and finally the right subtree?
  • a)
    Inorder traversal.
  • b)
    Preorder traversal.
  • c)
    Postorder traversal.
  • d)
    Level order traversal.
Correct answer is option 'A'. Can you explain this answer?

Inorder traversal visits the left subtree, then the root, and finally the right subtree. It is a depth-first traversal technique that follows the left-root-right order. In this traversal, all nodes are visited in ascending order if the tree is a binary search tree.

Here is a step-by-step explanation of how inorder traversal works:

1. Visit the left subtree: Start from the root node and recursively traverse the left subtree until you reach a leaf node (a node with no children).
2. Visit the root: Once you reach a leaf node or a node with no left child, visit the current node.
3. Visit the right subtree: After visiting the root node, recursively traverse the right subtree following the same order: left subtree, root, and right subtree.

This process continues until all nodes in the tree have been visited.

Example:

Let's consider a binary tree with the following structure:

```
1
/ \
2 3
/ \
4 5
```

The inorder traversal of this tree would be: 4 -> 2 -> 5 -> 1 -> 3.

Explanation of the traversal:

1. Start at the root node (1).
2. Visit the left subtree (2).
3. Visit the left subtree of node 2 (4).
4. No more left child of node 4, so visit node 4.
5. No more right child of node 4, so go back to node 2.
6. Visit node 2.
7. Visit the right subtree of node 2 (5).
8. Visit node 5.
9. No more left or right child of node 5, so go back to node 1.
10. Visit node 1.
11. Visit the right subtree of node 1 (3).
12. No more left child of node 3, so visit node 3.
13. No more right child of node 3, so traversal is complete.

Therefore, the inorder traversal of the given tree is 4 -> 2 -> 5 -> 1 -> 3.

In conclusion, the correct answer is option 'A' (Inorder traversal) as it visits the left subtree, then the root, and finally the right subtree.

Which of the following operations can be efficiently performed on a binary search tree?
  • a)
    Insertion and deletion of elements.
  • b)
    Sorting the elements in ascending order.
  • c)
    Finding the kth smallest element.
  • d)
    All of the above.
Correct answer is option 'D'. Can you explain this answer?

**Explanation:**

A binary search tree (BST) is a data structure that allows efficient insertion, deletion, and retrieval operations. The properties of a BST make it well-suited for these operations.

**a) Insertion and deletion of elements:**
- Insertion: To insert an element into a BST, the tree is traversed based on the value of the element being inserted. The element is compared with the current node, and based on the comparison, it is either inserted as the left child or the right child of the node. This process continues until the element is inserted at the appropriate position. The time complexity of insertion in a BST is O(log n) in the average case and O(n) in the worst case.
- Deletion: Deleting an element from a BST involves finding the element in the tree and then removing it while maintaining the BST properties. If the node to be deleted has no children, it can be simply removed. If it has one child, the child takes the place of the deleted node. If it has two children, the successor or predecessor node is used to replace the deleted node. The time complexity of deletion in a BST is also O(log n) in the average case and O(n) in the worst case.

**b) Sorting the elements in ascending order:**
- In a BST, the left subtree of any node contains elements that are smaller than the node, and the right subtree contains elements that are greater than the node. By performing an in-order traversal of the tree, all the elements will be visited in ascending order. The time complexity of sorting the elements in a BST through in-order traversal is O(n).

**c) Finding the kth smallest element:**
- In a BST, finding the kth smallest element involves performing an in-order traversal until the kth element is reached. This can be done by keeping track of the count of visited nodes during the traversal. The time complexity of finding the kth smallest element in a BST is O(k), which is efficient compared to other data structures.

**Conclusion:**
- A binary search tree supports efficient insertion, deletion, sorting the elements in ascending order, and finding the kth smallest element.
- Therefore, the correct answer is option D: All of the above.

Which traversal technique can be used to obtain a sorted sequence of elements from a binary search tree?
  • a)
    Preorder traversal.
  • b)
    Inorder traversal.
  • c)
    Postorder traversal.
  • d)
    Level order traversal.
Correct answer is option 'B'. Can you explain this answer?

Om Mehta answered
Explanation:

In a binary search tree (BST), elements are stored in such a way that the values of the left subtree are less than the root node, and the values of the right subtree are greater than the root node.

To obtain a sorted sequence of elements from a BST, we can use the inorder traversal technique. In inorder traversal, we recursively traverse the left subtree, visit the root node, and then recursively traverse the right subtree. This ensures that we visit the nodes in ascending order.

Here's how the inorder traversal works:
1. Start at the root node.
2. Recursively traverse the left subtree.
3. Visit the current node.
4. Recursively traverse the right subtree.

Example:
Let's consider the following binary search tree:

```
5
/ \
3 7
/ \ \
2 4 8
```

When we perform an inorder traversal on this tree, the sequence of nodes visited will be: 2, 3, 4, 5, 7, 8. As we can see, the elements are visited in ascending order.

Benefits of Inorder Traversal:
1. Inorder traversal gives us a sorted sequence of elements from a binary search tree.
2. It allows us to efficiently retrieve elements in sorted order.
3. It can be used to validate whether a binary tree is a valid binary search tree.

Therefore, the correct answer is option B) Inorder traversal to obtain a sorted sequence of elements from a binary search tree.

What is a tree data structure?
  • a)
    A linear data structure
  • b)
    A hierarchical data structure
  • c)
    A circular data structure
  • d)
    A self-referential data structure
Correct answer is option 'B'. Can you explain this answer?

Codebreakers answered
A tree data structure is a hierarchical data structure consisting of nodes connected by edges. It starts with a root node and each node can have zero or more child nodes.

Code and Output (HOTS):
#include <iostream>
using namespace std;
struct Node {
   int data;
   Node* left;
   Node* right;
};
bool isBinarySearchTree(Node* root, int minValue, int maxValue) {
   if (root == NULL)
      return true;
   if (root->data < minValue || root->data > maxValue)
      return false;
   return isBinarySearchTree(root->left, minValue, root->data - 1) &&
          isBinarySearchTree(root->right, root->data + 1, maxValue);
}
int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->left->data = 5;
   root->right = new Node();
   root->right->data = 15;
   root->right->left = new Node();
   root->right->left->data = 12;
   root->right->right = new Node();
   root->right->right->data = 18;
   bool isBST = isBinarySearchTree(root, INT_MIN, INT_MAX);
   cout << isBST << endl;
   return 0;
}
What will be the output of the above code?
  • a)
    0
  • b)
    1
  • c)
    -1
  • d)
    Compilation Error
Correct answer is option 'B'. Can you explain this answer?

The code defines a struct 'Node' which represents a node in a binary tree. The function 'isBinarySearchTree' is used to check if the given binary tree is a valid binary search tree.
In the 'main' function, a binary tree is created with the following structure:
      10
     /  \
    5    15
        /  \
       12   18
The function 'isBinarySearchTree' is then called with the root of the tree and the minimum and maximum values as arguments. The function recursively checks if each node satisfies the binary search tree property, which states that for any node, all the values in its left subtree should be less than its value, and all the values in its right subtree should be greater than its value.
In this case, all the nodes in the tree satisfy the binary search tree property, so the function returns true. The variable isBST is assigned the value true, which is equivalent to 1. Finally, 'isBST' is printed, resulting in the output 1.

In a binary search tree, the time complexity of the search operation is:
  • a)
    O(log N), where N is the total number of nodes in the tree.
  • b)
    O(N), where N is the total number of nodes in the tree.
  • c)
    O(1), as the search can be done in constant time.
  • d)
    O(N log N), where N is the total number of nodes in the tree.
Correct answer is option 'A'. Can you explain this answer?

Hackers World answered
In a binary search tree, the search operation has a time complexity of O(log N), where N is the total number of nodes in the tree. This is because the search narrows down the search space by half at each step, similar to a binary search in a sorted array.

Code and Output:
#include <iostream>
using namespace std;
struct Node {
   int data;
   Node* left;
   Node* right;
};
void preorderTraversal(Node* root) {
   if (root == NULL)
      return;
   cout << root->data << " ";
   preorderTraversal(root->left);
   preorderTraversal(root->right);
}
int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->left->data = 20;
   root->right = new Node();
   root->right->data = 30;
   preorderTraversal(root);
   return 0;
}
What will be the output of the above code?
  • a)
    10 20 30
  • b)
    20 10 30
  • c)
    10 30 20
  • d)
    20 30 10
Correct answer is option 'A'. Can you explain this answer?

The code defines a binary tree structure using the 'Node' struct, where each node has an integer value ('data') and pointers to its left and right child nodes. In the 'preorderTraversal' function, a recursive pre-order traversal is performed on the tree.
In the 'main' function, a binary tree is constructed with a root node having a value of 10. The left child of the root is assigned a value of 20, and the right child is assigned a value of 30.
The 'preorderTraversal' function is then called with the root node as the argument, which prints the data of the current node, recursively calls 'preorderTraversal' for the left subtree, and then recursively calls 'preorderTraversal' for the right subtree.

What will be the output of the following code?
#include <iostream>
struct Node {
   int data;
   Node* left;
   Node* right;
};
Node* deleteNode(Node* root, int key) {
   if (root == nullptr)
      return root;
   if (key < root->data) {
      root->left = deleteNode(root->left, key);
   } else if (key > root->data) {
      root->right = deleteNode(root->right, key);
   } else {
      if (root->left == nullptr) {
         Node* temp = root->right;
         delete root;
         return temp;
      } else if (root->right == nullptr) {
         Node* temp = root->left;
         delete root;
         return temp;
      }
      Node* temp = root->right;
      while (temp->left != nullptr) {
         temp = temp->left;
      }
      root->data = temp->data;
      root->right = deleteNode(root->right, temp->data);
   }
   return root;
}
void inOrderTraversal(Node* node) {
   if (node == nullptr)
      return;
   inOrderTraversal(node->left);
   std::cout << node->data << " ";
   inOrderTraversal(node->right);
}
int main() {
   Node* root = new Node();
   root->data = 10;
   root->left = new Node();
   root->right = new Node();
   root->left->data = 20;
   root->right->data = 30;
   root = deleteNode(root, 20);
   inOrderTraversal(root);
   delete root->left;
   delete root->right;
   delete root;
   return 0;
}
  • a)
    10 20 30
  • b)
    20 10 30
  • c)
    30 20 10
  • d)
    10 30
Correct answer is option 'D'. Can you explain this answer?

Qudrat Chauhan answered
The code deletes the node with a value of 20 from the binary search tree and performs an inorder traversal. The output will be "10 30"

    In a binary search tree, if the values are inserted in a sorted order, the resulting tree will be:
    • a)
      Balanced.
    • b)
      Unbalanced.
    • c)
      Completely filled.
    • d)
      None of the above.
    Correct answer is option 'B'. Can you explain this answer?

    Coders Trust answered
    If the values are inserted in a sorted order into a binary search tree, the resulting tree will be unbalanced, with one long branch of nodes. This is because the tree's structure depends on the order of insertion, and inserting values in sorted order leads to a skewed tree.

    Chapter doubts & questions for Trees - DSA in C++ 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

    Chapter doubts & questions of Trees - DSA in C++ in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

    DSA in C++

    153 videos|115 docs|24 tests

    Top Courses Software Development

    Signup to see your scores go up within 7 days!

    Study with 1000+ FREE Docs, Videos & Tests
    10M+ students study on EduRev