Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
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;
}

The header is:
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

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)
   }
 
Share this answer
 
Comments
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?
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();
	}
 
Share this answer
 
v2
Comments
PIEBALDconsult 21-Nov-15 14:07pm    
Please don't answer your own question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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