Help with C++ Program

Hello,

I'm trying to code this program (part of my assignment), I'm just having a problem with generating a random tree, each node with a random number of children, the default depth is 5, and the number of children is bounded between 0 and 5.  I want a program that generates that tree and print it.  Please help!
Here are my three files,

"Tree.h"
#ifndef TREE_H
#define TREE_H

#include <iostream>
using std::endl;
#include "TreeNode.h"

template < class NODETYPE >
class Tree {
public:
      Tree();
      int printTree;
private:
      TreeNode< NODETYPE > *rootptr;
};

template < class NODETYPE >
Tree< NODETYPE >::Tree()
{};
template< class NODETYPE >
int Tree< NODETYPE >::printTree(TreeNode< NODETYPE > *ptr )
{
      if (ptr!=null)
      {
            printTree(ptr->children);
            cout<<ptr->data<<' ';
      }
};
#endif
-------------
file 2 "#ifndef TREENODE_H
#define TREENODE_H

#include <iostream>
#include <new>
#include <cstdlib>

template< class NODETYPE > class Tree;
template< class NODETYPE >
class TreeNode {
      friend class Tree< NODETYPE >;
      private:
      int depth=5;
      int data;
      int children_num;
      TreeNode *children;

      public:
      TreeNode()
      {
            data=(1+Rand()%50);
            if (depth=0)
                  children_num=0;
            else {
                     depth=depth-1;
                     children_num=(0+Rand()%5);
                 }
      }
      };
#endif.h"
------
file3 "prog1.cpp"
#include <iostream>
using namespace std;

using std::cout;
using std::cin;
using std::fixed;

#include <iomanip>
using std::setprecision;

#include "Tree.h"

int main()
{
      Tree < int > intTree;
      intTree.printTree;
      return 0;
}

LVL 1
SerphAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexNekCommented:
Here is wrong:
class Tree {
public:
      Tree();
      int printTree; /*/***

data not declared.
ptr->data
I can't found the place where you add a node.

it must be function call:
intTree.printTree;
0
SerphAuthor Commented:
I'm such an idiot!
I just fixed that, also changed from int print to void.
And removed int from the function call in main. (Thanks!)

But the data is supposed to be a random number which is assigned to the data variable in the object through  the constructor.  Nodes are generated at runtime.  Do I still need insert node?  Isn't the constructor enough?
0
AlexNekCommented:
In constructor I only saw
children_num=(0+Rand()%5);

What I found wrong, in addition
TreeNode< NODETYPE > *rootptr
It must be something like
Tree* rootptr;
or better
Tree* m_pNext;

so you need
to use somewhere
Tree* Node = new Tree();
Node->m_pNext = Node;
for add one Node
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

SerphAuthor Commented:
Thanks again, I'm really having a lot of errors in this code, would it be better if I try and merge the two header files?
I'll also try and write an insertnode to add nodes.

As for the data, it is in the same place as the children_num:  data=(1+Rand()%50);

And there is an error I keep getting for the depth!? It says I can't give it a default value.

0
AlexNekCommented:
Aach, You have tree and treeNode classes.
Why then you use "int" for tree?
Tree < int > intTree
0
AlexNekCommented:
If you want to change depth
then
int depth=5;
must be static;

if (depth=0)
                  children_num=0;
            else {
                     depth=depth-1;
0
SerphAuthor Commented:
I think when I started I was a bit confused, sorry!
Again, I just realized I don't need a template!  I already know the data type...-_-;;

So I merged the two files to have only one header file.  By the way, this what I have in my mind

[data=2+child_num=3+depth=2]
 |
[data=4+child_num=1+depth=1] [data=9+child_num=2+depth=1] [data=7+child_num=0+depth=1]
 |                                                                 |
[data=1+child_num=0+depth=0]           [data=5+child_num=0+depth=0][ data=6+child_num=0+depth=0]

I think I'll need a root pointer and a pointer for each dynamic array created, right?


#ifndef TREE_H
#define TREE_H

#include <iostream>
#include <new>
#include <cstdlib>
#include <cmath>


class Tree {

      static int depth=5;
      int data;
      int children_num;
      Tree *children;
      

      public:
      Tree* m_pNext;
      Tree()
      {
            data=(1+rand()%50);
            if (depth=0)
                  children_num=0;
            else {
                     depth=depth-1;
                     children_num=(1+rand()%5);
                 }
      }
      void Tree::printTree()
{
      if (m_pNext!=NULL)
      {
            printTree();
            cout<<m_pNext->data<<' ';
      }
};
      };
#endif
-=----------------------------------
main prog file
#include <iostream>
using namespace std;

using std::cout;
using std::cin;
using std::fixed;

#include <iomanip>
using std::setprecision;

#include "Tree.h"

int main()
{
      Tree* Node = new Tree();
    Node->m_pNext = Node;
      Node->printTree();
      return 0;
}
0
AlexNekCommented:
Try to understood base data structure first,please.
http://www.dreamincode.net/forums/showtopic10157.htm

You are rigth, you need the Root and don't forget to create each node, not only one.
Pay attetion,
- that all data member declaration must use the same "template" for naming convention.
- static data member needed two declaration
- how to calculate the node depth
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SerphAuthor Commented:
Thanks, that was really helpful, but can you help me with one last thing?  Is this correct? I seperated it into to functions, one to add a node, the other to add a child node.  Because now that I think about it, I'm adding a node to a tree, and child node to a linkedlist. Right?

void tree::add()
{
      leaf *t,*parent;

          static int depth=3;
          t=new leaf;
            t->data=(1+rand()%10);
            t->parent=NULL;
            t->children_num=(0+rand()%3);
            t->children=NULL;
            t->sibling=NULL;

            if(parent==NULL)
                  p=t;
            else
                  parent->children=t;
            depth=depth-1;
}

I also have an addchild function

void tree::addchild()
{
         if (t->children=NULL)    // add first child
          {c=new leaf;
         c->data=(1+rand()%10);
         c->parent=t;
         c->children_num=(0+rand()%3);
         c->children=NULL;
         t->children=c;
           c->sibling=NULL;}
   
         else
          {d=new leaf;
         d->data=(1+rand()%10);
         d->parent=t;
         d->children_num=(0+rand()%3);
         d->children=NULL;
         c->sibling=d;
           c->sibling=NULL;}
}
0
AlexNekCommented:
It look better. Yes, you are rights. For the the next level you can use "children" and for the next child on the level "sibling".

I missed declaration of p in add() function and assigment operartor for parent.
The next step. Use a loop for add more that one child, level.
0
SerphAuthor Commented:
If I can give more than 500 points, I would!

Thank you so much!!! This was the most confusing part of my assignment, the rest is much easier.

Thanks again for the resources as well,
Serph
0
AlexNekCommented:
You are welcome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.