效果如
└──0
├──1
│ ├──11
│ │ ├──111
│ │ └──112
│ └──12
│ └──13
│ └──14
└──2
├──21
└──3
代码如下
#include <iostream>
#include <vector>
#include <memory>
class MNode;
using MNodeSharedPtr = std::shared_ptr<MNode>;
class MNode {
public:
std::vector<MNodeSharedPtr> children;
int data;
};
class MTree {
public:
MNodeSharedPtr root;
};
void printMTree(const std::string& prefix, MNodeSharedPtr node, bool isFirst)
{
if( node != nullptr )
{
std::cout << prefix;
std::cout << (isFirst ? "├──" : "└──" );
// print the value of the node
std::cout << node->data << std::endl;
// enter the next tree level - left and right branch
for (int i = 0; i <