fork download
  1. #include <algorithm>
  2. #include <array>
  3. #include <fstream>
  4. #include <functional>
  5. #include <iostream>
  6. #include <list>
  7. #include <memory>
  8. #include <regex>
  9. #include <sstream>
  10. #include <string>
  11. #include <tuple>
  12. #include <unordered_map>
  13. #include <utility>
  14. #include <vector>
  15.  
  16.  
  17. class Serializable {
  18. public:
  19. virtual const std::string get_as_string() const = 0;
  20. virtual ~Serializable(){};
  21. };
  22.  
  23. static uint64_t ID_GLOBAL = 0;
  24.  
  25. class Tree {
  26. public:
  27. class Node : public Serializable {
  28. uint64_t uid = ID_GLOBAL++;
  29.  
  30. Node* parent;
  31.  
  32. public:
  33. std::vector<std::unique_ptr<Node> > children;
  34.  
  35. Node(Node* parent)
  36. : parent(parent){
  37.  
  38. };
  39.  
  40. const std::string get_as_string() const override {
  41. static std::string depth_string;
  42.  
  43.  
  44. std::stringstream accumulator;
  45.  
  46. if (depth_string.capacity() == 0) {
  47. depth_string.reserve(128);
  48. };
  49.  
  50. accumulator << "Node#" << std::to_string(uid) << "\n";
  51.  
  52. if(children.size() == 0) { return accumulator.str(); }
  53.  
  54. depth_string.push_back(' ');
  55. depth_string.push_back('|');
  56.  
  57.  
  58. auto&& pointer_to_child_first = std::cbegin(children);
  59. auto&& pointer_to_child_last = std::cend(children) - 1;
  60. auto pointer_to_child_current = pointer_to_child_first;
  61.  
  62. //debug logs
  63. std::cout << "f> " << accumulator.str() << "<\n";
  64.  
  65. if(pointer_to_child_current < pointer_to_child_last) std::cout << " _lt_ " << pointer_to_child_last - pointer_to_child_current << " " << pointer_to_child_current - pointer_to_child_last << "\n";
  66. if(pointer_to_child_current == pointer_to_child_last) std::cout << " _eq_ " << pointer_to_child_last - pointer_to_child_current << " " << pointer_to_child_current - pointer_to_child_last << "\n";
  67. if(pointer_to_child_current > pointer_to_child_last) std::cout << " _gt_ " << pointer_to_child_last - pointer_to_child_current << " " << pointer_to_child_current - pointer_to_child_last << "\n";
  68.  
  69.  
  70. if(std::cbegin(children) < std::cend(children)) std::cout << " 2_lt_ " << std::cend(children) - std::cbegin(children) << " " << std::cbegin(children) - std::cend(children) << "\n";
  71. if(std::cbegin(children) == std::cend(children)) std::cout << " 2_eq_ " << std::cend(children) - std::cbegin(children) << " " << std::cbegin(children) - std::cend(children) << "\n";
  72. if(std::cbegin(children) > std::cend(children)) std::cout << " 2_gt_ " << std::cend(children) - std::cbegin(children) << " " << std::cbegin(children) - std::cend(children) << "\n";
  73.  
  74. //end debug logs
  75.  
  76. for (; pointer_to_child_current < pointer_to_child_last; pointer_to_child_current += 1) {
  77. std::cout << "pcu < pla \n"; //debug
  78.  
  79. accumulator << depth_string << "-" << (*pointer_to_child_current)->get_as_string();
  80. };
  81.  
  82. depth_string.pop_back();
  83.  
  84. std::cout << "fff";//debug
  85.  
  86. if (pointer_to_child_current == pointer_to_child_last) {
  87. std::cout << "\npcu==pla\n";//debug
  88.  
  89. accumulator << depth_string << "`-"
  90. << (*pointer_to_child_last)->get_as_string();
  91. };
  92.  
  93.  
  94. std::cout << accumulator.str() << "\n";
  95.  
  96. depth_string.pop_back();
  97.  
  98. return accumulator.str();
  99. };
  100. };
  101.  
  102. std::unique_ptr<Node> root;
  103.  
  104. Tree() { root = std::make_unique<Node>(new Node{nullptr}); }
  105. };
  106.  
  107.  
  108. int main() {
  109.  
  110.  
  111. using Node = Tree::Node;
  112.  
  113. Tree e;
  114.  
  115. auto parent = e.root.get();
  116.  
  117. std::cout << "gj\n";
  118.  
  119. parent->children.emplace_back(new Node{parent});
  120. parent->children.emplace_back(new Node{parent});
  121. parent->children.emplace_back(new Node{parent});
  122.  
  123. parent = e.root->children[0].get();
  124. parent->children.emplace_back(new Node{parent});
  125. parent->children.emplace_back(new Node{parent});
  126. parent->children.emplace_back(new Node{parent});
  127.  
  128. parent = e.root->children[2].get();
  129. parent->children.emplace_back(new Node{parent});
  130.  
  131. parent = e.root->children[0]->children[1].get();
  132. parent->children.emplace_back(new Node{parent});
  133. parent->children.emplace_back(new Node{parent});
  134.  
  135. std::cout << e.root->get_as_string();
  136. std::cout << "\n end";
  137. }
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
gj
f> Node#1
<
 _lt_ 2 -2
 2_lt_ 3 -3
pcu < pla 
f> Node#2
<
 _lt_ 2 -2
 2_lt_ 3 -3
pcu < pla 
pcu < pla 
f> Node#6
<
 _lt_ 1 -1
 2_lt_ 2 -2
pcu < pla 
fff
pcu==pla
Node#6
 | | |-Node#9
 | | `-Node#10

fff
pcu==pla
Node#2
 | |-Node#5
 | |-Node#6
 | | |-Node#9
 | | `-Node#10
 | `-Node#7

pcu < pla 
fff
pcu==pla
f> Node#4
<
 _eq_ 0 0
 2_lt_ 1 -1
fff
pcu==pla
Node#4
  `-Node#8

Node#1
 |-Node#2
 | |-Node#5
 | |-Node#6
 | | |-Node#9
 | | `-Node#10
 | `-Node#7
 |-Node#3
 `-Node#4
  `-Node#8

Node#1
 |-Node#2
 | |-Node#5
 | |-Node#6
 | | |-Node#9
 | | `-Node#10
 | `-Node#7
 |-Node#3
 `-Node#4
  `-Node#8

 end