Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Created by Mikhail Sokolovskiy <sokolmish@gmail.com>
- Can be used by anyone for any purpose :)
- Compilation:
- g++ --std=c++11 -o trees trees.cpp
- */
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <stack>
- class TreeNode {
- private:
- std::string name;
- std::vector<TreeNode*> childs;
- void printVertRec(std::stringstream &ss, std::vector<bool> &prev) const {
- int indent = (int)(prev.size()) - 1;
- if (indent != 0) {
- for (int i = 1; i < indent; i++)
- ss << (prev[i] ? " " : "│ ");
- if (prev.back())
- ss << "└─" << name << std::endl;
- else
- ss << "├─" << name << std::endl;
- }
- else {
- ss << name << std::endl;
- }
- if (childs.size() != 0) {
- prev.push_back(false);
- for (int i = 0; i < (int)(childs.size()) - 1; i++)
- childs[i]->printVertRec(ss, prev);
- prev[prev.size() - 1] = true;
- childs[childs.size() - 1]->printVertRec(ss, prev);
- prev.pop_back();
- }
- }
- void printHorRec(std::stringstream &ss, std::vector<int> &prev) const {
- if (prev.back() != 0) {
- for (int i = 1; i < (int)(prev.size()) - 1; i++) {
- for (int j = 0; j < abs(prev[i]); j++)
- ss << " ";
- ss << ((prev[i] < 0) ? " " : "│ ");
- }
- for (int j = 0; j < abs(prev.back()); j++)
- ss << " ";
- ss << ((prev.back() < 0) ? "└─" : "├─");
- }
- if (!childs.empty()) {
- std::stack<const TreeNode*> stack;
- stack.push(this);
- const TreeNode *cur = stack.top();
- while (!cur->childs.empty()) {
- ss << cur->name;
- if (cur->childs.size() == 1) {
- ss << "───";
- prev.push_back(-(cur->name.size() + 1));
- }
- else {
- ss << "─┬─";
- prev.push_back(cur->name.size() + 1);
- }
- stack.push(cur->childs.front());
- cur = stack.top();
- }
- ss << cur->name << std::endl;
- prev.push_back(0);
- while (!stack.empty()) {
- int childsCount = stack.top()->childs.size();
- if (childsCount > 1) {
- for (int i = 1; i < childsCount - 1; i++)
- stack.top()->childs[i]->printHorRec(ss, prev);
- prev[prev.size() - 1] *= -1;
- stack.top()->childs[childsCount - 1]->printHorRec(ss, prev);
- }
- stack.pop();
- prev.pop_back();
- }
- }
- else {
- ss << name << std::endl;
- }
- }
- public:
- explicit TreeNode() = default;
- TreeNode(const std::string &name) : name(name) {}
- void setName(const std::string &name) {
- this->name = name;
- }
- void addChild(TreeNode *child) {
- childs.push_back(child);
- }
- std::string getName() const {
- return name;
- }
- std::vector<TreeNode*> getChilds() const {
- return childs;
- }
- std::string printVert() const {
- std::stringstream ss;
- std::vector<bool> prev;
- prev.push_back(true);
- printVertRec(ss, prev);
- return ss.str();
- }
- std::string printHor() const {
- std::stringstream ss;
- std::vector<int> prev;
- prev.push_back(0);
- printHorRec(ss, prev);
- return ss.str();
- }
- ~TreeNode() {
- for (auto e : childs)
- delete e;
- }
- };
- TreeNode* createTree() {
- TreeNode *root = new TreeNode("root");
- TreeNode *lang = new TreeNode("lang");
- TreeNode *greek = new TreeNode("greek");
- TreeNode *color = new TreeNode("color");
- TreeNode *alpha = new TreeNode("alpha");
- TreeNode *beta = new TreeNode("beta");
- TreeNode *gamma = new TreeNode("gamma");
- TreeNode *delta = new TreeNode("delta");
- TreeNode *black = new TreeNode("black");
- TreeNode *white = new TreeNode("white");
- TreeNode *red = new TreeNode("red");
- TreeNode *purple = new TreeNode("purple");
- for (int i = 0; i < 3; i++) {
- black->addChild(new TreeNode(std::to_string(i * 1 + 1)));
- beta->addChild(new TreeNode(std::to_string(i * 3 + 10)));
- }
- for (int i = 0; i < 2; i++) {
- white->addChild(new TreeNode(std::to_string(i * 1 + 3)));
- purple->addChild(new TreeNode(std::to_string(i * 2 + 23)));
- delta->addChild(new TreeNode(std::to_string(i * 5 + 102)));
- }
- for (int i = 0; i < 3; i++) {
- red->addChild(new TreeNode(std::to_string(i * 12 + 53)));
- alpha->addChild(new TreeNode(std::to_string(i * 16 + 36)));
- gamma->addChild(new TreeNode(std::to_string(i * 7 + 12)));
- }
- greek->addChild(alpha);
- greek->addChild(beta);
- greek->addChild(gamma);
- greek->addChild(delta);
- color->addChild(black);
- color->addChild(white);
- color->addChild(red);
- color->addChild(purple);
- lang->addChild(greek);
- root->addChild(lang);
- root->addChild(color);
- return root;
- }
- int main(int argc, char **argv) {
- TreeNode *root = createTree();
- std::cout << root->printVert() << std::endl;
- std::cout << std::endl;
- std::cout << root->printHor() << std::endl;
- delete root;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement