mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
add avl tree and heap part cpp code (#320)
* 将avl_tree翻译成c++代码(文档明天补) * markdown翻译了 * avl_tree.cpp翻译了 * 堆的cpp翻译 * modify the code format * Update heap.md --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -287,7 +287,23 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="my_heap.cpp"
|
||||
// 使用 vector 而非数组,这样无需考虑扩容问题
|
||||
vector<int> maxHeap;
|
||||
|
||||
/* 获取左子结点索引 */
|
||||
int left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* 获取右子结点索引 */
|
||||
int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 获取父结点索引 */
|
||||
int parent(int i) {
|
||||
return (i - 1) / 2; // 向下整除
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -400,7 +416,10 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="my_heap.cpp"
|
||||
|
||||
/* 访问堆顶元素 */
|
||||
int peek() {
|
||||
return maxHeap.front();
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -513,7 +532,28 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="my_heap.cpp"
|
||||
/* 元素入堆 */
|
||||
void push(int val) {
|
||||
// 添加结点
|
||||
maxHeap.push_back(val);
|
||||
// 从底至顶堆化
|
||||
siftUp(size() - 1);
|
||||
}
|
||||
|
||||
/* 从结点 i 开始,从底至顶堆化 */
|
||||
void siftUp(int i) {
|
||||
while (true) {
|
||||
// 获取结点 i 的父结点
|
||||
int p = parent(i);
|
||||
// 当“越过根结点”或“结点无需修复”时,结束堆化
|
||||
if (p < 0 || maxHeap[i] <= maxHeap[i])
|
||||
break;
|
||||
// 交换两结点
|
||||
swap(i, p);
|
||||
// 循环向上堆化
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -691,7 +731,39 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="my_heap.cpp"
|
||||
/* 元素出堆 */
|
||||
int poll() {
|
||||
// 判空处理
|
||||
if (isEmpty())
|
||||
throw out_of_range("堆已空\n");
|
||||
// 交换根结点与最右叶结点(即交换首元素与尾元素)
|
||||
swap(0, size() - 1);
|
||||
// 删除结点
|
||||
int val = maxHeap.back();
|
||||
maxHeap.pop_back();
|
||||
// 从顶至底堆化
|
||||
siftDown(0);
|
||||
// 返回堆顶元素
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 从结点 i 开始,从顶至底堆化 */
|
||||
void siftDown(int i) {
|
||||
while (true) {
|
||||
// 判断结点 i, l, r 中值最大的结点,记为 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;
|
||||
// 若结点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if (ma == i) break;
|
||||
// 交换两结点
|
||||
swap(i, ma);
|
||||
// 循环向下堆化
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -843,7 +915,16 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="my_heap.cpp"
|
||||
|
||||
/* 构造函数,根据输入列表建堆 */
|
||||
MaxHeap(vector<int> nums) {
|
||||
// 将列表元素原封不动添加进堆
|
||||
maxHeap = nums;
|
||||
// 堆化除叶结点以外的其他所有结点
|
||||
for (int i = parent(size() - 1); i >= 0; i--) {
|
||||
siftDown(i);
|
||||
}
|
||||
}
|
||||
// Tip: std::make_heap() 函数可以原地建堆。
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
Reference in New Issue
Block a user