i have written a binary heap that stores priority class(private members are PRIORITY and KEY). i wonder how i can change the priority of a element of heap in less than O(n) complexity when KEY of variable is given. thanks in advance

- 69,226
- 18
- 123
- 176

- 13
- 3
-
@2501 the approach may be similar but the problem is different. I do not think this is a duplicate of the question you link to – Ivaylo Strandjev Jan 07 '15 at 14:21
1 Answers
One way to solve this problem is have another structure(I'll call that auxiliary data structure). For instance a hash map(e.g. unordered_map) that maps keys to elements in the binary heap. You use this datastructure to find which element in the heap needs to have its priority modified. After you modify the priority of the element, you need to perform one sink
and one sift
operation with it. Both operations have complexity O(log(n))
thus overall the modification of priority has complexity O(log(n))
. Here I assume you use an auxiliary data structure that supports find by key in complexity no more than O(log(n))
(in case of hash map the complexity is O(1)).
Note you will have to modify the operations you've already implemented for the binary heap to keep the auxiliary data structure in sync with the binary heap.

- 69,226
- 18
- 123
- 176
-
i also thought of that. is there any other data structures i can use to solve this problem?yes i already use hash table to find by key so complexity is O(1).but im afraid i cant use another hast table to store the indexes of priorities.what other data structure can i use to solve this problem? thanks btw – user3482695 Jan 07 '15 at 16:48