Solved

Help with C++ Program

Posted on 2007-04-10
12
190 Views
Last Modified: 2010-04-01
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;
}

0
Comment
Question by:Serph
  • 7
  • 5
12 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 18885066
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
 
LVL 1

Author Comment

by:Serph
ID: 18885132
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18885453
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
 
LVL 1

Author Comment

by:Serph
ID: 18885544
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18885596
Aach, You have tree and treeNode classes.
Why then you use "int" for tree?
Tree < int > intTree
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18885622
If you want to change depth
then
int depth=5;
must be static;

if (depth=0)
                  children_num=0;
            else {
                     depth=depth-1;
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:Serph
ID: 18888188
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
 
LVL 16

Accepted Solution

by:
AlexNek earned 500 total points
ID: 18888518
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
 
LVL 1

Author Comment

by:Serph
ID: 18891121
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18891337
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
 
LVL 1

Author Comment

by:Serph
ID: 18891479
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
 
LVL 16

Expert Comment

by:AlexNek
ID: 18891863
You are welcome!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now