Fetch binary tree recursively in ascending order

My current implementation of how to output my binary tree leads to an error in g ++ according to

Conditional jump or move depends on uninitialised value(s) 

My current implementation:

 void Foo::output(ostream &s, const Node *p) { if( p ) { output( s , p -> left ); s << p -> info; output( s , p -> right ); } } 

Node is a basic structure with a left and right pointer and an integer information variable.

ostream is just cout

The error message is very direct, I donโ€™t like that I let it โ€œrun awayโ€.

My question is twofold:

  • Why is this wrong? Nothing changes, I do not know what it can hurt.
  • What is the right way to do this?

thanks

+4
source share
1 answer

This basically means that some Node objects do not have left and right initialized to zero.

It is usually a good idea to define a node something like this.

 class Node { int info; Node* left; Node* right; public: Node( int infoin , Node* leftin = NULL , Node* rightin = NULL ) : info(infoin) , left(leftin) , right(rightin) {} } 

Thus, if the left and right nodes are not known at build time, they are null.

And if they are really known when building Node , you donโ€™t pay a fine for installing right and left to zero and then something else

+6
source

Source: https://habr.com/ru/post/1381233/


All Articles