voidsiftUp(std::vector<T> &data, int len, int cur){ //插入元素,需要上浮 while (cur > 0) { int parent = getParent(cur); if (data[cur] <= data[parent]) break; std::swap(data[cur],data[parent]); cur = parent; } }
private: voidsiftDown(std::vector<T> &data, int len, int cur){ while (true) { int best_index = cur; int left_index = getLeft(cur); int right_index = getRight(cur);
if (left_index < len && comp(data[best_index], data[left_index])) { best_index = left_index; } if (right_index < len && comp(data[best_index], data[right_index])) { best_index = right_index; }
if (best_index != cur) { std::swap(data[best_index], data[cur]); cur = best_index; } else { break; } } }
voidsiftUp(std::vector<T> &data, int len, int cur){ while (cur > 0) { int parent_index = getParent(cur); if (comp(data[parent_index], data[cur])) { std::swap(data[parent_index], data[cur]); cur = parent_index; } else { break; } } }
[[nodiscard]] intgetLeft(int index)const{ return2 * index + 1; }
[[nodiscard]] intgetRight(int index)const{ return2 * index + 2; }