Solved

Help with C++ Program

Posted on 2007-04-10
12
194 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

679 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