Klasa, która wygeneruje drzewo stringów

0

Witam mam zadanie do wykonania, które polega na napisaniu klasy, która będzie w stanie wygenerować drzewo stringów.

int main(...)
{
  Tree* root = new Tree("nazwa drzewa");
  Tree* galaz1 = root->AddSub("galaz 1"); // funkcja ta tworzy galaz w roocie i zwraca wskaznik
  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");

Jak do tej pory (korzystając z: https://4programmers.net/Forum/C_i_C++/133771-Drzewo_w_C++) zrobiłem to:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Tree{
public:
    string name;
    vector<Tree*> children;
    Tree(string name_) : name(name_) { };

    AddSub(string name){
		;   //Co miałoby się znaleźć w tej metodzie?
    }
};


int nesting = 0;
void print(Tree *dir) {
    for (int i = 0; i < nesting; ++i) {
            cout << ' ';
    }
    cout << dir->name << endl;
    ++nesting;
    for_each(dir->children.begin(), dir->children.end(), print);
    --nesting;
}

int main()
{
    Tree* root = new Tree("nazwa drzewa");
    Tree* galaz1 = root->AddSub("galaz 1"); 
    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->children.push_back(galaz1);
    galaz1->children.push_back(galaz2);
    galaz2->children.push_back(galaz3);
    galaz1_1->children.push_back(galaz1);
    galaz2_1->children.push_back(galaz2);
    galaz2_2->children.push_back(galaz2);
    galaz2_1_2->children.push_back(galaz2_1);

    print(root);
    return 0;
}

Nie wiem co miałbym zapisać w metodzie AddSub, która jest wywoływana w mainie? Wydaje mi si, że powinna coś zwracać ponieważ wartość jest przypisywana do galaz1, galaz2...
Mógłby mi ktoś pomóc?

1

W AddSub trzeba utworzyć obiekt klasy Tree, dodać go do tablicy children i w wyniku przekazać w/w nowoutworzony obiekt.

2
/*///
// Wiersja w starym C++
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Tree
{
private:
    string name;
    vector<Tree*> children;
public:
    Tree(const string &name):name(name) { }

    Tree *AddSub(const string &name)
    {
		Tree *node=new Tree(name);
        children.push_back(node);
		return node;
    }
    void print(int nesting=0)const
    {
        cout<<string(nesting,'\t')<<name<<endl;
        ++nesting;
        for(Tree *node:children) node->print(nesting);
    }
};

int main()
{
    Tree* root = new Tree("nazwa drzewa");
    Tree* galaz1 = root->AddSub("galaz 1"); 
    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();
    return 0;
}
/*/
// Wiersja w nowym C++
#include <iostream>
#include <memory>
#include <string>
#include <vector>
using namespace std;

class Tree
{
private:
    string name;
    vector<shared_ptr<Tree>> children;
public:
    Tree(const string &name):name(name) { }

    shared_ptr<Tree> AddSub(const string &name)
    {
		shared_ptr<Tree> node=make_shared<Tree>(name);
        children.push_back(node);
		return node;
    }
    void print(int nesting=0)const
    {
        cout<<string(nesting,'\t')<<name<<endl;
        ++nesting;
        for(const shared_ptr<Tree> node:children) node->print(nesting);
    }
};

int main()
{
    shared_ptr<Tree> root=make_shared<Tree>("nazwa drzewa");
    shared_ptr<Tree> galaz1 = root->AddSub("galaz 1"); 
    shared_ptr<Tree> galaz2 = root->AddSub("galaz 2");
    shared_ptr<Tree> galaz3 = root->AddSub("galaz 3");
    shared_ptr<Tree> galaz1_1 = galaz1->AddSub("galaz 1.1");
    shared_ptr<Tree> galaz2_1 = galaz2->AddSub("galaz 2.1");
    shared_ptr<Tree> galaz2_2 = galaz2->AddSub("galaz 2.2");
    shared_ptr<Tree> galaz2_1_2 = galaz2_1->AddSub("galaz 2.1.2");
    root->print();
    return 0;
}
//*///
0

@_13th_Dragon:

Właśnie, raziły mnie w oryginale raziły zwykle C-wskazniki

0
ZrobieDobrze napisał(a):

Właśnie, raziły mnie w oryginale raziły zwykle C-wskazniki

Nie wiedzieć czemu wszystkie szkoły/licea/uczelnie uczą co najwyżej C++ przed C++11 czyli z poprzedniego tysiąclecia!

0

Dziękuję za pomoc szczególnie dla @_13th_Dragon
Mam jeszcze pytanie co w linijce 26 for(Tree *node:children) node->print(nesting);
oznacza for(Tree *node:children)? Jak to rozumieć?

0
bartosz1986 napisał(a):

Mam jeszcze pytanie co w linijce 26 for(Tree *node:children) node->print(nesting);
oznacza for(Tree *node:children)? Jak to rozumieć?

https://www.digitalocean.com/community/tutorials/foreach-loop-c-plus-plus <- pierwsze z brzegu pod hasłem: cpp foreach

1 użytkowników online, w tym zalogowanych: 0, gości: 1