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 & 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<iostream>
#define null 0
struct Node
{
int value;
Node *child[4];
};
void display(Node *root){
if(root==null)
return;
else{
std::cout<<root->value<<" ";
if(root->child[0]!=null)
for (int i=0;i<4;i++)
{
if(root->child[i]!=null)
display(root->child[i]);
}
}
}
int main()
{
Node *root=new Node;
Node *test=null;
root->value=5;
int values[]={10,20,30,40};
int i=0;
for (i=0;i<4;i++)
{
Node *temp=new Node;
temp->value=values[i];
root->child[i]=temp;
for (int j=0;j<4;j++)
{
root->child[i]->child[j]=null;
}
std::cout<<"Root value "<<root->child[i]->value<<"\n\n ";
}
std::cout<<"\n\n\n\n\n";
display(root);
}

#define null 0"? What's wrong withNULL? – Johnsyweb Feb 7 at 8:21