Index Heap and indexheap
First, let's look at a normal heap built by a common array.
Then we use the previous method to heapify it and build it into the largest heap.
The result is as follows:
For the array we are concerned about, the element position in the array has changed. It is because the positions of these elements have changed that we can build them into the largest heap.
However, due to the change in the element position in the array, we will face several limitations.
1. If our elements are very complex, for example, we store a 0.1 million-word article on each location. Switching the positions between them will produce a lot of time consumption. (However, this can be solved through technical means)
2. Because the position of our array elements has changed after building a pile, it is difficult for us to index it and change it. For example, it will be very difficult for us to change the priority (value) of an original element after building a pile.
We may add an attribute to each element to indicate that the original position can be solved, but in this case, we must traverse this array to solve the problem. (Inefficient Performance)
To solve the above problems, we need to introduce the Index Heap concept.
For the index heap, we store data and indexes separately. The array that represents the heap is constructed from the index array. (As in the middle, the index number is written at each node)
When building the heap (taking the largest index heap as an example), compare the value in data (that is, the value of the corresponding index in the original array)
After the build, the data domain does not change, and the location changes to the index domain.
So how can we interpret this biggest heap?
For example, if the top element of the heap is Index = 10, it indicates the value of the data field with the Index of 10, that is, 62.
At this time, we can see that the heap construction process is a simple exchange between indexes, and the index is a simple int type. High efficiency.
Now, if we want to make some changes to this array, for example, we want to change the element value of index 7 to 100, what we need to do is to change the data field 28 corresponding to index 7 to 100. The time complexity is O (1 ).
Of course, after the change, we still need to perform some operations to maintain the nature of the largest heap. However, the adjustment process changes the content of the index field.