[英]C++ | Print tree (not necessarily binary) in a pretty way to stdout
假設我有一個這樣的struct
:
struct Node {
int val = 0;
std::vector<Node *> _children{};
};
如何以整潔漂亮的方式將其打印到stdout
? 例如像tree
程序打印出目錄結構的方式。
parent/
├── child1
│ ├── cchild1
│ ├── cchild2
│ ├── child3
│ │ └── child1
│ └── child4
│ ├── child1
│ └── child2
└── child2
└── cchild1
└── ccchild1
它不必完全按照上面的格式,因為我在這里並沒有什么偏好。
這個問題與此不同,因為我想漂亮地打印一棵有多個孩子的樹。
通過修改此答案中的代碼,我設法使其適用於具有多個孩子的 Tree
void Node::printSubtree(const std::string &prefix) {
using std::cout;
using std::endl;
if (_children.empty()) return;
cout << prefix;
size_t n_children = _children.size();
cout << (n_children > 1 ? "├── " : "");
for (size_t i = 0; i < n_children; ++i) {
Node *c = _children[i];
if (i < n_children - 1) {
bool printStrand = n_children > 1 && !c->_children.empty();
std::string newPrefix = prefix + (printStrand ? "│\t" : "\t");
std::cout << val << "\n";
c->printSubtree(newPrefix);
} else {
cout << (n_children > 1 ? prefix : "") << "└── ";
std::cout << val << "\n";
c->printSubtree(prefix + "\t");
}
}
}
void Node::printTree() {
using std::cout;
std::cout << val << "\n";
printSubtree("");
cout << "\n";
}
但是,歡迎新的解決方案。
要在 if (i < n_children - 1) 條件下修復縮進,並給出整個示例,這是我調整的代碼:
#include <iostream>
#include <vector>
using namespace std;
class Node {
public:
Node(std::string val) {
this->val = val;
}
std::vector<Node*> _children;
std::string val;
void printSubtree(const std::string &prefix) {
using std::cout;
using std::endl;
if (_children.empty()) return;
cout << prefix;
size_t n_children = _children.size();
cout << (n_children > 1 ? "├── " : "");
for (size_t i = 0; i < n_children; ++i) {
Node *c = _children[i];
if (i < n_children - 1) {
if (i > 0) { // added fix
cout << prefix<< "├── "; // added fix
} // added fix
bool printStrand = n_children > 1 && !c->_children.empty();
std::string newPrefix = prefix + (printStrand ? "│\t" : "\t");
std::cout << c->val << "\n";
c->printSubtree(newPrefix);
} else {
cout << (n_children > 1 ? prefix : "") << "└── ";
std::cout << c->val << "\n";
c->printSubtree(prefix + "\t");
}
}
}
void printTree() {
using std::cout;
std::cout << val << "\n";
printSubtree("");
cout << "\n";
}
};
int main()
{
Node root("root");
Node node1("c1");
Node node11("c11");
Node node12("c12");
Node node13("c13");
node1._children.push_back(&node11);
node1._children.push_back(&node12);
node1._children.push_back(&node13);
Node node111("c111");
node11._children.push_back(&node111);
Node node112("c112");
node11._children.push_back(&node112);
Node node113("c113");
node11._children.push_back(&node113);
Node node114("c114");
node11._children.push_back(&node114);
Node node115("c115");
node11._children.push_back(&node115);
Node node116("c116");
node11._children.push_back(&node116);
Node node2("c2");
Node node21("c21");
node2._children.push_back(&node21);
Node node211("c211");
node21._children.push_back(&node211);
Node node2111("c2111");
node211._children.push_back(&node2111);
Node node3("c3");
Node node31("c31");
node3._children.push_back(&node31);
Node node4("c4");
root._children.push_back(&node1);
root._children.push_back(&node2);
root._children.push_back(&node3);
root._children.push_back(&node4);
root.printTree();
return 0;
}
root
├── c1
│ ├── c11
│ │ ├── c111
│ │ ├── c112
│ │ ├── c113
│ │ ├── c114
│ │ ├── c115
│ │ └── c116
│ ├── c12
│ └── c13
├── c2
│ └── c21
│ └── c211
│ └── c2111
├── c3
│ └── c31
└── c4
我使用https://www.tutorialspoint.com/compile_cpp_online.php來測試它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.