Good day.

I want to write an RRT, but first I wanted to write a regular tree, only so that the pointers to the right and left descendant were stored in a vector.

Writing:

#include <iostream> //#include <GL\freeglut.h> #include <ctime> #include <cmath> #include <vector> #include <iterator> using namespace std ; struct Node{ int value ; vector <Node*> Children(2) ; }; void Push(int info, Node *&tree){ if(tree == NULL){ tree = new Node ; tree -> value = info ; tree -> Children[0] = NULL ; tree -> Children[1] = NULL ; } if(info > tree -> value){ Push(info, tree -> Children[1]) ; } if(info < tree -> value){ Push(info, tree -> Children[0]) ; } } void Show(Node *&tree){ if(tree != NULL){ Show(tree -> Children[0]) ; cout << tree -> value << " " ; Show(tree -> Children[1]) ; } } int main(){ Node *tree = NULL ; int x ; for (int i = 0; i < 7 ; i++){ cout << "Put : " ; cin >> x ; Push(x, tree) ; cout << endl ; } Show(tree) ; return 0 ; } 

Errors:

expected unqualified-id before numeric constant in the structure.

invalid types ' unresolved overloaded function type int ' for array subscript in each function.

Help me to understand. I am new to vectors, so errors can be trivial, thanks in advance for your understanding.

Update

As I understood: vector <Node*> Children(2) - it was not worth it. It was necessary vector <Node*> Children ; .

But now how to address descendants?

    1 answer 1

    The first mistake you have already fixed. It remains to fix one more.

     if(tree == NULL){ tree = new Node ; tree -> value = info ; tree->Children.resize(2); // эта строка добавляет два элемента. tree -> Children[0] = NULL ; tree -> Children[1] = NULL ; } 

    Or you could do it this way:

     if(tree == NULL){ tree = new Node ; tree -> value = info ; tree->Children.push_back(NULL); tree->Children.push_back(NULL); } 

    Does it work correctly - I do not know.

    There is a good memory leak in this code. You create an object, and you forget to delete it.

    • Thanks, after your decision came up with another: in the structure to add a selection. struct Node {int value; vector <Node *> Children; Node (): Children (2) {}}; - Alexander Kuznetsov