156 lines
3.5 KiB
C++
156 lines
3.5 KiB
C++
|
/**
|
|||
|
* File: my_heap.cpp
|
|||
|
* Created Time: 2023-02-04
|
|||
|
* Author: LoneRanger (836253168@qq.com), what-is-me (whatisme@outlook.jp)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><EFBFBD> */
|
|||
|
class MaxHeap {
|
|||
|
private:
|
|||
|
// ʹ<>ö<EFBFBD>̬<EFBFBD><CCAC><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뿼<EFBFBD><EBBFBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
vector<int> maxHeap;
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
int left(int i) {
|
|||
|
return 2 * i + 1;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
int right(int i) {
|
|||
|
return 2 * i + 2;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
int parent(int i) {
|
|||
|
return (i - 1) / 2; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ӽڵ<D3BD> i <20><>ʼ<EFBFBD><CABC><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѻ<EFBFBD> */
|
|||
|
void siftUp(int i) {
|
|||
|
while (true) {
|
|||
|
// <20><>ȡ<EFBFBD>ڵ<EFBFBD> i <20>ĸ<EFBFBD><C4B8>ڵ<EFBFBD>
|
|||
|
int p = parent(i);
|
|||
|
// <20><><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD>ڵ㡱<DAB5>ڵ<F2A1B0BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB8><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѻ<EFBFBD>
|
|||
|
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
|||
|
break;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>
|
|||
|
swap(maxHeap[i], maxHeap[p]);
|
|||
|
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD>϶ѻ<CFB6>
|
|||
|
i = p;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ӽڵ<D3BD> i <20><>ʼ<EFBFBD><CABC><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD>ѻ<D7B6> */
|
|||
|
void siftDown(int i) {
|
|||
|
while (true) {
|
|||
|
// <20>жϽڵ<CFBD> i, l, r <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>Ľڵ㣬<DAB5><E3A3AC>Ϊ ma
|
|||
|
int l = left(i), r = right(i), ma = i;
|
|||
|
if (l < size() && maxHeap[l] > maxHeap[ma])
|
|||
|
ma = l;
|
|||
|
if (r < size() && maxHeap[r] > maxHeap[ma])
|
|||
|
ma = r;
|
|||
|
// <20><><EFBFBD>ڵ<EFBFBD> i <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> l, r Խ<>磬<EFBFBD><E7A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѻ<EFBFBD><D1BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (ma == i)
|
|||
|
break;
|
|||
|
swap(maxHeap[i], maxHeap[ma]);
|
|||
|
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD>¶ѻ<C2B6>
|
|||
|
i = ma;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public:
|
|||
|
/* <20><><EFBFBD>췽<EFBFBD><ECB7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
MaxHeap(vector<int> nums) {
|
|||
|
// <20><><EFBFBD>б<EFBFBD>Ԫ<EFBFBD><D4AA>ԭ<EFBFBD>ⲻ<EFBFBD><E2B2BB><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>
|
|||
|
maxHeap = nums;
|
|||
|
// <20>ѻ<EFBFBD><D1BB><EFBFBD>Ҷ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>нڵ<D0BD>
|
|||
|
for (int i = parent(size() - 1); i >= 0; i--) {
|
|||
|
siftDown(i);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD>Ѵ<EFBFBD>С */
|
|||
|
int size() {
|
|||
|
return maxHeap.size();
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ж϶<D0B6><CFB6>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool isEmpty() {
|
|||
|
return size() == 0;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ʶѶ<CAB6>Ԫ<EFBFBD><D4AA> */
|
|||
|
int peek() {
|
|||
|
return maxHeap[0];
|
|||
|
}
|
|||
|
|
|||
|
/* Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void push(int val) {
|
|||
|
// <20><><EFBFBD>ӽڵ<D3BD>
|
|||
|
maxHeap.push_back(val);
|
|||
|
// <20>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѻ<EFBFBD>
|
|||
|
siftUp(size() - 1);
|
|||
|
}
|
|||
|
|
|||
|
/* Ԫ<>س<EFBFBD><D8B3><EFBFBD> */
|
|||
|
void pop() {
|
|||
|
// <20>пմ<D0BF><D5B4><EFBFBD>
|
|||
|
if (isEmpty()) {
|
|||
|
throw out_of_range("<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҷ<EFBFBD>ڵ㣨<DAB5><E3A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>βԪ<CEB2>أ<EFBFBD>
|
|||
|
swap(maxHeap[0], maxHeap[size() - 1]);
|
|||
|
// ɾ<><C9BE><EFBFBD>ڵ<EFBFBD>
|
|||
|
maxHeap.pop_back();
|
|||
|
// <20>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD>ѻ<D7B6>
|
|||
|
siftDown(0);
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ӡ<EFBFBD>ѣ<EFBFBD><D1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
|||
|
void print() {
|
|||
|
cout << "<EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>";
|
|||
|
printVector(maxHeap);
|
|||
|
cout << "<EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD>״<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>" << endl;
|
|||
|
TreeNode *root = vectorToTree(maxHeap);
|
|||
|
printTree(root);
|
|||
|
freeMemoryTree(root);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD> */
|
|||
|
vector<int> vec{9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2};
|
|||
|
MaxHeap maxHeap(vec);
|
|||
|
cout << "\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѻ<EFBFBD>" << endl;
|
|||
|
maxHeap.print();
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD><D4AA> */
|
|||
|
int peek = maxHeap.peek();
|
|||
|
cout << "\n<EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>Ϊ " << peek << endl;
|
|||
|
|
|||
|
/* Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
int val = 7;
|
|||
|
maxHeap.push(val);
|
|||
|
cout << "\nԪ<EFBFBD><EFBFBD> " << val << " <20><><EFBFBD>Ѻ<EFBFBD>" << endl;
|
|||
|
maxHeap.print();
|
|||
|
|
|||
|
/* <20>Ѷ<EFBFBD>Ԫ<EFBFBD>س<EFBFBD><D8B3><EFBFBD> */
|
|||
|
peek = maxHeap.peek();
|
|||
|
maxHeap.pop();
|
|||
|
cout << "\n<EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD><EFBFBD> " << peek << " <20><><EFBFBD>Ѻ<EFBFBD>" << endl;
|
|||
|
maxHeap.print();
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD>Ѵ<EFBFBD>С */
|
|||
|
int size = maxHeap.size();
|
|||
|
cout << "\n<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << size << endl;
|
|||
|
|
|||
|
/* <20>ж϶<D0B6><CFB6>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool isEmpty = maxHeap.isEmpty();
|
|||
|
cout << "\n<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> " << isEmpty << endl;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|