0% found this document useful (0 votes)
7 views

Tutorial-9_2 _Solution

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Tutorial-9_2 _Solution

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Course- B.Tech.

Type- Core
Course Code- CSET202 Course Name- Data Structure using C++

Year- 2024 Semester- Odd


Date- 24/9/2024 Batch- 2022-2026

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.

Tutorial 10 – Binary Search Tree, 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

if (isBST(root->left != NULL) and isBST(root->right != NULL))


return True 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.

3. Consider the following BST.

Perform following traversals for the given BST.


(a) Pre-order traversal
(b) In-order traversal
(c) post-order traversal

Pre-order: 7, 4, 2, 3, 6, 5, 12, 9, 8, 11, 19, 15, 20


In-order: 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 19, 20
Post-order: 3, 2, 5, 6, 4, 8, 11, 9, 15, 20, 19, 12, 7

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

6. Construct a min binary heap with the following sequence of numbers:


70, 42, 35, 8, 12, 15, 23, 6, 90 (a)
Show the heap at each stage of construction.
(b) Remove the root node from the min binary heap that you have constructed and show the heap after
deletion. (a) Final heap
(b)

[We remove the root and replace it with the last element of the heap and then restore the heap property by
heapify]

You might also like