Software Development Exam  >  Software Development Test  >  DSA in C++  >  Test: Trees - 2 - Software Development MCQ

Trees - 2 - Free MCQ Practice Test with solutions, Software Development


MCQ Practice Test & Solutions: Test: Trees - 2 (15 Questions)

You can prepare effectively for Software Development DSA in C++ with this dedicated MCQ Practice Test (available with solutions) on the important topic of "Test: Trees - 2". These 15 questions have been designed by the experts with the latest curriculum of Software Development 2026, to help you master the concept.

Test Highlights:

  • - Format: Multiple Choice Questions (MCQ)
  • - Duration: 30 minutes
  • - Number of Questions: 15

Sign up on EduRev for free to attempt this test and track your preparation progress.

Test: Trees - 2 - Question 1

In a binary search tree, the time complexity of the search operation is:

Detailed Solution: Question 1

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.

Test: Trees - 2 - Question 2

Which of the following operations can be efficiently performed on a binary search tree?

Detailed Solution: Question 2

All of the mentioned operations can be efficiently performed on a binary search tree. Insertion and deletion of elements maintain the binary search tree property, and the elements can be sorted in ascending order by performing an inorder traversal. Additionally, finding the kth smallest element can be done efficiently using inorder traversal or other techniques.

Test: Trees - 2 - Question 3

In a binary search tree, if the values are inserted in a sorted order, the resulting tree will be:

Detailed Solution: Question 3

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.

Test: Trees - 2 - Question 4

#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

Node* findMin(Node* root) {
   if (root == NULL)
      return NULL;

   if (root->left == NULL)
      return root;

   return findMin(root->left);
}

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;

   Node* minNode = findMin(root);

   cout << minNode->data << endl;
   return 0;
}
What will be the output of the above code?

Detailed Solution: Question 4

The code defines a function 'findMin' that finds the minimum value in a binary search tree. The tree is created with six nodes, and the function is called with the root node. The output is the minimum value, which is 5.

Test: Trees - 2 - Question 5

#include <iostream>
using namespace std;

struct Node {
   int data;
   Node* left;
   Node* right;
};

void deleteTree(Node* root) {
   if (root == NULL)
      return;

   deleteTree(root->left);
   deleteTree(root->right);

   delete root;
}

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;
   root->left->left = new Node();
   root->left->left->data = 40;
   root->left->right = new Node();
   root->left->right->data = 50;

   deleteTree(root);

   cout << root->data << endl;
   return 0;
}
What will be the output of the above code?

Detailed Solution: Question 5

The code defines a function 'deleteTree' that recursively deletes all the nodes in a binary tree. After calling 'deleteTree' with the root node, the 'root' pointer becomes a dangling pointer, and accessing its data will lead to undefined behavior. The output cannot be determined.

Test: Trees - 2 - Question 6

What is a tree data structure?

Detailed Solution: Question 6

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.

Test: Trees - 2 - Question 7

Which traversal technique visits the root node first?

Detailed Solution: Question 7

Preorder traversal visits the root node first, then the left subtree, and finally the right subtree.

Test: Trees - 2 - Question 8

Which traversal technique visits the left subtree first?

Detailed Solution: Question 8

Inorder traversal visits the left subtree first, then the root node, and finally the right subtree.

Test: Trees - 2 - Question 9

In a binary search tree, which property holds true for any node?

Detailed Solution: Question 9

In a binary search tree, the value of the right child is greater than the value of the node, while the value of the left child is less than the value of the node.

Test: Trees - 2 - Question 10

What is the time complexity of searching for an element in a binary search tree?

Detailed Solution: Question 10

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.

Test: Trees - 2 - Question 11

What will be the output of the following code?
#include <iostream>

struct Node {
   int data;
   Node* left;
   Node* right;
};

Node* createNode(int data) {
   Node* newNode = new Node();
   newNode->data = data;
   newNode->left = nullptr;
   newNode->right = nullptr;

   return newNode;
}

void insertNode(Node* root, int data) {
   if (root->data > data) {
      if (root->left == nullptr) {
         root->left = createNode(data);
      } else {
         insertNode(root->left, data);
      }
   } else {
      if (root->right == nullptr) {
         root->right = createNode(data);
      } else {
         insertNode(root->right, data);
      }
   }
}

void inOrderTraversal(Node* node) {
   if (node == nullptr)
      return;

   inOrderTraversal(node->left);
   std::cout << node->data << " ";
   inOrderTraversal(node->right);
}

int main() {
   Node* root = createNode(10);
   insertNode(root, 20);
   insertNode(root, 30);

   inOrderTraversal(root);

   delete root->left;
   delete root->right;
   delete root;

   return 0;
}

Detailed Solution: Question 11

The code inserts nodes with values 20 and 30 into the binary search tree and performs an inorder traversal. The output will be "20 10 30".

Test: Trees - 2 - Question 12

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;
}

Detailed Solution: Question 12

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"

    Test: Trees - 2 - Question 13

    Which traversal technique is used in the following code?
    #include <iostream>

    struct Node {
       int data;
       Node* left;
       Node* right;
    };

    void preOrderTraversal(Node* node) {
       if (node == nullptr)
          return;

       std::cout << node->data << " ";
       preOrderTraversal(node->left);
       preOrderTraversal(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;

       preOrderTraversal(root);

       delete root->left;
       delete root->right;
       delete root;

       return 0;
    }

    Detailed Solution: Question 13

    The code performs a preorder traversal of the tree, which visits the root node first, then the left subtree, and finally the right subtree.

    Test: Trees - 2 - Question 14

    What will be the output of the following code?
    #include <iostream>

    struct Node {
       int data;
       Node* left;
       Node* right;
    };

    int findMin(Node* root) {
       if (root == nullptr)
          return INT_MAX;

       int minVal = root->data;
       int leftMin = findMin(root->left);
       int rightMin = findMin(root->right);

       if (leftMin < minVal)
          minVal = leftMin;
       if (rightMin < minVal)
          minVal = rightMin;

       return minVal;
    }

    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;

       int minValue = findMin(root);

       delete root->left;
       delete root->right;
       delete root;

       std::cout << minValue;

       return 0;
    }

    Detailed Solution: Question 14

    The code finds the minimum value in the tree using a recursive function. The minimum value in the given tree is 20.

    Test: Trees - 2 - Question 15

    What will be the output of the following code?
    #include <iostream>

    struct Node {
       int data;
       Node* left;
       Node* right;
    };

    bool searchNode(Node* root, int key) {
       if (root == nullptr)
          return false;

       if (root->data == key)
          return true;
       else if (key < root->data)
          return searchNode(root->left, key);
       else
          return searchNode(root->right, key);
    }

    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;

       bool isFound = searchNode(root, 20);

       delete root->left;
       delete root->right;
       delete root;

       std::cout << std::boolalpha << isFound;

       return 0;
    }

    Detailed Solution: Question 15

    The code searches for the node with a value of 20 in the binary search tree and returns true if it is found. In this case, the node is found, so the output will be "true".

    152 videos|118 docs|24 tests
    Information about Test: Trees - 2 Page
    In this test you can find the Exam questions for Test: Trees - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Trees - 2, EduRev gives you an ample number of Online tests for practice
    152 videos|118 docs|24 tests
    Download as PDF