Format C++ codes in Clang-Format Style: Microsoft

This commit is contained in:
krahets
2023-04-14 03:44:02 +08:00
parent f8513455b5
commit 9c9c8b7574
46 changed files with 732 additions and 888 deletions

View File

@@ -6,23 +6,20 @@
#include "../include/include.hpp"
void testPush(priority_queue<int> &heap, int val)
{
void testPush(priority_queue<int> &heap, int val) {
heap.push(val); // 元素入堆
cout << "\n元素 " << val << " 入堆后" << endl;
PrintUtil::printHeap(heap);
printHeap(heap);
}
void testPop(priority_queue<int> &heap)
{
void testPop(priority_queue<int> &heap) {
int val = heap.top();
heap.pop();
cout << "\n堆顶元素 " << val << " 出堆后" << endl;
PrintUtil::printHeap(heap);
printHeap(heap);
}
int main()
{
int main() {
/* 初始化堆 */
// 初始化小顶堆
// priority_queue<int, vector<int>, greater<int>> minHeap;
@@ -62,6 +59,6 @@ int main()
vector<int> input{1, 3, 2, 5, 4};
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
cout << "输入列表并建立小顶堆后" << endl;
PrintUtil::printHeap(minHeap);
printHeap(minHeap);
return 0;
}

View File

@@ -8,7 +8,7 @@
/* 大顶堆 */
class MaxHeap {
private:
private:
// 使用动态数组,这样无需考虑扩容问题
vector<int> maxHeap;
@@ -20,7 +20,7 @@ private:
/* 获取右子节点索引 */
int right(int i) {
return 2 * i + 2;
}
}
/* 获取父节点索引 */
int parent(int i) {
@@ -31,7 +31,7 @@ private:
void siftUp(int i) {
while (true) {
// 获取节点 i 的父节点
int p = parent(i);
int p = parent(i);
// 当“越过根节点”或“节点无需修复”时,结束堆化
if (p < 0 || maxHeap[i] <= maxHeap[p])
break;
@@ -48,12 +48,12 @@ private:
// 判断节点 i, l, r 中值最大的节点,记为 ma
int l = left(i), r = right(i), ma = i;
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
if (l < size() && maxHeap[l] > maxHeap[ma])
if (l < size() && maxHeap[l] > maxHeap[ma])
ma = l;
if (r < size() && maxHeap[r] > maxHeap[ma])
ma = r;
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
if (ma == i)
if (ma == i)
break;
swap(maxHeap[i], maxHeap[ma]);
// 循环向下堆化
@@ -61,7 +61,7 @@ private:
}
}
public:
public:
/* 构造方法,根据输入列表建堆 */
MaxHeap(vector<int> nums) {
// 将列表元素原封不动添加进堆
@@ -112,15 +112,14 @@ public:
/* 打印堆(二叉树)*/
void print() {
cout << "堆的数组表示:";
PrintUtil::printVector(maxHeap);
printVector(maxHeap);
cout << "堆的树状表示:" << endl;
TreeNode *root = vecToTree(maxHeap);
PrintUtil::printTree(root);
printTree(root);
freeMemoryTree(root);
}
};
/* Driver Code */
int main() {
/* 初始化大顶堆 */