DSA-CSF23 LAB10
DSA-CSF23 LAB10
LAB-10
Total Marks: 50
Objective:
In this lab, students will practice about the working of Binary Trees.
Instructions:
1)Follow the question instructions very carefully, no changes in function prototypes are allowed.
2)Your laptops must be on airplane mode
3)Anyone caught in an act of plagiarism would be awarded an “F” grade in this Lab.
You are given a sorted list of words. Your task is to build a Binary Search Tree (BST) from these
words, and implement an auto-complete feature. In this feature, when a user enters a prefix, the
system should return all words stored in the BST that start with that prefix.
Constraints
Implement:
● A function to convert list of words to a BST
Node * wordsToBST(const vector<string>& words)
● A function to return all words with a given prefix from that BST.
vector<string> autoComplete(Node* root, const string& prefix)
Input:
● A list of all words from the BST that start with the given prefix, in any order.
Example 1:
Input:
prefix = "ba"
Output :
Example 2:
Input:
prefix = "app"
Output :
["apple", "appetizer"]
TASK-02: News Feed Aggregator (K-th Most Viewed Post) [15 Marks]
You are building a news feed aggregator system. Each post is represented as a node in a
Binary Search Tree (BST) where:
Your task is to write a function that returns the k-th most viewed post in the BST.
Here, 1st most viewed means the post with the highest number of views, 2nd most viewed
means second-highest, and so on.
Input
● The root of a BST, where each node contains a unique integer representing the view
count.
● An integer k.
● Prototype: int kthMostViewedPost(Node* root, int k)
Output
50
/\
30 70
/ \ / \
20 40 60 80
Example1:
Input: k=5
Output : 40
Example2:
Input: k=2
Output : 70
You are given the root of a binary tree and an integer targetSum.
Your task is to determine whether there exists at least one root-to-leaf path in the tree such
that the sum of all the node values along the path is equal to targetSum.A leaf is a node with
no children.
Input
Output
Tree Structure:
/ \
4 8
/ /\
11 13 4
/\
Explanation: