文章圖片存儲在GitHub
,網速不佳的朋友,請看《進擊的堆:最大索引堆》 或者 來我的技術小站 godbmw.com
1. 爲什麼需要索引堆?
堆結構的數據增刪操作,需要swap
操作。雖然可以被優化成每次一次賦值,然而當元素類型是複雜數據機構(例如:類、浮點數、結構體等),賦值操作的消耗不容小覷。
因此,如果可以通過交換整數數據,來實現堆的數據操作,就會大大提高程序性能。而索引堆就是爲此而生。
2. 堆和索引堆
友情鏈接:《堆、堆排序和優先隊列的那些事》
在堆的基礎上,增加一個整數數組indexes
。
indexes[i]
就代表:堆中第 i 個元素在所屬數組中的位置。
如下圖所示, index[1]
代表堆中的第 1 個元素是data
中的第 10 個元素。
因此,有了indexes
數組,data
數組不要變動,只需要維護indexes
數組即可表示堆中的數據順序。
3. 反向查找
當我們需要改變原來data
數組中的一個數據並且維護堆的結構,需要反向索引來幫助。
堆中引入reverse
數組。reverse[i]
代表索引 i 在indexes
數組的位置。
如下圖所示, reverse[1]
代表 1 在indexes
中的位置是 8。
藉助反向查找,當我們修改第 9 個元素的時候,訪問reverse[9]
便可以知道data[9]
在堆中的位置是 2。時間複雜度降低到O(1)
。
爲了方便理解,請看後面實現的MaxIndexHeap.h
中的void change(int i, Item new_item)
函數。
4. 代碼實現
4.1 實現最大堆
MaxIndexHeap.h
代碼如下:
//
// Created by godbmw.com on 2018/9/26.
//
#ifndef MAXHEAP_INDEXMAXHEAP_H
#define MAXHEAP_INDEXMAXHEAP_H
#include <iostream>
#include <algorithm>
#include <cassert>
#include <typeinfo>
using namespace std;
template <typename Item>
class IndexMaxHeap {
private:
Item* data; // 堆數據存放
int* indexes;
int* reverse;
int count; // 堆目前所含數據量大小
int capacity; // 堆容量大小
void shift_up(int k) {
while( k > 1 && data[indexes[k/2]] < data[indexes[k]]) {
// 交換堆中2個元素的位置, 但是不操作data數組
// 相當於交換 int 型數據,比交換Item更有效
swap(indexes[k/2], indexes[k]);
// 交換後reverse的對應值
// 更新在indexes中的位置
reverse[indexes[k/2]] = k/2;
reverse[indexes[k]] = k;
k /= 2;
}
}
void shift_down(int k) {
while( 2*k <= count ) {
int j = 2*k;
if( j+1 <= count && data[indexes[j+1]] > data[indexes[j]]) {
j+=1;
}
if(data[indexes[k]] >= data[indexes[j]]) {
break;
}
swap(indexes[k], indexes[j]);
reverse[indexes[k]] = k;
reverse[indexes[j]] = j;
k = j;
}
}
public:
IndexMaxHeap(int capacity) {
this->data = new Item[capacity + 1]; // 堆中數據從索引爲1的位置開始存儲
this->indexes = new int[capacity + 1];
this->reverse = new int[capacity + 1];
for(int i = 0; i < this->capacity + 1; ++i) {
this->reverse[i] = -1;
}
this->count = 0;
this->capacity = capacity;
}
~IndexMaxHeap(){
delete[] this->data;
delete[] this->indexes;
delete[] this->reverse;
}
// 返回堆中元素個數
int size() {
return this->count;
}
// 返回布爾值:堆中是否爲空
bool is_empty() {
return this->count == 0;
}
// 向堆中插入元素Item, i是元素索引
void insert(int i, Item item) {
assert(this->count < this->capacity);
assert( i >= 0 && i <= this->capacity);
// 對外部用戶而言, 是從索引0開始的
i += 1;
this->data[i] = item;
this->indexes[this->count + 1] = i;
this->reverse[i] = this->count + 1;
this->count++;
this->shift_up(this->count);
}
// 取出最大值
Item extract_max() {
assert(this->count > 0);
Item ret = this->data[this->indexes[1]]; // 取出根節點
swap(this->indexes[1], this->indexes[this->count]); // 將根節點元素和最後元素交換
this->reverse[this->indexes[1]] = 1;
this->reverse[this->indexes[this->count]] = -1;
this->count --; // 刪除最後一個元素
this->shift_down(1); // shift_down 將元素放到應該在的位置
return ret;
}
int extract_max_index() {
assert(this->count > 0);
int ret = this->indexes[1] - 1;
swap(this->indexes[1], this->indexes[this->count]);
this->reverse[this->indexes[1]] = 1;
this->reverse[this->indexes[this->count]] = -1;
this->count --;
this->shift_down(1);
return ret;
}
bool contain(int i) {
assert( i >= 0 && i <= this->capacity);
return reverse[i+1] != -1;
}
Item get_item(int i) {
assert(this->contain(i));
return this->data[i+1];
}
void change(int i, Item new_item) {
assert(this->contain(i));
i += 1;
this->data[i] = new_item;
// 找到 indexes[j] = i, j 表示data[i]在堆中的位置
// 之後shift_up 和 shift_down
// for(int j = 1; j <= this->count; ++j) {
// if(this->indexes[j] == i) {
// this->shift_up(j);
// this->shift_down(j);
// return;
// }
// }
// 利用 reverse 實現反向查找, 從 O(n) => O(1)
int j = this->reverse[i];
this->shift_up(j);
this->shift_down(j);
}
};
#endif //MAXHEAP_INDEXMAXHEAP_H
4.2 測試代碼
main.cpp
代碼如下:
#include <iostream>
#include <ctime>
#include <algorithm>
#include "MaxHeap.h"
#include "IndexMaxHeap.h"
#include "SortHelper.h"
#define HEAP_CAPACITY 10
#define MAX_NUM 100
using namespace std;
int main() {
IndexMaxHeap<int> index_max_heap = IndexMaxHeap<int>(HEAP_CAPACITY);
srand(time(NULL));
for(int i = 0; i < HEAP_CAPACITY - 2 ; i++) {
index_max_heap.insert(i, rand() % MAX_NUM);
}
cout<<endl;
index_max_heap.change(1, 109);
while( !index_max_heap.is_empty() ) {
cout<< index_max_heap.extract_max() << " ";
}
cout<<endl;
return 0;
}