DSA_Assignment_3A
DSA_Assignment_3A
Instructions:
4. Please read the instructions given in the questions carefully. In case of any
ambiguity, post your queries on Google Classroom at least 3 days before the
deadline. No TA will be responsible for responding to the queries
after this.
5. All the TAs will strictly follow the rubric provided to them. No requests
will be entertained related to the scoring strategy.
1. enqueue(”Alice”, 4)
2. enqueue(”Bob”, 2)
3. enqueue(”Carol”, 5)
4. enqueue(”Dave”, 1)
5. dequeue()
6. enqueue(”Eve”, 3)
7. dequeue()
2.1 After all operations, list the names in the priority queue in their current
order (as per the priority). (7.5 marks)
2.2 Explain how the underlying data structure (like a binary heap) ensures
correct order in O(log n) time for both insert and delete. (7.5 marks)
3. Search Tree
The Legacy of the Tree Kingdom (20 Marks)
In the ancient Kingdom of Arboris, a mystical tree stood at the center of
the realm — the Tree of Fortune. Each branch bore a golden number, repre-
senting the wealth of a noble house in the kingdom. The tree was structured
in a very particular way: every noble house on the left of a branch held
less wealth, and those on the right held more — a clear hierarchy rooted in
centuries of tradition.
But times were changing. The wise queen of Arboris declared a new law:
from now on, each house’s wealth must reflect not only its own fortune but
also the accumulated wealth of all mightier houses.
In simpler terms, every house was to inherit the collective riches of all
houses more powerful than itself as a symbol of unity and shared prosperity.
The royal scholars were tasked with updating the Tree of Fortune accord-
ingly. The challenge? Traverse the tree in just the right order to redistribute
the wealth — no house left behind and no riches miscounted.
You are given the root of the Tree of Fortune, which is a binary tree
representing a Binary Search Tree (BST). Your task is to transform this tree
into a Prosperity Tree, where each node’s value becomes the original value
plus the sum of all values strictly greater than it in the tree.
Return the updated root of the tree after completing the transformation.
Example Input: 5 2 13
Example Output: 18 20 13
4. Heap
The Skyline Watchtower Mission (20 Marks)
In the towering city of Skyreach, each building serves as a watchtower to keep
an eye on the horizon. These towers are lined up along the main boulevard,
and each one has a specific height.
To improve surveillance efficiency, the city’s engineers have developed an
advanced hover drone that patrols over the city. The drone can monitor
exactly k towers at a time, capturing intelligence from the tallest tower in its
current view.
As the drone glides from left to right across the skyline, one tower at a
time, you are tasked with reporting the height of the tallest tower visible to
the drone at each of its positions.
Given an array of heights[], where each element represents the height of a
watchtower, and an integer k represents the number of towers the drone ob-
serves at once, return an array of integers where each element is the maximum
height the drone sees in that window as it moves across the city.
Example:
Input: heights = [1, 3, 2, 4, 15, 3, 10, 7] k = 3
Output: [3, 4, 15, 15, 15, 10]