Untitled
unknown
plain_text
2 years ago
3.7 kB
3
Indexable
#include <iostream> #include <vector> class Tree { public: std::string name; std::vector<Tree*> pointerVector; Tree(std::string name = "") { this->name = name; } Tree* AddSub(std::string newTreeName) { Tree* helper = new Tree(newTreeName); pointerVector.push_back(helper); return helper; } void print(int depth, bool flag) { printTree(this, depth, flag); } void printTree(Tree* root, int depth, bool flag) { if (flag) { depth++; for (int i = 0; i < (depth) - 1; i++) { std::cout << "\t"; } std::cout << root->name << " "; int pointerSize = root->pointerVector.size(); for (int i = 0; i < pointerSize; i++) { std::cout << "\n"; if (depth == i) { } printTree(root->pointerVector[i], depth, flag); } } else { depth++; for (int i = 0; i < (depth) - 1; i++) { std::cout << "\t"; } std::cout << root->name << " "; int pointerSize = root->pointerVector.size(); for (int i = 0; i < pointerSize; i++) { std::cout << "\n"; printTree(root->pointerVector[i], depth, flag); } } } uint32_t GetSubCount() { return this->pointerVector.size(); } /*int GetAllSubCount() { std::cout << "\n\n"; int iterator = 1; std::vector<int> tab; int k = GetAllSubCountH(this, iterator); return k; } uint32_t GetAllSubCountH(Tree* root, std::vector<int> iterator) { iterator.push_back(1); std::cout << root->name << " "<<iterator.size(); int pointerSize = root->pointerVector.size(); for (int i = 0; i < pointerSize; i++) { std::cout << "\n"; GetAllSubCountH(root->pointerVector[i], iterator); } return iterator.size(); } int GetAllSubCountH(Tree* root, int iterator) { std::cout << root->name << " " << iterator; int pointerSize = root->pointerVector.size(); for (int i = 0; i < pointerSize; i++) { std::cout << "\n"; GetAllSubCountH(root->pointerVector[i], iterator++ ); } return iterator; }*/ int GetAllSubCount() { std::cout << "\n"; int k = getAllSubCountH(this); } int getAllSubCountH(Tree* root, int level = 1) { int pointerSize = root->pointerVector.size(); for (int i = 0; i < pointerSize ;i++) { return getAllSubCountH(root->pointerVector[i], level++); } } }; void task4() { Tree* root = new Tree("tree name"); Tree* galaz1 = root->AddSub("galaz 1"); // this function creates a child Tree object and returns pointer to it Tree* galaz2 = root->AddSub("galaz 2"); Tree* galaz3 = root->AddSub("galaz 3"); Tree* galaz1_1 = galaz1->AddSub("galaz 1.1"); Tree* galaz2_1 = galaz2->AddSub("galaz 2.1"); Tree* galaz2_2 = galaz2->AddSub("galaz 2.2"); Tree* galaz2_1_2 = galaz2_1->AddSub("galaz 2.1.2"); root->print(0,true); uint32_t rootChildrenCnt = root->GetSubCount(); std::cout << "\n" << rootChildrenCnt; uint32_t galaz1childrenCount = galaz1->GetSubCount(); std::cout << "\n" << galaz1childrenCount; int countOfAllChildren = root->GetAllSubCount(); std::cout << "\n" << countOfAllChildren; } void task5() { }
Editor is loading...