#include <iostream>
usingnamespace std;
struct node
{
int DATA;
node *PL;
node *PR;
};
void preorder(node *HEAD);
void inorder(node *HEAD);
void postorder(node *HEAD);
int countNodes( node *HEAD);
main (void){
int nodez;
int numbernode;
struct node t[4];
struct node *HEAD;
struct node *TEMP;
HEAD=&t[0];
t[0].DATA=8;
HEAD->PL=&t[1];
TEMP=HEAD;
HEAD=HEAD->PL;
t[1].DATA=0;
HEAD->PL=NULL;
HEAD->PR=NULL;
HEAD = TEMP;
TEMP->PR=&t[2];
t[2].DATA=9;
TEMP=TEMP->PR;
TEMP->PL=NULL;
TEMP->PR=&t[3];
t[3].DATA=20;
TEMP=TEMP->PR;
TEMP->PL=&t[4];
TEMP->PR=NULL;
TEMP=TEMP->PL;
TEMP->PL=NULL;
TEMP->PR=NULL;
t[4].DATA=15;
numbernode=countNodes(HEAD);
cout<<"the number of nodes: "<<numbernode<<endl;
cout<<"the preorder of the tree is: ";
preorder(HEAD);
cout<<endl<<"the postorder of the tree is: ";
postorder(HEAD);
cout<<endl<<"the preorder of the tree is: ";
inorder(HEAD);
cout<<endl;
cout<<"\n \n \n ";
system ("PAUSE");
}
void preorder(node *HEAD){
if (HEAD!=NULL){
cout<<HEAD->DATA<<" ";
preorder(HEAD->PL);
preorder(HEAD->PR);
}
}
void inorder(node *HEAD){
if (HEAD!=NULL){
inorder(HEAD->PL);
cout<<HEAD->DATA<<" ";
inorder(HEAD->PR);
}
}
void postorder(node *HEAD){
if (HEAD!=NULL){
postorder(HEAD->PL);
postorder(HEAD->PR);
cout<<HEAD->DATA<<" ";
}
}
int countNodes( node *HEAD){
if (HEAD == NULL) {
return 0;
}
else {
int count = 1;
count += countNodes(HEAD->PL);
count += countNodes(HEAD->PR);
return count;
}
}
`I really don't understand whats wrong with this code. It works fine if I remove the declaration "int nodez", but If I include it on the program, IT CRASHES.
I dont understand why... need help pls... :(((
(actually, It took me 1hr to guess wats wrong w the code, and when I tried to remove the "int nodez", I was like, "wth?! O_O")
I tryed to compile with the Dev C++, it is compiling but it gives me error when i start the program. sorry man but there is something wrong, something like the syntax.
`I'm using Dev C++ 4.9.9.2... its the application our prof. required to us.. I'll try to use Borland, but is it free?? :((
yes.. but try to remove the int nodez, look it will run!
but.. geez. I guess I'm gonna copy my mate's code nao. lol