Making grandchild of root null , the right way in C++ I am trying to create a multiway tree with the following code in C++. As of now, it is more like a sample piece of code.

I wish to do the following with this piece of code(and I am able to get the desired result):


Create a struct Node of two members : value(of the node) and array of children of the node.
Make 'root's value = 5
Make the children's values equal to 10,20,30 &amp; 40
Make the children of children's values equal to null(=0);


I am getting the desired output , but what I want to know is if I am doing it the right way.

Thanks, in advance.

//multinode tree
#include&lt;iostream&gt;
#define null 0

struct Node
{
    int value;
    Node *child[4]; 
};

void display(Node *root){
    if(root==null)
        return;
    else{

        std::cout&lt;&lt;root-&gt;value&lt;&lt;" ";
        if(root-&gt;child[0]!=null)
            for (int i=0;i&lt;4;i++)
            {
                if(root-&gt;child[i]!=null)
                    display(root-&gt;child[i]);

            }
    }
}

int main()
{
    Node *root=new Node;
    Node *test=null;
    root-&gt;value=5;
    int values[]={10,20,30,40};
    int i=0;
    for (i=0;i&lt;4;i++)
    {
        Node *temp=new Node;
        temp-&gt;value=values[i];
        root-&gt;child[i]=temp;
        for (int j=0;j&lt;4;j++)
        {
            root-&gt;child[i]-&gt;child[j]=null;
        }
        std::cout&lt;&lt;"Root value "&lt;&lt;root-&gt;child[i]-&gt;value&lt;&lt;"\n\n ";
    }
    std::cout&lt;&lt;"\n\n\n\n\n";
    display(root);

}
This is a very C like approach and you are using C++ so take advantage of that fact.


Get rid of the #define and use nullptr keyword instead.
Name member variables something special such as m_ so Node *m_children[4]; 
Try use C++ data structures instead of arrays and char* so vector&lt;Node*&gt; children.
Make the Node struct into a class and write getters and setters to encapsulate the new vector structure you should use.
Encapsulate the void display(Node *root) function into a class such:

class Tree
{
    Tree(int depth);// create a tree with x depth
    etc...
    void display();
public:
    Node* m_root;
};


You could create some functionality to able to initialise the tree with some custom data structure.
Add the following code in main into the tree class

for (i=0;i&lt;4;i++)
{
    Node *temp=new Node;
    temp-&gt;value=values[i];
    root-&gt;child[i]=temp;
    for (int j=0;j&lt;4;j++)
    {
        root-&gt;child[i]-&gt;child[j]=null;
    }
    std::cout&lt;&lt;"Root value "&lt;&lt;root-&gt;child[i]-&gt;value&lt;&lt;"\n\n ";
}



PS. Sorry if the code doesn't work; I'm at work and have no way to check it!