Tutorial-9_2 _Solution
Tutorial-9_2 _Solution
Type- Core
Course Code- CSET202 Course Name- Data Structure using C++
CO-Mapping
CO1 CO2 CO3
Q1 √
Q2 √
Q3 √
Q4 √
Q5 √
Q6 √
Topic:
Tree Traversal, Insertion and Deletion, BST
Objectives
1. Students will be able to learn about various operations on BST and Heap.
2. Students will be able to learn about various applications of BST and Heap.
Questions:
1. Write an algorithm to check whether the given binary tree is BST or not.
isBST(root)
{ if (root ==
NULL)
return True
if (root->left != NULL and root->left->val > root->val)
return False
if (root->right != NULL and root->right->val < root->val)
return False
2. Given a BST and a positive integer k, where k ≤ number of elements of the given BST. Write an algorithm
to find the kth-smallest element in the given BST. For example, the 4th smallest node in the following
BST is 15, and the 6th smallest is 20. The 8th smallest node does not exist.
Give an in-order traversal of the given BST. While traversing, keep track of the count of nodes visited. If
the count becomes k, print the node.
4. Given the pre-order and in-order traversals of a BST. Construct the BST from the given pre-order and
in-order traversals.
Pre-order: 12, 8, 6, 9, 16, 15, 19
In-order: 6, 8, 9, 12, 15, 16, 19
Ans:
5. Give a recursive algorithm to find the smallest element in a binary search tree.
minValueBST(root)
{ if (root == NULL)
{
print (“Empty Tree”);
}
temp = root; while (temp-
>left != NULL) temp =
temp->left; return temp-
>data;
}
[We remove the root and replace it with the last element of the heap and then restore the heap property by
heapify]