### Segmentation fault (core dumped)???

Okay so we are talking about binary trees in class and were told to practice. This is based off of (and is very close to) code that our teacher gave us in class, so I am hoping this is just some typo and that I am not a complete dunce. However, he was writing it on the board and never typed it up and compiled it.

To my knowledge, this should just generate random numbers, insert them in a binary tree, and then the traverse function should print them out.

When I compile everything goes fine but when I run the program it just says "Segmentation fault (core dumped)." I tried a google search but most of what I found assumes you have some sort of debugging software, which we don't. What am I missing?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` `````` #include #include using namespace std; struct Node { Node *ll; int info; Node *rl; }; void traverse (Node *r) { if (r->ll != NULL) traverse (r->ll); cout << r->info << endl; if (r->rl != NULL) traverse (r->rl); } int main() { int i, t, done = 0; Node *root = NULL, *r, *p; //Create Binary Tree for (i = 0; i < 9; i++) { t = rand()%100; p = new Node; p->ll = p->rl = NULL; p->info = t; if (root = NULL) root = p; else { //Inserting Binary Tree r = root; while (done = 0) { if (p->info <= r->info) { if (r->ll != NULL) r = r->ll; else { r->ll = p; done = 1; } } else { if (r->rl != NULL) r = r->rl; else { r->rl = p; done = 1; } } } } } traverse (root); } ``````

Any insight is greatly appreciated!
Last edited on
> assumes you have some sort of debugging software, which we don't
You should

http://www.cplusplus.com/forum/general/112111/#msg612042
 ```\$ g++ -W{all,extra,pedantic} foo.cpp foo.cpp: In function ‘int main()’: foo.cpp:31:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses] foo.cpp:36:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]```

Last edited on
You're using assignment `=` operator in your conditional statements, instead of the comparison operator `==`. Which is what the debug output @ne555 posted is complaining about.
Last edited on
It is not the `debug output', those are the warnings of the compiler
That was the problem, however now I have another. I have two different traversal functions coded at this point, one recursive and one not, but both only return two numbers and then stop. They are coded almost exactly as the professor's functions, but for some reason his works and mine does not. Currently the recursive traversal function is commented out, but I have tried both of them and they each print two numbers when they should print the entire tree.

 ``123456789101112131415161718192021222324252627282930`` ``````//Non-recursive traversal void inorder (struct Node *t) { stack stk; while (t != NULL || !stk.empty()) { if (t == NULL) { t = stk.top(); stk.pop(); cout << setw(5) << t->info; t = t->rl; } if (t != NULL) { stk.push(t); t = t->ll; } } } //Recursive traversal...not working /* void traverse (struct Node *r) { if (r->ll != NULL) traverse (r->ll); cout << r->info << " "; if (r->rl != NULL) traverse (r->rl); } */``````
Topic archived. No new replies allowed.