-2
class node{
    unsigned long int data;
    node *lchild,*mchild,*rchild;         //childs of ternery tree
    unsigned long int *stack1,*stack2;
    static int count,top1,top2;
  public:
    node()
    {
         data=0;
         lchild->data=0;
         mchild->data=0;
         rchild->data=0;
    }
    node(int x)
    {
         data=0;
         lchild->data=0;
         mchild->data=0;
         rchild->data=0;
    }
node(unsigned long int d)
    {
     data=d;
     lchild->data=floor(d/2);
     mchild->data=floor(d/3);
     rchild->data=floor(d/4);
    }
node(node* n)
    {
     data=n->data;
     lchild->data=n->lchild->data;
     mchild->data=n->mchild->data;
     rchild->data=n->rchild->data;
    }
void mallocate(int x)
    {
     stack1=new unsigned long int[x];
     stack2=new unsigned long int[x];
    }
void free()
    {
     delete[] stack1;
     delete[] stack2;
    }


void storedata(node *);
void backtrack(node *);

}root,temp,*block;
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
Ranjeet
  • 1
  • 1

1 Answers1

2

In this constructor:

 node()
    {
         data=0;
         lchild->data=0;
         mchild->data=0;
         rchild->data=0;
    }

you are using yet uninitialized pointers lchild, mchild and rchild. You should remove those 3 lines:

         lchild->data=0;
         mchild->data=0;
         rchild->data=0;

And then you have to rethink your design. New node should just initialize itself. You have to build the tree structure outside of your node class. Of if you insist on building something in node class you should allocate it properly.

You have more constructor methods that do access data that doesn't exist:

node(int x);
node(unsigned long int d);
node(unsigned long int d);
node(node* n);

I would advise you to just construct the tree... data will be set to zero for each node in constructor with the line you already have: data=0;. All your special calculations should be put outside of constructors, you could have just one constructor node and maybe one for setting the data for the constructed node.

Then, you're not calling mallocate method in any of your constructors, you should do that in all of them. Next you should make a destructor ~node and call the this->free method from it. Anyway the method has an ambiguous name to the standard free function from stdlib.h, i would rename it to something else, for example cleanup.

Normally you would set all three pointers to null in the constructor:

 node()
    {
         data=0;
         lchild=NULL;
         mchild=NULL;
         rchild=NULL;
    }

And then you would build the structure like this:

int main()
{
    node n1;
    node *l = new node;
    n1.lchild = l;
    ...
}
nio
  • 5,141
  • 2
  • 24
  • 35