Binary Tree

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Binary Tree

Postby Sscorpius » Wed Mar 28, 2012 12:47 am

Hello! I have to implement a binary tree using classes, in OOP. But i really don't know how to build the copy constructor and the asignment operator. Well, i'm unsure about the deconstructor too; and the overwriting of "<<" keeps returning some errors. Here it is a part of the code.

Code: Select all
struct nod{
       int info;
       nod *st, *dr;};
       int n;
       
class arborebin
      {      nod *rad;
             int stergere(nod *&);
             void stergere_nod(nod *&,int);
       public:
              arborebin() {rad=NULL; n=0;};
              ~arborebin(){destroy (rad);};
              void destroy (nod *);
              void parcurgere_srd(nod *);
              void srd();
              void parcurgere_rsd(nod *);
              void rsd();
              void parcurgere_sdr(nod *);
              void sdr();
              nod *inserare_nod(nod *,int);
              void operator+(int);
              void operator-(int);
              void operator=(nod *);
              friend ostream& operator << (ostream&, arborebin&);
       };
       
void arborebin::destroy(nod *q)
{
     if (!q) return;
     destroy(q->st);
     destroy(q->dr);
     delete q;


Now i create the tree, etc. An then:
Code: Select all
void arborebin::parcurgere_srd(nod *rad)
{
     if (rad)
        {parcurgere_srd(rad->st);
         cout<<rad->info<<" ";
         parcurgere_srd(rad->dr);}
}

void arborebin::srd()
{
     nod *p;
     p=rad;
     parcurgere_srd(p);
}

ostream& operator << (ostream &out, arborebin &arb)
{
       out<<arb.srd();
       return out;
}   


I need some help, please..
Sscorpius
 
Posts: 4
Joined: Wed Mar 28, 2012 12:34 am

Re: Binary Tree

Postby exomo » Wed Mar 28, 2012 6:38 am

For copy constructor and destructor you could implement both of them for the nod struct. (similar to the destroy method)
If you du so the destructor of arborebin is still as simple as "delete rod;", but you don't need the destroy function.
In the operator<< function you have to use known types. For example srd() could return a string. I don't know what is the best way to print a tree, but it could help if you add a toString method or something to nod.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 935
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Binary Tree

Postby Sscorpius » Wed Mar 28, 2012 3:08 pm

Can you be a little more explicit, please? i didn't understand how the copy constructor could be similar to the destroy method.
Sscorpius
 
Posts: 4
Joined: Wed Mar 28, 2012 12:34 am

Re: Binary Tree

Postby exomo » Thu Mar 29, 2012 5:10 am

OK, perhaps it was a little unprecise. The constructor and deconstructor are similar to destroy in a way that they recursiveley call the same method for the child nodes. It could look like this:
Code: Select all
struct nod{
       int info;
       nod *st, *dr;

       nod(int info) : info(info), st(NULL), dr(NULL) {}

       nod(const nod & orig_nod)
       {
           if(orig_nod.dr==NULL)
           {
               dr = NULL;
           }
           else{
               dr = new nod( *orig_nod.dr );
           }
           if(orig_nod.st==NULL)
           {
               st = NULL;
           }
           else{
               st = new nod( *orig_nod.st );
           }
       }

       ~nod()
       {
           delete st;
           delete dr;
       }
};

int n;

class arborebin
      {      nod *rad;
             int stergere(nod *&);
             void stergere_nod(nod *&,int);
       public:
              arborebin() {rad=NULL; n=0;}; // default constructor
              arborebin(const arborebin &orig_arborebin) {rad = new nod(*orig_arborebin.rad);} // copy constructor
              ~arborebin(){ delete rad;};
...

I also have changed the default constructor of nod to a constructor taking it's value as parameter as I think this is a nicer way to initialize a node, but you could leave that as it was and just use a default constructor. I don't how you create the nodes so I'm probably wrong here.
Additionally you should implement an assignment operator for your arborebin class.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 935
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Binary Tree

Postby Sscorpius » Thu Mar 29, 2012 6:08 am

And this assignment operator does kind of the same thing as the copy constructor?
Sscorpius
 
Posts: 4
Joined: Wed Mar 28, 2012 12:34 am

Re: Binary Tree

Postby exomo » Thu Mar 29, 2012 12:07 pm

The assignment operator is similar, but a little more things to do.
There is a good (and lengthy) article on the assignment operator here : http://icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html. If you want to learn something read the article, if you just want your program to work copy his assignment operator and adjust it to your calss.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 935
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Binary Tree

Postby Sscorpius » Thu Mar 29, 2012 2:38 pm

Thank you very much for your help, exomo!
Sscorpius
 
Posts: 4
Joined: Wed Mar 28, 2012 12:34 am

Re: Binary Tree

Postby arthunna » Fri Apr 20, 2012 1:23 am

Can NOD32 remove a sniffer program that was sent through an email? I want to see if a sniffer program was sent to me through an email. If NOD32 can't then is there another program I can use in conjunction to NOD32 to detect it?
_________________________________
external keyword tool ~ keyworddiscovery.com ~ keycompete.com ~ compete.com ~ webmasterworld.com
arthunna
 
Posts: 1
Joined: Wed Apr 18, 2012 11:25 pm


Return to Homeworks

Who is online

Users browsing this forum: Google Adsense [Bot] and 6 guests