cpp-algo-cases/utils/print_utils.hpp

290 lines
9.0 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* 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 */ // <20><>һ<EFBFBD><D2BB> vector <20>в<EFBFBD><D0B2><EFBFBD>Ԫ<EFBFBD><D4AA>
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) // <20><><EFBFBD>ҵ<EFBFBD>Ԫ<EFBFBD><D4AA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><D8B5>±<EFBFBD>
{
j = i;
}
}
return j;
}
/* Concatenate a vector with a delim */ // <20><>һ<EFBFBD><D2BB> vector <20><>ָ<EFBFBD><D6B8><EFBFBD>ķָ<C4B7><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3>ַ<EFBFBD><D6B7><EFBFBD>
template <typename T>
string strJoin(const string &delim, const T &vec) // delim <20>Ƿָ<C7B7><D6B8><EFBFBD><EFBFBD><EFBFBD>vec <20><> vector(ʹ<><CAB9>&vec<65><63>ʾ<EFBFBD><CABE><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>)
{
ostringstream s; // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (const auto &i : vec)
{
if (&i != &vec[0])
{
s << delim; // <20><><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
s << i; // <20><>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
return s.str(); // .str() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ
}
/* Repeat a string for n times */ // <20><>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ظ<EFBFBD> n <20><>
string strRepeat(string str, int n)
{
ostringstream os; // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < n; i++)
os << str; // <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ظ<EFBFBD> n <20><>
return os.str(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ
}
/* Print an Array */ // <20><>ӡһ<D3A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
template <typename T>
void printArray(T *arr, int n) // arr <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ<EFBFBD><D6B7>n <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
{
cout << "[";
for (int i = 0; i < n - 1; i++)
{
cout << arr[i] << ", "; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
}
if (n >= 1) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4>ڵ<EFBFBD><DAB5><EFBFBD> 1 ʱ
cout << arr[n - 1] << "]" << endl; // <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA> <20><> <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> ']' <20><><EFBFBD><EFBFBD>
else
cout << "]" << endl; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9B3A4>Ϊ 0 ʱ<><CAB1>ֱ<EFBFBD>Ӵ<EFBFBD>ӡһ<D3A1><D2BB> ']' <20><><EFBFBD><EFBFBD>
}
/* Get the Vector String object */ // <20><>ȡһ<C8A1><D2BB> vector <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ
template <typename T>
string getVectorString(vector<T> &list) // list <20><> vector
{
return "[" + strJoin(", ", list) + "]"; // ʹ<><CAB9> strJoin <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> vector <20>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> '[' <20><> ']'
}
/* Print a vector */ // <20><>ӡһ<D3A1><D2BB> vector
template <typename T>
void printVector(vector<T> list)
{
cout << getVectorString(list) << '\n'; // ʹ<><CAB9> getVectorString <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ vector <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ
}
/* Print a vector matrix */ // <20><>ӡһ<D3A1><D2BB> vector <20><><EFBFBD><EFBFBD>
template <typename T>
void printVectorMatrix(vector<vector<T>> &matrix) // matrix <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ά vector, <20><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>Ԫ<EFBFBD>ض<EFBFBD><D8B6><EFBFBD>һ<EFBFBD><D2BB> vector, <20><><EFBFBD>ڱ<EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
{
cout << "[" << '\n';
for (vector<T> &list : matrix) // <20><><EFBFBD><EFBFBD> matrix <20>е<EFBFBD>ÿһ<C3BF><D2BB> vector Ԫ<><D4AA>
cout << " " + getVectorString(list) + "," << '\n'; // ʹ<><CAB9> getVectorString <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ vector <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ
cout << "]" << '\n';
}
/* Print a linked list */ // <20><>ӡһ<D3A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void printLinkedList(ListNode *head)
{
vector<int> list; // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> vector <20><><EFBFBD>ڴ洢<DAB4><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
while (head != nullptr) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>벻Ϊ<EBB2BB><CEAA>ʱ, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
list.push_back(head->val); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD> vector <20><>
head = head->next; // <20><>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA>
}
cout << strJoin(" -> ", list) << '\n'; // ʹ<><CAB9> strJoin <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> vector <20>ü<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>ӳ<EFBFBD><D3B3>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ
}
/**
* This tree printer is borrowed from TECHIE DELIGHT // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD>Ǵ<EFBFBD> TECHIE DELIGHT <20><><EFBFBD>õ<EFBFBD>
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
struct Trunk // <20><><EFBFBD>ɽṹ<C9BD><E1B9B9>
{
Trunk *prev; // ָ<><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
string str; // <20><><EFBFBD>ɵ<EFBFBD><C9B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ
Trunk(Trunk *prev, string str) // <20><><EFBFBD><EFBFBD><ECBAAF> <20><>cpp <20>е<EFBFBD> struct Ҳ<><D2B2><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD><ECBAAF><EFBFBD><EFBFBD>
{
this->prev = prev;
this->str = str;
}
};
/* Helper function to print branches of the binary tree */ // <20><><EFBFBD>ڴ<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void showTrunks(Trunk *p)
{
if (p == nullptr) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ<EFBFBD><CAB1>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
{
return;
}
// <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD>õ<EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>˳<EFBFBD><CBB3><EFBFBD>
showTrunks(p->prev); // <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD> showTrunks <20><><EFBFBD><EFBFBD>
cout << p->str; // <20><>ӡ<EFBFBD><D3A1><EFBFBD>ɵ<EFBFBD><C9B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ
}
/* Print a binary tree */ // <20><>ӡһ<D3A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void printTree(TreeNode *root, Trunk *prev, bool isRight)
{
if (root == nullptr)
{
return;
}
string prev_str = " "; // <20><>ʼ<EFBFBD><CABC> prev_str Ϊ<>ո<EFBFBD>
Trunk trunk(prev, prev_str); // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ɶ<EFBFBD><C9B6><EFBFBD>
printTree(root->right, &trunk, true); // <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD> printTree <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (!prev) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ
{
trunk.str = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"; // <20><><EFBFBD>ɵ<EFBFBD><C9B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾΪ "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
}
else if (isRight) // <20><><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
{
trunk.str = "/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"; // <20><><EFBFBD>ɵ<EFBFBD><C9B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾΪ "/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
prev_str = " |"; // prev_str Ϊ " |"
}
else // <20><><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
{
trunk.str = "\\<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"; // <20><><EFBFBD>ɵ<EFBFBD><C9B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾΪ "\<5C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
prev->str = prev_str; // prev_str Ϊ prev->str
}
showTrunks(&trunk); // <20><><EFBFBD><EFBFBD> showTrunks <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
cout << " " << root->val << endl; // <20><>ӡ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>ֵ
if (prev) // <20><><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD>Ϊ<EFBFBD><CEAA>ʱ
{
prev->str = prev_str; // prev->str Ϊ prev_str
}
trunk.str = " |"; // <20><><EFBFBD>ɵ<EFBFBD><C9B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾΪ " |"
printTree(root->left, &trunk, false); // <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD> printTree <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/* The interface of the tree printer */ // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD>Ľӿ<C4BD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: Ŀ<><C4BF><EFBFBD><EFBFBD>Ϊ<EFBFBD>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
void printTree(TreeNode *root)
{
printTree(root, nullptr, false); // <20><><EFBFBD><EFBFBD> printTree <20><><EFBFBD><EFBFBD> (<28><><EFBFBD>ذ汾)
}
/* Print a stack */ // <20><>ӡһ<D3A1><D2BB>ջ
template <typename T>
void printStack(stack<T> stk)
{
// Reverse the input stack // <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ
stack<T> tmp; // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʱջ
while (!stk.empty()) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ϊ<EFBFBD><CEAA>ʱ (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ)
{
tmp.push(stk.top()); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>ʱջ<CAB1><D5BB>
stk.pop(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA>
}
// Generate the string to print // <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ӡ<EFBFBD><D3A1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
ostringstream s; // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool flag = true; // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>־λ (Ĭ<><C4AC>Ϊ true), <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ǵ<EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA>
while (!tmp.empty())
{
if (flag)
{
s << tmp.top(); // <20><><EFBFBD><EFBFBD>־λΪ true ʱ<><CAB1><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
flag = false; // <20><><EFBFBD><EFBFBD>־λ<D6BE><CEBB><EFBFBD><EFBFBD>Ϊ false
}
else // <20><><EFBFBD><EFBFBD>־λΪ false ʱ
s << ", " << tmp.top(); // <20><>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
tmp.pop(); // <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA>
}
cout << "[" + s.str() + "]" << '\n'; // ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ
}
/* Print a queue */ // <20><>ӡһ<D3A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
template <typename T>
void printQueue(queue<T> queue) // queue <20>Ƕ<EFBFBD><C7B6><EFBFBD>
{
// Generate the string to print // <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ӡ<EFBFBD><D3A1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
ostringstream s;
bool flag = true;
while (!queue.empty())
{
if (flag)
{
s << queue.front(); // <20><><EFBFBD><EFBFBD>־λΪ true ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĶ<D0B5><C4B6><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (true <20><>ʾ<EFBFBD>ǵ<EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA>)
flag = false;
}
else
s << ", " << queue.front(); // <20><><EFBFBD><EFBFBD>־λΪ false ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĶ<D0B5><C4B6><EFBFBD>Ԫ<EFBFBD><D4AA>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
queue.pop(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
}
cout << "[" + s.str() + "]" << '\n'; // ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ
}
/* Print a deque */ // <20><>ӡһ<D3A1><D2BB>˫<EFBFBD>˶<EFBFBD><CBB6><EFBFBD>
template <typename T>
void printDeque(deque<T> deque) // deque <20><>˫<EFBFBD>˶<EFBFBD><CBB6><EFBFBD>(<28><><EFBFBD><EFBFBD> deck)
{
// Generate the string to print // <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ӡ<EFBFBD><D3A1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
ostringstream s;
bool flag = true;
while (!deque.empty()) // <20><>˫<EFBFBD>˶<EFBFBD><CBB6>в<EFBFBD>Ϊ<EFBFBD><CEAA>ʱ
{
if (flag)
{
s << deque.front();
flag = false;
}
else
s << ", " << deque.front();
deque.pop_front(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
}
cout << "[" + s.str() + "]" << '\n';
}
/* Print a HashMap */ // <20><>ӡһ<D3A1><D2BB><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3>
// <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TKey <20><> TValue <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ֵ<EFBFBD>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD><EFBFBD>
template <typename TKey, typename TValue>
void printHashMap(unordered_map<TKey, TValue> map) // map <20>ǹ<EFBFBD>ϣ<EFBFBD><CFA3>
{
for (auto kv : map) // auto <20>ؼ<EFBFBD><D8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
cout << kv.first << " -> " << kv.second << '\n'; // .first <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>.second <20><>ʾֵ
}
}
/* Expose the underlying storage of the priority_queue container */ // <20><><EFBFBD><C2B6><EFBFBD>ȶ<EFBFBD><C8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵײ<C4B5><D7B2>
template <typename T, typename S, typename C>
S &Container(priority_queue<T, S, C> &pq) // pq <20><><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><C8B6><EFBFBD>
{
struct HackedQueue : private priority_queue<T, S, C> // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>˽<EFBFBD>м̳е<CCB3><D0B5><EFBFBD><EFBFBD>ȶ<EFBFBD><C8B6><EFBFBD>
{
static S &Container(priority_queue<T, S, C> &pq) // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>̬<EFBFBD><CCAC>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD> Container
{
return pq.*&HackedQueue::c; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><C8B6>еĵײ<C4B5><D7B2>
}
};
return HackedQueue::Container(pq); // <20><><EFBFBD>þ<EFBFBD>̬<EFBFBD><CCAC>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD> Container
}
/* Print a Heap (PriorityQueue) */ // <20><>ӡһ<D3A1><D2BB><EFBFBD>ѣ<EFBFBD><D1A3><EFBFBD><EFBFBD>ȶ<EFBFBD><C8B6>У<EFBFBD>
template <typename T, typename S, typename C>
void printHeap(priority_queue<T, S, C> &heap) // heap <20><><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><C8B6><EFBFBD>
{
vector<T> vec = Container(heap); // <20><>ȡ<EFBFBD><C8A1><EFBFBD>ȶ<EFBFBD><C8B6>еĵײ<C4B5><D7B2>
cout << "<EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>";
printVector(vec);
cout << "<EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD>״<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>" << endl;
TreeNode *root = vectorToTree(vec); // <20>½<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> root<6F><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><C8B6>еĵײ<C4B5><D7B2>洢ת<E6B4A2><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
printTree(root);
freeMemoryTree(root);
}