I have the following code ( I'm creating a binary tree and then I want to print the values on it's leafs) but when I'm running it, it never enters in that if from the parcurgere function. What have I done wrong?

#include <iostream>
using namespace std;
class node
{
public: int val;
		static int niv;
		node *st;
		node *dr;
		node(int i)
		{
			val = i;
			st = NULL;
			dr = NULL;
		}
};

void creare(node *p)
{
	int x;

cin>>x;
if(x == 0) 
{
	p = NULL;
}
else 
{
	node *p = new node(x);
	cout<<"dati fiul stang al lui "<<x<<" ";
	creare(p->st);
	cout<<"dati fiul drept al lui "<<x<<" ";
	creare(p->dr);
}
}
void parcurgere(node *p)
{
	if(p != NULL)
	{
		cout<<p->val;
		parcurgere(p->st);
		parcurgere(p->dr);
	}
}
void main()
{
	node *t = NULL;
	cout<<"dati radacina arborelui ";
	creare(t);
	parcurgere(t);
	
}

Edited 6 Years Ago by Nick Evan: Added code-tags

I have the following code ( I'm creating a binary tree and then I want to print the values on it's leafs) but when I'm running it, it never enters in that if from the parcurgere function. What have I done wrong?

void creare(node *p)
{
	//your code
}
void parcurgere(node *p)
{
	if(p != NULL)
	{
		cout<<p->val;
		parcurgere(p->st);
		parcurgere(p->dr);
	}
}
void main()
{
	node *t = NULL;
	cout<<"dati radacina arborelui ";
	creare(t);
	parcurgere(t);
	
}

The function 'creare' does not change 't' , after the call 't' is still pointing to NULL and hence the 'if' condition in 'parcurgere' does not evaluate to TRUE. If you want 't' to point to a new object which is created in 'creare' then you should it pass the pointer by reference. So you function could be

void creare(node *& p)

And don't use void main

Comments
good answer
Fast, cheap, good. Sounds like a winner to me. :)

Well, t is NULL -- what do you expect?

node *t = NULL;
	cout<<"dati radacina arborelui ";
	creare(t);
	parcurgere(t);

Edited 6 Years Ago by Dave Sinkula: D'oh! Ten minutes slow? /me blames leaving tabs open too long.

Thank you for your answer! I passed the parameter by reference but I'm still having the same problem... t is still pointing to NULL...

#include <iostream>
using namespace std;
class node
{
public: int val;
        static int niv;
        node *st;
        node *dr;
        node(int i)
        {
            val = i;
            st = NULL;
            dr = NULL;
        }
};

void creare(node *&p)
{
    int x; 

cin>>x;
if(x == 0) 
{
    p = NULL;
}
else 
{
    node *p = new node(x);
    cout<<"dati fiul stang al lui "<<x<<" ";
    creare(p->st);
    cout<<"dati fiul drept al lui "<<x<<" ";
    creare(p->dr);
}

}
void parcurgere(node *p)
{
    cout<<"a";
    if(p != NULL)
    {
        cout<<p->val;
        parcurgere(p->st);
        parcurgere(p->dr);
    }
}
int main()
{
    node *t =NULL ;
    cout<<"dati radacina arborelui ";
    creare(t);
    parcurgere(t);
    return 0;

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I think you have to re-implement this code from scratch, this just doesn't seem like an implementation of a binary tree. I would suggest that you read some tutorial on Binary trees before you start. Here are some good links Binary Trees , Binary search trees . Or just find some book and go through them and then try again.

I think you have to re-implement this code from scratch, this just doesn't seem like an implementation of a binary tree. I would suggest that you read some tutorial on Binary trees before you start. Here are some good links Binary Trees , Binary search trees . Or just find some book and go through them and then try again.

You were right ;)

This question has already been answered. Start a new discussion instead.