Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (10 points) Let T be a heap storing n keys. Give the pseudocode for an effici

ID: 3747404 • Letter: 2

Question

2. (10 points) Let T be a heap storing n keys. Give the pseudocode for an efficient algorithm for printing all the keys in T that are smaller than or equal to a given query key a (which is not necessarily in T). You can assume the existence of a O(1)-time print(key) function. For example, given the heap of Figure 2.41 and query key 7, the algorithm should report 4,5,6,7. Note that the keys do not need to be reported in sorted order. Your algorithm should run in O(k time, where k is the number of keys rrpuriel

Explanation / Answer

The double heap data structures is an exhibit that can be seen as a total parallel tree. Every hub of the parallel tree relates to a component of the exhibit. The exhibit is totally filled on all levels with the exception of perhaps most reduced.

We speak to piles in level request, going from left to right. The exhibit comparing to the heap above is [25, 13, 17, 5, 8, 3].

The base of the tree A[1] and given record I of a hub, the lists of its parent, left tyke and right kid can be processed

PARENT (I)

return floor(i/2)

LEFT (I)

return 2i

RIGHT (I)

return 2i + 1

How about we attempt these out on a pile to ensure we trust they are right. Take this pile,

which is spoken to by the cluster [20, 14, 17, 8, 6, 9, 4, 1].

We'll go from the 20 to the 6 first. The record of the 20 is 1. To discover the record of the left youngster, we figure 1 * 2 = 2. This takes us (accurately) to the 14. Presently, we go right, so we ascertain 2 * 2 + 1 = 5. This takes us (once more, accurately) to the 6.

Presently we should take a stab at going from the 4 to the 20. 4's record is 7. We need to go to the parent, so we figure 7/2 = 3, which takes us to the 17. Presently, to get 17's parent, we compute 3/2 = 1, which takes us to the 20.

Store Property

In a store, for each hub I other than the root, the estimation of a hub is more noteworthy than or break even with (at most) to the estimation of its parent.

A[PARENT (i)] A[i]

In this manner, the biggest component in a load is put away at the root.

Following is a case of Heap:

By the meaning of a heap, all the tree levels are totally filled aside from potentially for the most minimal level, which is topped from the left off to a point. Unmistakably a store of stature h has the base number of components when it has only one hub at the most reduced level. The levels over the most reduced level frame a total parallel tree of tallness h - 1 and 2h - 1 hubs. Henceforth the base number of hubs conceivable in a pile of stature h is 2h. Plainly a load of tallness h, has the greatest number of components when its most minimal level is totally filled. For this situation the pile is an entire twofold tree of tallness h and consequently has 2h+1 - 1 hubs.

Following isn't a pile, since it just has the load property - it's anything but a total paired tree. Review that to be finished, a paired tree needs to top off the majority of its levels with the conceivable special case of the last one, which must be filled in from the left side.

Stature of a hub

We characterize the stature of a hub in a tree to be various edges on the longest basic descending way from a hub to a leaf.

Stature of a tree

The quantity of edges on a basic descending way from a root to a leaf. Note that the tallness of a tree with n hub is lg n which is (lgn). This suggests a n-component heap has stature lg n

Keeping in mind the end goal to demonstrate this let the stature of the n-component load be h. From the limits acquired on greatest and least number of components in a load, we get

2h n 2h+1-1

Where n is the quantity of components in a heap.

2h n 2h+1

Taking logarithms to the base 2

h lgn h +1

It takes after that h = lgn.

We known from over that biggest component dwells in root, A[1]. The common thing to ask is the place in a pile may the littlest component lives? Think about any way from foundation of the tree to a leaf. In light of the store property, as we take after that way, the components are either diminishing or remaining the same. On the off chance that it happens to be the situation that all components in the heap are particular, at that point the above suggests that the littlest is in a leaf of the tree. It could likewise be that a whole subtree of the pile is the littlest component or to be sure that there is just a single component in the store, which in the littlest component, so the littlest component is all over. Note that anything beneath the littlest component must equivalent the littlest component, so all in all, just whole subtrees of the load can contain the littlest component.

Embeddings Element in the Heap

Assume we have a load as takes after

How about we guess we need to include a hub with key 15 to the pile. Initially, we add the hub to the tree at the following spot accessible at the most reduced level of the tree. This is to guarantee that the tree stays finish.

We should assume we need to include a hub with key 15 to the load. To start with, we add the hub to the tree at the following spot accessible at the most minimal level of the tree. This is to guarantee that the tree stays finish.

Presently we do a similar thing once more, contrasting the new hub with its parent. Since 14 < 15, we need to do another swap:

Presently we are done, in light of the fact that 15 20.

Four essential strategies on store are

Heapify, which keeps running in O(lg n) time.

Manufacture Heap, which keeps running in straight time.

Stack Sort, which keeps running in O(n lg n) time.

Concentrate Max, which keeps running in O(lg n) time.

Keeping up the Heap Property

Heapify is a technique for controlling load data structures. It is given a cluster An and list I into the exhibit. The subtree established at the offspring of A[i] are store however hub A[i] itself may conceivably damage the load property i.e., A[i] < A[2i] or A[i] < A[2i +1]. The methodology 'Heapify' controls the tree established at A[i] so it turns into a heap. At the end of the day, 'Heapify' is given the incentive at A[i] "a chance to glide down" in a heap with the goal that subtree established at file I turns into a pile.

Framework of Procedure Heapify

Heapify picks the biggest tyke key and contrast it with the parent key. In the event that parent key is bigger than heapify stops, else it swaps the parent key with the biggest youngster key. With the goal that the parent is presently winds up bigger than its youngsters.

Note that swap may wreck the heap property of the subtree established at the biggest tyke hub. If so, Heapify calls itself again utilizing biggest youngster hub as the new root.

Heapify (An, I)

l left [i]

r right [i]

in the event that l pile measure [A] and A[l] > A[i]

at that point biggest l

else biggest I

in the event that r pile measure [A] and A[i] > A[largest]

at that point biggest r

in the event that biggest I

at that point trade A[i] A[largest]

Heapify (A, biggest)

Examination

In the event that we put an incentive at root that is not as much as each incentive in the left and right subtree, at that point 'Heapify' will be called recursively until the point when leaf is come to. To make recursive considers navigate the longest way to a leaf, pick esteem that make 'Heapify' dependably recurse on the left tyke. It takes after the left branch when left tyke is more noteworthy than or equivalent to the correct tyke, so putting 0 at the root and 1 at all different hubs, for instance, will achieved this undertaking. With such qualities 'Heapify' will called h times, where h is the pile stature so its running time will be (h) (since each call does (1) work), which is (lgn). Since we have a case in which Heapify's running time (lg n), its most pessimistic scenario running time is (lgn).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote