what is wrong in this code

#include<iostream.h>
#include<string>
#include <cstdlib>
struct node//node type

{
int c_code;//current acount number



node *r;


node *l;

};//end struct
node * head2;

/////////////////////////////////////////
////////////////////////////////////////
int pre[11];

int post[11];

int in[11];

int index;

class tree
{
private:
node * newnode,*head;

public:
tree();
void create_tree();
void pre_order(node * T)
{
if (!(T=NULL))
{
pre[index]=T->c_code;
index++;
pre_order(T->l);
pre_order(T->r);
}
}

void post_order(node * T)

{
if (!(T=NULL))
{
post[index]=T->c_code;
index++;
post_order(T->l);
post_order(T->r);
}
}
void in_order(node * T)
{
if (!(T=NULL))
{
in[index]=T->c_code;
index++;
in_order(T->l);
in_order(T->r);
}
}
};
tree::tree()//constructor
{
newnode=head=head2=NULL;
}
void tree::create_tree()
{
//head=new node;
head2=head;
head->c_code=18;
head->l=NULL;
head->r=NULL;
newnode=new node;
newnode->c_code=95;
newnode->l=NULL;
newnode->r=NULL;
head->l=newnode;
newnode=new node;
newnode->c_code=41;
newnode->l=NULL;
newnode->r=NULL;
head->l->l=newnode;
newnode=new node;
newnode->c_code=29;
newnode->l=NULL;
newnode->r=NULL;
head->l->r=newnode;
newnode=new node;
newnode->c_code=52;
newnode->l=NULL;
newnode->r=NULL;
head->l->r->l=newnode;
newnode=new node;
newnode->c_code=74;
newnode->l=NULL;
newnode->r=NULL;
head->l->r->l->l=newnode;
newnode=new node;
newnode->c_code=86;
newnode->l=NULL;
newnode->r=NULL;
head->r=newnode;
newnode=new node;
newnode->c_code=33;
newnode->l=NULL;
newnode->r=NULL;
head->r->r=newnode;
newnode=new node;
newnode->c_code=57;
newnode->l=NULL;
newnode->r=NULL;
head->r->r->l=newnode;
newnode=new node;
newnode->c_code=20;
newnode->l=NULL;
newnode->r=NULL;
head->r->r->r=newnode;
newnode=new node;
newnode->c_code=17;
newnode->l=NULL;
newnode->r=NULL;
head->r->r->l->r=newnode;
cout<<"tree completed \n";


}
int main()
{
cout<<"jay";
int i;
tree obj;
//tree creation
obj.create_tree();



//pre order
index=0;
cout<<"Pre Order Output\n";
obj.pre_order(head2);
for (i=0;i<=11;i++)
{
cout<<pre[i]<<"\t";
}

//post order
index=0;
cout<<"Post Order Output\n";
obj.post_order(head2);
for (i=0;i<=11;i++)
{
cout<<post[i]<<"\t";
}

//in order
index=0;
cout<<"In Order Output\n";
obj.in_order(head2);
for (i=0;i<=11;i++)
{
cout<<in[i]<<"\t";
}
}

I don't think iostream.h exists, use #include <iostream> instead of #include <iostream.h>?
1
2
3
4
for (i=0;i<=11;i++)
{
	cout<<pre[i]<<"\t";
}
This loop will access pre[11]. pre only have 11 elements so the last element in the array is pre[10] (remember that array indices start at 0). Either make the array one bigger or change you loop condition to exclude i==11.
Topic archived. No new replies allowed.