`I did my best but I guess my best wasn't good enough :((

Sep 23, 2010 at 11:55am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
using namespace 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")
Sep 23, 2010 at 11:59am
What kind of application are you using ? Dev C++ 4.9.9.2 ?? Borland C++ ??
try use the program with other C++ application, ok ?
Sep 23, 2010 at 12:03pm
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.
Sep 23, 2010 at 12:44pm
`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
Sep 23, 2010 at 1:14pm
hello ,

The problem is this
TEMP=TEMP->PR;
TEMP->PL=&t[4];
TEMP->PR=NULL;

TEMP=TEMP->PL;
TEMP->PL=NULL;
TEMP->PR=NULL;
t[4].DATA=15;


given an array t[4] you can only use t[0] ... t[3]. Using t[4] will corrupt the stack.
Sep 23, 2010 at 1:36pm
`should I change the array size to 5?
Sep 23, 2010 at 1:57pm
yes, that would solve your current problem. Take a look at this: http://www.cplusplus.com/doc/tutorial/arrays/
Sep 24, 2010 at 8:52am
`wow thanks dude, it works fine now.. :))
Topic archived. No new replies allowed.