mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Add the initial EN translation for C++ code (#1346)
This commit is contained in:
3
en/codes/cpp/chapter_heap/CMakeLists.txt
Normal file
3
en/codes/cpp/chapter_heap/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
||||
add_executable(heap heap.cpp)
|
||||
add_executable(my_heap my_heap.cpp)
|
||||
add_executable(top_k top_k.cpp)
|
||||
66
en/codes/cpp/chapter_heap/heap.cpp
Normal file
66
en/codes/cpp/chapter_heap/heap.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: heap.cpp
|
||||
* Created Time: 2023-01-19
|
||||
* Author: LoneRanger(836253168@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
void testPush(priority_queue<int> &heap, int val) {
|
||||
heap.push(val); // Push the element into heap
|
||||
cout << "\nAfter element " << val << " is added to the heap" << endl;
|
||||
printHeap(heap);
|
||||
}
|
||||
|
||||
void testPop(priority_queue<int> &heap) {
|
||||
int val = heap.top();
|
||||
heap.pop();
|
||||
cout << "\nAfter the top element " << val << " is removed from the heap" << endl;
|
||||
printHeap(heap);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize the heap */
|
||||
// Initialize min-heap
|
||||
// priority_queue<int, vector<int>, greater<int>> minHeap;
|
||||
// Initialize max-heap
|
||||
priority_queue<int, vector<int>, less<int>> maxHeap;
|
||||
|
||||
cout << "\nThe following test case is for max-heap" << endl;
|
||||
|
||||
/* Push the element into heap */
|
||||
testPush(maxHeap, 1);
|
||||
testPush(maxHeap, 3);
|
||||
testPush(maxHeap, 2);
|
||||
testPush(maxHeap, 5);
|
||||
testPush(maxHeap, 4);
|
||||
|
||||
/* Access heap top element */
|
||||
int peek = maxHeap.top();
|
||||
cout << "\nTop element of the heap is " << peek << endl;
|
||||
|
||||
/* Pop the element at the heap top */
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.size();
|
||||
cout << "\nNumber of elements in the heap is " << size << endl;
|
||||
|
||||
/* Determine if heap is empty */
|
||||
bool isEmpty = maxHeap.empty();
|
||||
cout << "\nIs the heap empty " << isEmpty << endl;
|
||||
|
||||
/* Enter list and build heap */
|
||||
// Time complexity is O(n), not O(nlogn)
|
||||
vector<int> input{1, 3, 2, 5, 4};
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
||||
cout << "After inputting the list and building a min-heap" << endl;
|
||||
printHeap(minHeap);
|
||||
|
||||
return 0;
|
||||
}
|
||||
155
en/codes/cpp/chapter_heap/my_heap.cpp
Normal file
155
en/codes/cpp/chapter_heap/my_heap.cpp
Normal file
@ -0,0 +1,155 @@
|
||||
/**
|
||||
* File: my_heap.cpp
|
||||
* Created Time: 2023-02-04
|
||||
* Author: LoneRanger (836253168@qq.com), what-is-me (whatisme@outlook.jp)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Max-heap */
|
||||
class MaxHeap {
|
||||
private:
|
||||
// Using a dynamic array to avoid the need for resizing
|
||||
vector<int> maxHeap;
|
||||
|
||||
/* Get index of left child node */
|
||||
int left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get index of right child node */
|
||||
int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get index of parent node */
|
||||
int parent(int i) {
|
||||
return (i - 1) / 2; // Integer division down
|
||||
}
|
||||
|
||||
/* Start heapifying node i, from bottom to top */
|
||||
void siftUp(int i) {
|
||||
while (true) {
|
||||
// Get parent node of node i
|
||||
int p = parent(i);
|
||||
// When "crossing the root node" or "node does not need repair", end heapification
|
||||
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
||||
break;
|
||||
// Swap two nodes
|
||||
swap(maxHeap[i], maxHeap[p]);
|
||||
// Loop upwards heapification
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Start heapifying node i, from top to bottom */
|
||||
void siftDown(int i) {
|
||||
while (true) {
|
||||
// Determine the largest node among i, l, r, noted as 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;
|
||||
// If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
|
||||
if (ma == i)
|
||||
break;
|
||||
swap(maxHeap[i], maxHeap[ma]);
|
||||
// Loop downwards heapification
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
/* Constructor, build heap based on input list */
|
||||
MaxHeap(vector<int> nums) {
|
||||
// Add all list elements into the heap
|
||||
maxHeap = nums;
|
||||
// Heapify all nodes except leaves
|
||||
for (int i = parent(size() - 1); i >= 0; i--) {
|
||||
siftDown(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get heap size */
|
||||
int size() {
|
||||
return maxHeap.size();
|
||||
}
|
||||
|
||||
/* Determine if heap is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Access heap top element */
|
||||
int peek() {
|
||||
return maxHeap[0];
|
||||
}
|
||||
|
||||
/* Push the element into heap */
|
||||
void push(int val) {
|
||||
// Add node
|
||||
maxHeap.push_back(val);
|
||||
// Heapify from bottom to top
|
||||
siftUp(size() - 1);
|
||||
}
|
||||
|
||||
/* Element exits heap */
|
||||
void pop() {
|
||||
// Empty handling
|
||||
if (isEmpty()) {
|
||||
throw out_of_range("Heap is empty");
|
||||
}
|
||||
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
swap(maxHeap[0], maxHeap[size() - 1]);
|
||||
// Remove node
|
||||
maxHeap.pop_back();
|
||||
// Heapify from top to bottom
|
||||
siftDown(0);
|
||||
}
|
||||
|
||||
/* Print heap (binary tree)*/
|
||||
void print() {
|
||||
cout << "Array representation of the heap:";
|
||||
printVector(maxHeap);
|
||||
cout << "Tree representation of the heap:" << endl;
|
||||
TreeNode *root = vectorToTree(maxHeap);
|
||||
printTree(root);
|
||||
freeMemoryTree(root);
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize max-heap */
|
||||
vector<int> vec{9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2};
|
||||
MaxHeap maxHeap(vec);
|
||||
cout << "\nEnter list and build heap" << endl;
|
||||
maxHeap.print();
|
||||
|
||||
/* Access heap top element */
|
||||
int peek = maxHeap.peek();
|
||||
cout << "\nTop element of the heap is " << peek << endl;
|
||||
|
||||
/* Push the element into heap */
|
||||
int val = 7;
|
||||
maxHeap.push(val);
|
||||
cout << "\nAfter element " << val << " is added to the heap" << endl;
|
||||
maxHeap.print();
|
||||
|
||||
/* Pop the element at the heap top */
|
||||
peek = maxHeap.peek();
|
||||
maxHeap.pop();
|
||||
cout << "\nAfter the top element " << peek << " is removed from the heap" << endl;
|
||||
maxHeap.print();
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.size();
|
||||
cout << "\nNumber of elements in the heap is " << size << endl;
|
||||
|
||||
/* Determine if heap is empty */
|
||||
bool isEmpty = maxHeap.isEmpty();
|
||||
cout << "\nIs the heap empty " << isEmpty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
38
en/codes/cpp/chapter_heap/top_k.cpp
Normal file
38
en/codes/cpp/chapter_heap/top_k.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* File: top_k.cpp
|
||||
* Created Time: 2023-06-12
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Using heap to find the largest k elements in an array */
|
||||
priority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {
|
||||
// Initialize min-heap
|
||||
priority_queue<int, vector<int>, greater<int>> heap;
|
||||
// Enter the first k elements of the array into the heap
|
||||
for (int i = 0; i < k; i++) {
|
||||
heap.push(nums[i]);
|
||||
}
|
||||
// From the k+1th element, keep the heap length as k
|
||||
for (int i = k; i < nums.size(); i++) {
|
||||
// If the current element is larger than the heap top element, remove the heap top element and enter the current element into the heap
|
||||
if (nums[i] > heap.top()) {
|
||||
heap.pop();
|
||||
heap.push(nums[i]);
|
||||
}
|
||||
}
|
||||
return heap;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
int main() {
|
||||
vector<int> nums = {1, 7, 6, 3, 2};
|
||||
int k = 3;
|
||||
|
||||
priority_queue<int, vector<int>, greater<int>> res = topKHeap(nums, k);
|
||||
cout << "The largest " << k << " elements are:";
|
||||
printHeap(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user