2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2023-04-24 04:11:18 +08:00
|
|
|
* File: print_utils.hpp
|
2022-11-25 02:04:38 +08:00
|
|
|
* Created Time: 2021-12-19
|
2024-02-07 22:21:18 +08:00
|
|
|
* Author: krahets (krahets@163.com), msk397 (machangxinq@gmail.com), LoneRanger(836253168@qq.com)
|
2022-11-25 02:04:38 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
#include "list_node.hpp"
|
|
|
|
#include "tree_node.hpp"
|
2023-04-14 03:44:02 +08:00
|
|
|
#include <climits>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* 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;
|
2022-11-25 02:04:38 +08:00
|
|
|
}
|
2023-04-14 03:44:02 +08:00
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* 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;
|
2022-11-25 02:04:38 +08:00
|
|
|
}
|
2023-04-14 03:44:02 +08:00
|
|
|
s << i;
|
|
|
|
}
|
|
|
|
return s.str();
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* 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();
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Print an 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;
|
|
|
|
}
|
2022-11-27 19:07:35 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Get the Vector String object */
|
|
|
|
template <typename T> string getVectorString(vector<T> &list) {
|
|
|
|
return "[" + strJoin(", ", list) + "]";
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Print a vector */
|
|
|
|
template <typename T> void printVector(vector<T> list) {
|
|
|
|
cout << getVectorString(list) << '\n';
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Print a vector matrix */
|
|
|
|
template <typename T> void printVectorMatrix(vector<vector<T>> &matrix) {
|
|
|
|
cout << "[" << '\n';
|
|
|
|
for (vector<T> &list : matrix)
|
|
|
|
cout << " " + getVectorString(list) + "," << '\n';
|
|
|
|
cout << "]" << '\n';
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Print a linked list */
|
|
|
|
void printLinkedList(ListNode *head) {
|
|
|
|
vector<int> list;
|
|
|
|
while (head != nullptr) {
|
|
|
|
list.push_back(head->val);
|
|
|
|
head = head->next;
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
cout << strJoin(" -> ", list) << '\n';
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/**
|
|
|
|
* This tree printer is borrowed from TECHIE DELIGHT
|
|
|
|
* https://www.techiedelight.com/c-program-print-binary-tree/
|
|
|
|
*/
|
|
|
|
struct Trunk {
|
|
|
|
Trunk *prev;
|
|
|
|
string str;
|
|
|
|
Trunk(Trunk *prev, string str) {
|
|
|
|
this->prev = prev;
|
|
|
|
this->str = str;
|
|
|
|
}
|
|
|
|
};
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Helper function to print branches of the binary tree */
|
|
|
|
void showTrunks(Trunk *p) {
|
|
|
|
if (p == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-29 02:21:49 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
showTrunks(p->prev);
|
|
|
|
cout << p->str;
|
|
|
|
}
|
2022-11-30 02:27:26 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Print a binary tree */
|
2023-11-01 05:14:22 +08:00
|
|
|
void printTree(TreeNode *root, Trunk *prev, bool isRight) {
|
2023-04-14 03:44:02 +08:00
|
|
|
if (root == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-14 17:18:32 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
string prev_str = " ";
|
|
|
|
Trunk trunk(prev, prev_str);
|
2023-02-04 14:35:45 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
printTree(root->right, &trunk, true);
|
|
|
|
|
|
|
|
if (!prev) {
|
|
|
|
trunk.str = "———";
|
2023-11-01 05:14:22 +08:00
|
|
|
} else if (isRight) {
|
2023-04-14 03:44:02 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The interface of the tree printer */
|
|
|
|
void printTree(TreeNode *root) {
|
|
|
|
printTree(root, nullptr, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print a 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 a 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 a 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 a HashMap */
|
2023-09-17 01:13:15 +08:00
|
|
|
// 定义模板参数 TKey 和 TValue ,用于指定键值对的类型
|
2023-04-14 03:44:02 +08:00
|
|
|
template <typename TKey, typename TValue> void printHashMap(unordered_map<TKey, TValue> map) {
|
|
|
|
for (auto kv : map) {
|
|
|
|
cout << kv.first << " -> " << kv.second << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 23:04:01 +08:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Print a Heap (PriorityQueue) */
|
|
|
|
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;
|
2023-07-19 16:09:27 +08:00
|
|
|
TreeNode *root = vectorToTree(vec);
|
2023-04-14 03:44:02 +08:00
|
|
|
printTree(root);
|
|
|
|
freeMemoryTree(root);
|
|
|
|
}
|