15,902,447 members
2.00/5 (1 vote)
See more:
Hello there,

I tried to make an implementation of a function in my header that prints recursively by inorder method a binary tree search (printing each node from the lowest value to the biggest)

The function itself do not take any parameters
I tried to do this on my own for more than a few hours now.

I will be happy if anyone could help me figure this out

These are the implementations:
C++
```void BSNode::printNodes() const //prints the smallest _data value to the biggest
{
if (_left == NULL)
{
cout << _data << endl;
}
else
{
return _left->printNodes();
}

if (_right == NULL)
{
cout << _data << endl;
}
else
{
return _right->printNodes();
}
}

BSNode::BSNode(string data)
{
_data.clear();
_data.assign(data);
_count=1;

_right = NULL;
_left = NULL;
}

BSNode::BSNode(const BSNode& other)
{
_data.clear();
_data.assign(other._data);
_count = other._count;

_right = other._right;
_left = other._left;
}

BSNode::~BSNode()
{
delete _right;
delete _left;
_data.clear();
_count =0;
}```

C++
```class BSNode
{
public:
BSNode(string data);
BSNode(const BSNode& other);
~BSNode();
void insert(string value);
string getData() const;
BSNode* getLeft()const ;
BSNode* getRight() const;
void printNodes() const;//prints the smallest _data value to the biggest

private:

BSNode* _left;
BSNode* _right;
string _data;
int _count;```
Posted
Updated 26-Dec-22 20:42pm
v3

## Solution 1

Stop for a moment and think about what you are trying to do.
What you want to do is take a node, print it's left graph (if any), then it's right graph (if any), then the node itself:
So if you have a binary tree:
```    1
/ \
2   3
/   / \
4   5   6
```
You want to print
`4 2 5 6 3 1`
Yes?

You aren't far off, but your code returns too early. Try something a little simpler:
(This is your homework, so I won't give you actual code here)
C++
```node.Print
{
if (n.Left != null) n.Left.Print()
if (n.Right != null) n.Right.Print()
Display(n.Text)
}```

TotalyNotUbisoft 21-Nov-15 11:59am
I think i need to print 1 2 3 4 5 6
OriginalGriff 21-Nov-15 12:20pm
You can't do that recursively, because that would mean "pausing" and coming back later.
You can print
1 2 4 3 5 6
easily, which is probably what you really want - just move the print to the top of teh function, rather than the bottom.
TotalyNotUbisoft 21-Nov-15 12:24pm
the hardest part is to figure out how to print it without getting any root, I mean how to I move from node to node without getting anything?
PIEBALDconsult 21-Nov-15 13:21pm
Ummm... what? I don't think your tree is a binary search tree. How about putting the values in their expected places?

## Solution 2

I managed to find a solution.....somehow.... took me a while...

C#
```void BSNode::printNodes() const //prints the smallest _data value to the biggest
{

if (_left != NULL)
{
_left->printNodes();
}

cout << _data  << endl;

if (_right != NULL)
{
_right->printNodes();
}```

v2
PIEBALDconsult 21-Nov-15 14:07pm

Top Experts
Last 24hrsThis month
 Greg Utas 40 Richard Deeming 40 Ralf Meier 40 Pete O'Hanlon 25 Jo_vb.net 20
 Pete O'Hanlon 1,580 OriginalGriff 1,202 Richard Deeming 360 Richard MacCutchan 355 Dave Kreskowiak 320

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900