mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 01:36:29 +08:00
5f7385c8a3
* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
228 lines
5.3 KiB
C++
228 lines
5.3 KiB
C++
/**
|
|
* 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();
|
|
}
|
|
|
|
/* 列印陣列 */
|
|
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) + "]";
|
|
}
|
|
|
|
/* 列印串列 */
|
|
template <typename T> void printVector(vector<T> list) {
|
|
cout << getVectorString(list) << '\n';
|
|
}
|
|
|
|
/* 列印矩陣 */
|
|
template <typename T> void printVectorMatrix(vector<vector<T>> &matrix) {
|
|
cout << "[" << '\n';
|
|
for (vector<T> &list : matrix)
|
|
cout << " " + getVectorString(list) + "," << '\n';
|
|
cout << "]" << '\n';
|
|
}
|
|
|
|
/* 列印鏈結串列 */
|
|
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);
|
|
}
|
|
|
|
/* 列印二元樹 */
|
|
void printTree(TreeNode *root) {
|
|
printTree(root, nullptr, false);
|
|
}
|
|
|
|
/* 列印堆疊 */
|
|
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';
|
|
}
|
|
|
|
/* 列印佇列 */
|
|
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';
|
|
}
|
|
|
|
/* 列印雙向佇列 */
|
|
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';
|
|
}
|
|
|
|
/* 列印雜湊表 */
|
|
// 定義模板參數 TKey 和 TValue ,用於指定鍵值對的型別
|
|
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);
|
|
}
|
|
|
|
/* 列印堆積(優先佇列) */
|
|
template <typename T, typename S, typename C> void printHeap(priority_queue<T, S, C> &heap) {
|
|
vector<T> vec = Container(heap);
|
|
cout << "堆積的陣列表示:";
|
|
printVector(vec);
|
|
cout << "堆積的樹狀表示:" << endl;
|
|
TreeNode *root = vectorToTree(vec);
|
|
printTree(root);
|
|
freeMemoryTree(root);
|
|
}
|