Add the initial EN translation for C++ code (#1346)

This commit is contained in:
Yudong Jin
2024-05-06 13:31:46 +08:00
committed by GitHub
parent 9e4017b3fb
commit 8e60d12151
111 changed files with 6993 additions and 9 deletions

View File

@ -0,0 +1,4 @@
add_executable(utils
common.hpp print_utils.hpp
list_node.hpp tree_node.hpp
vertex.hpp)

View File

@ -0,0 +1,28 @@
/**
* File: common.hpp
* Created Time: 2021-12-19
* Author: krahets (krahets@163.com)
*/
#pragma once
#include <algorithm>
#include <chrono>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "list_node.hpp"
#include "print_utils.hpp"
#include "tree_node.hpp"
#include "vertex.hpp"
using namespace std;

View File

@ -0,0 +1,42 @@
/**
* File: list_node.hpp
* Created Time: 2021-12-19
* Author: krahets (krahets@163.com)
*/
#pragma once
#include <iostream>
#include <vector>
using namespace std;
/* Linked list node */
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {
}
};
/* Deserialize a list into a linked list */
ListNode *vecToLinkedList(vector<int> list) {
ListNode *dum = new ListNode(0);
ListNode *head = dum;
for (int val : list) {
head->next = new ListNode(val);
head = head->next;
}
return dum->next;
}
/* Free memory allocated to the linked list */
void freeMemoryLinkedList(ListNode *cur) {
// Free memory
ListNode *pre;
while (cur != nullptr) {
pre = cur;
cur = cur->next;
delete pre;
}
}

View File

@ -0,0 +1,228 @@
/**
* File: print_utils.hpp
* Created Time: 2021-12-19
* Author: krahets (krahets@163.com), msk397 (machangxinq@gmail.com), LoneRanger(836253168@qq.com)
*/
#pragma once
#include "list_node.hpp"
#include "tree_node.hpp"
#include <climits>
#include <iostream>
#include <sstream>
#include <string>
/* Find an element in a vector */
template <typename T> int vecFind(const vector<T> &vec, T ele) {
int j = INT_MAX;
for (int i = 0; i < vec.size(); i++) {
if (vec[i] == ele) {
j = i;
}
}
return j;
}
/* Concatenate a vector with a delim */
template <typename T> string strJoin(const string &delim, const T &vec) {
ostringstream s;
for (const auto &i : vec) {
if (&i != &vec[0]) {
s << delim;
}
s << i;
}
return s.str();
}
/* Repeat a string for n times */
string strRepeat(string str, int n) {
ostringstream os;
for (int i = 0; i < n; i++)
os << str;
return os.str();
}
/* Print array */
template <typename T> void printArray(T *arr, int n) {
cout << "[";
for (int i = 0; i < n - 1; i++) {
cout << arr[i] << ", ";
}
if (n >= 1)
cout << arr[n - 1] << "]" << endl;
else
cout << "]" << endl;
}
/* Get the Vector String object */
template <typename T> string getVectorString(vector<T> &list) {
return "[" + strJoin(", ", list) + "]";
}
/* Print list */
template <typename T> void printVector(vector<T> list) {
cout << getVectorString(list) << '\n';
}
/* Print matrix */
template <typename T> void printVectorMatrix(vector<vector<T>> &matrix) {
cout << "[" << '\n';
for (vector<T> &list : matrix)
cout << " " + getVectorString(list) + "," << '\n';
cout << "]" << '\n';
}
/* Print linked list */
void printLinkedList(ListNode *head) {
vector<int> list;
while (head != nullptr) {
list.push_back(head->val);
head = head->next;
}
cout << strJoin(" -> ", list) << '\n';
}
struct Trunk {
Trunk *prev;
string str;
Trunk(Trunk *prev, string str) {
this->prev = prev;
this->str = str;
}
};
void showTrunks(Trunk *p) {
if (p == nullptr) {
return;
}
showTrunks(p->prev);
cout << p->str;
}
/**
* 打印二叉树
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
void printTree(TreeNode *root, Trunk *prev, bool isRight) {
if (root == nullptr) {
return;
}
string prev_str = " ";
Trunk trunk(prev, prev_str);
printTree(root->right, &trunk, true);
if (!prev) {
trunk.str = "———";
} else if (isRight) {
trunk.str = "/———";
prev_str = " |";
} else {
trunk.str = "\\———";
prev->str = prev_str;
}
showTrunks(&trunk);
cout << " " << root->val << endl;
if (prev) {
prev->str = prev_str;
}
trunk.str = " |";
printTree(root->left, &trunk, false);
}
/* Print binary tree */
void printTree(TreeNode *root) {
printTree(root, nullptr, false);
}
/* Print stack */
template <typename T> void printStack(stack<T> stk) {
// Reverse the input stack
stack<T> tmp;
while (!stk.empty()) {
tmp.push(stk.top());
stk.pop();
}
// Generate the string to print
ostringstream s;
bool flag = true;
while (!tmp.empty()) {
if (flag) {
s << tmp.top();
flag = false;
} else
s << ", " << tmp.top();
tmp.pop();
}
cout << "[" + s.str() + "]" << '\n';
}
/* Print queue */
template <typename T> void printQueue(queue<T> queue) {
// Generate the string to print
ostringstream s;
bool flag = true;
while (!queue.empty()) {
if (flag) {
s << queue.front();
flag = false;
} else
s << ", " << queue.front();
queue.pop();
}
cout << "[" + s.str() + "]" << '\n';
}
/* Print deque */
template <typename T> void printDeque(deque<T> deque) {
// Generate the string to print
ostringstream s;
bool flag = true;
while (!deque.empty()) {
if (flag) {
s << deque.front();
flag = false;
} else
s << ", " << deque.front();
deque.pop_front();
}
cout << "[" + s.str() + "]" << '\n';
}
/* Print hash table */
// Define template parameters TKey and TValue to specify the types of key-value pairs
template <typename TKey, typename TValue> void printHashMap(unordered_map<TKey, TValue> map) {
for (auto kv : map) {
cout << kv.first << " -> " << kv.second << '\n';
}
}
/* Expose the underlying storage of the priority_queue container */
template <typename T, typename S, typename C> S &Container(priority_queue<T, S, C> &pq) {
struct HackedQueue : private priority_queue<T, S, C> {
static S &Container(priority_queue<T, S, C> &pq) {
return pq.*&HackedQueue::c;
}
};
return HackedQueue::Container(pq);
}
/* Print heap (Priority queue) */
template <typename T, typename S, typename C> void printHeap(priority_queue<T, S, C> &heap) {
vector<T> vec = Container(heap);
cout << "Array representation of the heap:";
printVector(vec);
cout << "Tree representation of the heap:" << endl;
TreeNode *root = vectorToTree(vec);
printTree(root);
freeMemoryTree(root);
}

View File

@ -0,0 +1,84 @@
/**
* File: tree_node.hpp
* Created Time: 2021-12-19
* Author: krahets (krahets@163.com)
*/
#pragma once
#include <limits.h>
#include <vector>
using namespace std;
/* Binary tree node structure */
struct TreeNode {
int val{};
int height = 0;
TreeNode *parent{};
TreeNode *left{};
TreeNode *right{};
TreeNode() = default;
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
}
};
// For serialization encoding rules, refer to:
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
// Array representation of the binary tree:
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
// Linked list representation of the binary tree:
// /——— 15
// /——— 7
// /——— 3
// | \——— 6
// | \——— 12
// ——— 1
// \——— 2
// | /——— 9
// \——— 4
// \——— 8
/* Deserialize a list into a binary tree: Recursively */
TreeNode *vectorToTreeDFS(vector<int> &arr, int i) {
if (i < 0 || i >= arr.size() || arr[i] == INT_MAX) {
return nullptr;
}
TreeNode *root = new TreeNode(arr[i]);
root->left = vectorToTreeDFS(arr, 2 * i + 1);
root->right = vectorToTreeDFS(arr, 2 * i + 2);
return root;
}
/* Deserialize a list into a binary tree */
TreeNode *vectorToTree(vector<int> arr) {
return vectorToTreeDFS(arr, 0);
}
/* Serialize a binary tree into a list: Recursively */
void treeToVecorDFS(TreeNode *root, int i, vector<int> &res) {
if (root == nullptr)
return;
while (i >= res.size()) {
res.push_back(INT_MAX);
}
res[i] = root->val;
treeToVecorDFS(root->left, 2 * i + 1, res);
treeToVecorDFS(root->right, 2 * i + 2, res);
}
/* Serialize a binary tree into a list */
vector<int> treeToVecor(TreeNode *root) {
vector<int> res;
treeToVecorDFS(root, 0, res);
return res;
}
/* Free memory allocated to the binary tree */
void freeMemoryTree(TreeNode *root) {
if (root == nullptr)
return;
freeMemoryTree(root->left);
freeMemoryTree(root->right);
delete root;
}

View File

@ -0,0 +1,36 @@
/**
* File: vertex.hpp
* Created Time: 2023-03-02
* Author: krahets (krahets@163.com)
*/
#pragma once
#include <vector>
using namespace std;
/* Vertex class */
struct Vertex {
int val;
Vertex(int x) : val(x) {
}
};
/* Input a list of values vals, return a list of vertices vets */
vector<Vertex *> valsToVets(vector<int> vals) {
vector<Vertex *> vets;
for (int val : vals) {
vets.push_back(new Vertex(val));
}
return vets;
}
/* Input a list of vertices vets, return a list of values vals */
vector<int> vetsToVals(vector<Vertex *> vets) {
vector<int> vals;
for (Vertex *vet : vets) {
vals.push_back(vet->val);
}
return vals;
}